News for hackers. Stuff that matters.
Re:1/0 = 0 (2018) by eesmith on Monday December 02, 2024 @05:35AM (#42294832)
In floating point a = b * (a / b) is not always a true statement.

  >>> import random
  >>> random.random()
  0.4667867537470992
  >>> n = 0
  >>> for i in range(1_000_000):
  ...   a = random.random()
  ...   b = random.random()
  ...   if (a == b * (a / b)):
  ...     n += 1
  ...
  >>> n
  886304
For example:

  >>> a, b = 0.7959754927336106, 0.7345016612407793
  >>> a == b * (a / b)
  False
  >>> a
  0.7959754927336106
  >>> b * (a / b)
  0.7959754927336105
This is off by one ulp ("unit in the last place").

And of course the division of two finite floating point numbers may be infinite:

  >>> a, b = 2, 1.5e-323
  >>> a
  2
  >>> b
  1.5e-323
  >>> b * (a / b)
  inf
  >>> a/b
  inf
As a minor technical point, x/0 can be -INF if sgn(x) < 0, and NaN if x is a NaN.

There are no comments on this comment yet.