简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM