clayton33
clayton33

Reputation: 4216

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

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?

Upvotes: 16

Views: 15820

Answers (4)

maro
maro

Reputation: 503

You can use sympy and evalf()

from sympy import sqrt
print(sqrt(2).evalf(101))

Upvotes: 1

Christian Berendt
Christian Berendt

Reputation: 3546

You can use gmpy2.

import gmpy2
ctx = gmpy2.get_context()
ctx.precision = 300
print(gmpy2.sqrt(2))

Upvotes: 2

TryPyPy
TryPyPy

Reputation: 6434

You can use the decimal module for arbitrary precision numbers:

import decimal

d2 = decimal.Decimal(2)

# Add a context with an arbitrary precision of 100
dot100 = decimal.Context(prec=100)

print d2.sqrt(dot100)

If you need the same kind of ability coupled to speed, there are some other options: [gmpy], 2, cdecimal.

Upvotes: 7

Senthil Kumaran
Senthil Kumaran

Reputation: 56871

decimal module comes in handy.

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

Upvotes: 35

Related Questions