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?
In computer science, arbitrary-precision arithmetic, also called bignum arithmetic, multiple-precision arithmetic, or sometimes infinite-precision arithmetic, indicates that calculations are performed on numbers whose digits of precision are limited only by the available memory of the host system.
Arbitrary-Precision arithmetic, also known as "bignum" or simply "long arithmetic" is a set of data structures and algorithms which allows to process much greater numbers than can be fit in standard data types.
Benchmark numbers can be used to state that the square root of 60 is between 6 and 7. The square root of 60, estimated to two decimal places is 7.75. Example: The square root of some numbers (for example the number 44.89) is a rational number that terminates (6.7).
Use the sqrt
method on Decimal
>>> from decimal import * >>> getcontext().prec = 100 # Change the precision >>> Decimal(2).sqrt() Decimal('1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With