Atnas
Atnas

Reputation: 613

Equivalent of the Matlab function `eps` in Python/Numpy

In Matlab eps has the following feature:

d = eps(x), where x has data type single or double, returns the positive distance from abs(x) to the next larger floating-point number of the same precision as x.

What is the equivalent way of computing this in Python or Numpy?

When searching for the answer, I found references to np.finfo(np.float64).eps, which is only the equivalent of eps('double') in Matlab.

Upvotes: 3

Views: 3664

Answers (1)

João Areias
João Areias

Reputation: 1448

You might be searching for numpy spacing. Here an example:

import numpy as np

for i in [1e-2, 1, 1e5, 1e10]:
    print(f'Spacing for {i:.4e} :\t {np.spacing(i):.4e}')

And here the output:

Spacing for 1.0000e-02 :         1.7347e-18
Spacing for 1.0000e+00 :         2.2204e-16
Spacing for 1.0000e+05 :         1.4552e-11
Spacing for 1.0000e+10 :         1.9073e-06

Upvotes: 2

Related Questions