Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arbitrary precision of square roots

Tags:

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?

like image 628
Randomblue Avatar asked May 23 '12 18:05

Randomblue


People also ask

What is the meaning of arbitrary-precision?

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.

What is arbitrary-precision C++?

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.

What are benchmarks in square roots?

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).


1 Answers

Use the sqrt method on Decimal

>>> from decimal import * >>> getcontext().prec = 100  # Change the precision >>> Decimal(2).sqrt() Decimal('1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573') 
like image 162
Nick Craig-Wood Avatar answered Sep 21 '22 01:09

Nick Craig-Wood