【问题标题】:How to use Newton-Raphson method to find the square root of a BigInteger in C#如何使用 Newton-Raphson 方法在 C# 中找到 BigInteger 的平方根
【发布时间】:2014-03-03 15:16:27
【问题描述】:

所以我尝试使用 Newton-Raphson 方法来求 BigInteger 的平方根。

这是我的代码:

            private void sqrRt(BigInteger candidate)
            {
                BigInteger epsilon = new BigInteger(0.0001);
                BigInteger guess = candidate / 2;

                while (BigInteger.Abs(guess * guess - candidate) >= epsilon)
                {
                    // guess = guess - (((guess**2) - y)/(2*guess))
                    guess = BigInteger.Subtract(guess, BigInteger.Divide(BigInteger.Subtract(BigInteger.Multiply(guess, guess), candidate), BigInteger.Multiply(2, guess)));
                    MessageBox.Show(Convert.ToString(guess));
                }
            }

问题似乎是 BigInteger 不够精确,无法落在 while 循环中 epsilon 的准确度范围内 - 即它需要小数位。我的问题是什么/如何/在哪里转换为 double 以使 while 循环最终返回 false?

【问题讨论】:

标签: c# biginteger square-root newtons-method


【解决方案1】:

您使用了错误的数据类型。为了有小数点,您需要使用doublefloatdecimalComplex

检查所有这些类型的链接,以便查看它们的精度数字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 2017-07-11
    • 1970-01-01
    • 2016-08-23
    相关资源
    最近更新 更多