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?

Gerald
  • 521
  • 1
  • 6
  • 16
  • 1
    ummm a `BigInteger` is for integers, not floating point math. – Daniel A. White Feb 06 '14 at 15:25
  • possible duplicate of [Calculate square root of a BigInteger (System.Numerics.BigInteger)](http://stackoverflow.com/questions/3432412/calculate-square-root-of-a-biginteger-system-numerics-biginteger) – AlexH Feb 06 '14 at 15:32
  • So how would I do floating point math on very large numbers? – Gerald Feb 06 '14 at 15:36
  • use `double` data type and for more accuracy you can use `decimal` – Sarvesh Mishra Feb 06 '14 at 15:40
  • see the last three answers here (http://matheplanet.com/matheplanet/nuke/html/viewtopic.php?rd2&topic=178333&start=40#p1332062), the last bitwise square root is really the fastest. – Lutz Lehmann Feb 07 '14 at 16:27

1 Answers1

1

You are using the wrong data type. In order to have decimal points, you would need to use double, float, decimal, or Complex.

Check the links of all these types so you can see their digits of precision.

Joe Brunscheon
  • 1,949
  • 20
  • 21