0

So I'm attempting to use the Newton-Raphson method to find the square root of a BigInteger.

Here is my code:

            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));
                }
            }

The problem seems to be that the BigInteger is not precise enough to fall within the degree of accuracy of the epsilon in the while loop - i.e. it needs a decimal place. My question is what/how/where do I convert to a double to make the while loop eventually return false?

4

1 回答 1

1

您使用了错误的数据类型。为了有小数点,您需要使用double, float, decimal, 或Complex.

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

于 2014-02-06T15:29:05.983 回答