Why there are orders of magnitude in the evaluation of torch.sin() and numpy.sin() at integer multiples of Pi?

Viewed 301

Why are trigonometric functions of Pytorch and Numpy when evaluated at integer multiples of Pi result in such huge discrepancy in the orders of magnitude?

>>> torch.sin(torch.ones(1)*2*np.pi)
tensor([1.7485e-07])
>>> np.sin(np.ones(1)*2*np.pi)
array([-2.4492936e-16])
2 Answers

Expanding on the answer you got, but using plain Python:

>>> import math
>>> math.sin(2 * math.pi)
-2.4492935982947064e-16

Python uses 64-bit floats, and that's a close match to the numpy result.

IEEE-754 64-bit floats have 53 bits of precision, but 32-bit floats only 24. So let's round the input back to 24 significant bits and see what happens:

>>> x = 2 * math.pi
>>> m, e = math.frexp(x)
>>> m *= 2**24
>>> m = round(m)
>>> y = math.ldexp(m, e - 24)
>>> math.sin(y)
1.7484556000744883e-07

And that's a close match to the torch result.

Torch defaults to 32-bit floats, while NumPy defaults to 64-bit. The rounding error you're getting in Torch is around the expected scale for 32-bit floats. Specify a different dtype if you want a different dtype.

Related