how can i show an irrational number to 100 decimal places in python?

clayton33 picture clayton33 · Jan 19, 2011 · Viewed 12.4k times · Source

I am trying to find the square root of 2 to 100 decimal places, but it only shows to like 10 by default, how can I change this?

Answer

Senthil Kumaran picture Senthil Kumaran · Jan 19, 2011

decimal module comes in handy.

>>> from decimal import *
>>> getcontext().prec = 100
>>> Decimal(2).sqrt()
Decimal('1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573')