Profile photo for Kirby Urner

Use the decimal module (Standard Library) or the 3rd party library gmpy2 to escape the constraints of the floating point type.

gmpy2 is faster I think, but maybe that’s not always the most important consideration. It also does more trigonometry.

Here’s a story on Medium about the topic.

Quoting:

Using the decimal library, we can go:

from decimal import *
getcontext().prec = 900
root5 = Decimal(‘5’).sqrt()
Ф = (1 + root5)/2

and get:

Ф=Decimal(‘1.61803398874989484820458683436563811772030917980576286213544862270526046281890244970720720418939113748475408807538689175212663386222353693179318006076672635443338908659593958290563832266131992829026788067520876689250171169620703222104321626954862629631361443814975870122034080588795445474924618569536486444924104432077134494704956584678850987433944221254487706647809158846074998871240076521705751797883416625624940758906970400028121042762177111777805315317141011704666599146697987317613560067087480710131795236894275219484353056783002287856997829778347845878228911097625003026961561700250464338243776486102838312683303724292675263116533924731671112115881863851331620384005222165791286675294654906811317159934323597349498509040947621322298101726107059611645629909816290555208524790352406020172799747175342777592778625619432082750513121815628551222480939471234145170223735805772786160086883829523045926’)

View 3 other answers to this question
About · Careers · Privacy · Terms · Contact · Languages · Your Ad Choices · Press ·
© Quora, Inc. 2025