qaisjp
qaisjp

Reputation: 811

Why is np.nextafter(0, 1) not equivalent to the float64 epsilon value?

This is very weird!

np.nextafter returns the smallest number after 0. Shouldn't this be equal to the float64 epsilon?

In [25]: np.nextafter(0, 1).dtype == np.finfo(np.float64).eps.dtype
Out[25]: True

In [26]: np.nextafter(0, 1) < np.finfo(np.float64).eps
Out[26]: True

Upvotes: 0

Views: 385

Answers (2)

Eric Postpischil
Eric Postpischil

Reputation: 222244

A floating-point number is comprised of:

  • A sign: + or −.
  • Some number n of digits in some base b, including a decimal or radix point: d0.d−1d−2d−3d−(n−1).
  • The base b raised to some power e: be.

The sign is applied to the digits, and the result is multiplied by be, so the entire number is ±d0.d−1d−2d−3d−(n−1)be. In computing, b is often 2, meaning each digit is a bit. In the common IEEE-754 basic 64-bit binary floating-point format, n is 53.

The so-called machine epsilon is the place value of d−(n−1) when the exponent e is 0. In other words, the machine epsilon is +0.000…1 • b0, which equals b−(n−1). For the common 64-bit format, this is 2−52.

In any floating-point format, there is a least value that e is allowed to have (since it must fit into a field of bits reserved for it). For the common 64-bit format, this is −1022. So the smallest positive value a floating-point number in this format can have is +0.000…1 • 2−1022. That equals 2−52 • 2−1022 = 2−1074.

In other words, the machine epsilon tells you the step size of floating-point numbers near 1. The step size depends on the exponent, so it is greater for greater numbers and smaller for smaller numbers. Near zero, the step size is 2−1074.

Upvotes: 1

Warren Weckesser
Warren Weckesser

Reputation: 114781

The float64 epsilon is computed relative to 1.0. It is not the smallest positive number representable in 64 bit floating point.

To find it using np.nextafter(), use np.nextafter(1, 2) - 1:

In [215]: np.nextafter(1, 2) - 1
Out[215]: 2.220446049250313e-16

In [216]: np.finfo(np.float64).eps
Out[216]: 2.220446049250313e-16

Upvotes: 4

Related Questions