python - Unable to calculate exact cube root -
this question has answer here:
- is floating point math broken? 20 answers
i'm solving cryptographic problem,
i need cube root of 4.157786362549383e+37
, gives me 3464341716380.1113
using
x = x ** (1. / 3)
i thought weird @ first, did try:
x=1000 print(x) x= pow(x,1/3) print(x)
but got 9.99999998
i have tried somewhere else. got same result. there wrong? how can calculate true cube root?
due floating-point arithmetic, hard represent.
using decimal resolves still problematic in numbers, , allows rounding integrals. try using decimal so:
>>> (1000 ** (decimal(1)/3)).to_integral_exact() decimal('10')
Comments
Post a Comment