1

I have a number 998001983016242063 and I want to take the exact square root of it. However, when I try to take the square root, python rounds my number to the integer part.

a=998001983016242063
print(a**0.5) # 999000492.0 Instead of 999 000 491,999999

I tried to use math.sqrt, pow, **0.5 and didn`t find a solution

Serg005
  • 31
  • 2
  • The reason is *not Python*, but *double floating-point numbers*. You can know something from https://stackoverflow.com/questions/588004/is-floating-point-math-broken – cup11 Apr 16 '23 at 11:42
  • 1
    999 000 491,999999 isn't exact, either. – Kelly Bundy Apr 16 '23 at 12:56
  • 1
    What form would you like this "exact square root" represented in? `998001983016242063` is not a square number, so its square root has a non-terminating decimal expansion. So if you really want an exact result, it's difficult to know what answer you were hoping for. Or do you just want the integer part of the square root to be calculated correctly? That is, you want `999000491`? – Mark Dickinson Apr 24 '23 at 17:58

1 Answers1

4

For arbitrary precision calculations, you can use decimal.Decimal:

>>> from decimal import Decimal
>>> a=998001983016242063
>>> Decimal(a).sqrt()
Decimal('999000491.9999999994994997460')
erip
  • 16,374
  • 11
  • 66
  • 121