-2

The exercise I was doing said that there'd likely be an issue when trying to solve this expression for 10^9 (first exercise is to try 10^7). I thought that it may have something to do with Python storing floats using 64 bits, but then the maximum signed integer is 9. ... x 10^18, which is more than the 10^18 generated using the x^2, and the maximum and minimum (absolute values) for floats is usually of the order of magnitude of 10^308 and 10^-308, if I'm not wrong. Also, I tried this using np.float128() and it works, but given the above limits I'm not sure why.

This doesn't work:

x = 10 ** 9
f00 = 1 / ((x ** 2 - 1) ** 0.5 - x)
print(f00)
f01 = -(x ** 2 - 1) ** 0.5 - x
print(f01)

This works:

import numpy as np

x = np.float128(10 ** 9)
f00 = 1 / ((x ** 2 - 1) ** 0.5 - x)
print(f00)
f01 = np.float128(-(x ** 2 - 1) ** 0.5 - x)
print(f01)
accdias
  • 5,160
  • 3
  • 19
  • 31

1 Answers1

-2

I might be wrong, but as far as I know floats in python are stored in IEEE 754 double64 format. And have only 53 significant bits.

What this means.

When you try to store 10^9 as float you'll get 10^9 because it can be represented with 53 bits But when you try to store 10^18, you need 60 bits to store the number which is greater than the capabilities of float to store whole number, that's why it will use exponenta, and you'll get 1e+18. And by substracting one you still get 1e+18.

That's why you get division by zero.

aramcpp
  • 339
  • 1
  • 8