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)