-1

I need to make a Math.Sqrt calculation but it must support really big numbers so I'm trying to use BigInteger but Math.Sqrt doesn't support them.

Are there any other options? How can I do this calculation and result is double ?

Sample :

sqrt 5 = 2,2360679774997896964091736687313 
sqrt 4789798765456456456 = xxxxx,xxxxxxxxxxx norm.
gturri
  • 13,807
  • 9
  • 40
  • 57
CarlesBerg
  • 33
  • 4
  • `double` d = Math.Sqrt(3); – Avijit Dec 28 '14 at 16:06
  • That makes little sense. You will first have to write a BigDouble class so you can represent the result. – Hans Passant Dec 28 '14 at 16:07
  • 1
    How big is "really big"? `double`s go quite high already – harold Dec 28 '14 at 16:07
  • 1
    You could use the fact that `Log(Sqrt(x)) = Log(x) / 2`. Use a BigDecimal, though. – SJuan76 Dec 28 '14 at 16:09
  • @SJuan76 The Math.Log method takes (double) or (double,double). I think the Math libary just doesn't have support of BigIntegers. Either the OP will have to self-implement (Newtons method) or find a library that does (Does anyone know of one?). – Nathan Cooper Dec 28 '14 at 16:11
  • Does your code need to work with integers outside the range of `double` (which is pretty huge)? If not, you could convert to `double` first, then use `Math.Sqrt`, then convert back if you need to (and perform some checks for boundaries) – Jon Skeet Dec 28 '14 at 16:13
  • @NathanCooper There is a `BigInteger.Log`... but anyway, for this method to work, it should deal with decimals, and it seems .Net lacks a `BigDecimal`. – SJuan76 Dec 28 '14 at 16:13
  • please help me I cant – CarlesBerg Dec 28 '14 at 16:52
  • 1
    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) – Erti-Chris Eelmaa Dec 28 '14 at 17:25
  • @ChrisEelmaa its not working – CarlesBerg Dec 28 '14 at 17:43
  • @...everyone I found the question confusing and commented on the basis that the OP wanted a BigInteger outputs for BigInteger inputs, which I'm not convinced it correct. – Nathan Cooper Dec 28 '14 at 19:40

1 Answers1

-2

The easiest way is probably to run Newton's method for square roots starting with a double-precision initial guess.

To speed this up, notice that Newton's method doubles its precision with every iteration. You can double your precision with every iteration as well by keeping track of an int32 "exponent" together with your BigInteger "mantissa", increasing the precision of the "mantissa" by a factor of two with each step until sufficient precision is reached.

tmyklebu
  • 13,915
  • 3
  • 28
  • 57