Arbitrary precision of square roots

Question:

I was quite disappointed when decimal.Decimal(math.sqrt(2)) yielded

Decimal('1.4142135623730951454746218587388284504413604736328125')

and the digits after the 15th decimal place turned out wrong. (Despite happily giving you much more than 15 digits!)

How can I get the first m correct digits in the decimal expansion of sqrt(n) in Python?

Asked By: Randomblue

||

Answers:

IEEE standard double precision floating point numbers only have 16 digits of precision. Any software/hardware that uses IEEE cannot do better:

http://en.wikipedia.org/wiki/IEEE_754-2008

You’d need a special BigDecimal class implementation, with all math functions implemented to use it. Python has such a thing:

https://literateprograms.org/arbitrary-precision_elementary_mathematical_functions__python_.html

Answered By: duffymo

You can try bigfloat. Example from the project page:

from bigfloat import *
sqrt(2, precision(100))  # compute sqrt(2) with 100 bits of precision
Answered By: wong2

Use the sqrt method on Decimal

>>> from decimal import *
>>> getcontext().prec = 100  # Change the precision
>>> Decimal(2).sqrt()
Decimal('1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573')
Answered By: Nick Craig-Wood

How can I get the first m correct digits in the decimal expansion of sqrt(n) in Python?

One way is to calculate integer square root of the number multiplied by required power of 10. For example, to see the first 20 decimal places of sqrt(2), you can do:

>>> from gmpy2 import isqrt
>>> num = 2
>>> prec = 20
>>> isqrt(num * 10**(2*prec)))
mpz(141421356237309504880)

The isqrt function is actually quite easy to implement yourself using the algorithm provided on the Wikipedia page.

Answered By: Eugene Yarmash

You can get the first m correct digits in the decimal expansion of square root in python3, only with a specific sqr arithmetic function like this one:

sqr(‘2.0’, 100)
‘1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727’

https://github.com/giocip/sqrt/blob/main/sqr.py

Also with num7 package and the following code:

from num7 import Num  
n = Num('2.0')  
root = n.sqrt(100)  # set 100 digits after decimal point  
print(root)  # 1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727  
Answered By: giocip
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.