搜索
bottom↓
回复: 8

请教迭代法计算平方根的整形算法

[复制链接]

出0入0汤圆

发表于 2015-11-12 22:22:58 | 显示全部楼层 |阅读模式
如下是牛顿迭代法求平方根的一般算法,这种算法是浮点运算,但不适合单片机等底端CPU,所以希望有程序达人能提供一种定点求平方根的方法,谢了!
  double my_sqrt(double data){//牛顿迭代算法
    double x0,x1;
    x0 = data / 2;
    do{
       x1 = x0;
       x0 = (x1 + data /x1) / 2;
    }while(fabs(x0-x1)>= 1e-6 );
    return x0;
}

阿莫论坛20周年了!感谢大家的支持与爱护!!

曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……

出0入0汤圆

发表于 2015-11-12 22:57:31 | 显示全部楼层
我觉得。全部放大若干倍就行了吧?

出0入0汤圆

发表于 2015-11-12 23:02:01 | 显示全部楼层
比如 要平方的数字放大10000倍。结果放大100倍。就是两位小数点了。

出0入0汤圆

发表于 2015-11-12 23:40:12 | 显示全部楼层
如果不需要精度。。。
http://stackoverflow.com/questio ... ithm-for-arm-thumb2

出0入0汤圆

 楼主| 发表于 2015-11-13 09:19:06 | 显示全部楼层
bg6agf 发表于 2015-11-12 23:02
比如 要平方的数字放大10000倍。结果放大100倍。就是两位小数点了。

这种方法不行的,3的平方根和30的平方根不成比例关系!

出0入0汤圆

发表于 2015-11-13 09:22:52 | 显示全部楼层
陶新成 发表于 2015-11-13 09:19
这种方法不行的,3的平方根和30的平方根不成比例关系!


应该说用3和300或者30000

出0入0汤圆

 楼主| 发表于 2015-11-13 09:52:38 | 显示全部楼层
eye 发表于 2015-11-12 23:40
如果不需要精度。。。
http://stackoverflow.com/questions/1100090/looking-for-an-efficient-integer-squ ...

这段代码是很不错的,至于精度它能满足一般的要求
/**
* \brief    Fast Square root algorithm, with rounding
*
* This does arithmetic rounding of the result. That is, if the real answer
* would have a fractional part of 0.5 or greater, the result is rounded up to
* the next integer.
*      - SquareRootRounded(2) --> 1
*      - SquareRootRounded(3) --> 2
*      - SquareRootRounded(4) --> 2
*      - SquareRootRounded(6) --> 2
*      - SquareRootRounded(7) --> 3
*      - SquareRootRounded(8) --> 3
*      - SquareRootRounded(9) --> 3
*
* \param[in] a_nInput - unsigned integer for which to find the square root
*
* \return Integer square root of the input value.
*/
uint32_t SquareRootRounded(uint32_t a_nInput)
{
    uint32_t op  = a_nInput;
    uint32_t res = 0;
    uint32_t one = 1uL << 30; // The second-to-top bit is set: use 1u << 14 for uint16_t type; use 1uL<<30 for uint32_t type


    // "one" starts at the highest power of four <= than the argument.
    while (one > op)
    {
        one >>= 2;
    }

    while (one != 0)
    {
        if (op >= res + one)
        {
            op = op - (res + one);
            res = res +  2 * one;
        }
        res >>= 1;
        one >>= 2;
    }

    /* Do arithmetic rounding to nearest integer */
    if (op > res)
    {
        res++;
    }

    return res;
}

出0入0汤圆

发表于 2015-11-13 23:59:09 | 显示全部楼层
陶新成 发表于 2015-11-13 09:19
这种方法不行的,3的平方根和30的平方根不成比例关系!

放大倍数必须按特定的倍数。我说的是10和10的平方。
3要和9配合。你完全没懂我的意思。

出0入0汤圆

发表于 2015-11-14 00:00:13 | 显示全部楼层
陶新成 发表于 2015-11-13 09:19
这种方法不行的,3的平方根和30的平方根不成比例关系!

在单片机上。可能用2的N次方和2的2N次方比较好。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-8-25 16:18

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表