Java is equivalent to Matlab’s’ EPS’ or numpy / Python’s’ spacing ‘function (floating point relative precision)

background

Matlab's built-in EPS function [1] can take a value x and return "the positive distance from ABS (x) to the next larger floating-point number with the same precision"

>> eps(1)
ans =
   2.2204e-16
>> eps(single(1))
ans =
   1.1921e-07
>> eps(1e6)
ans =
   1.1642e-10
>> eps(single(1e6))
ans =
       0.0625

Similarly, numpy provides a spacing function [2]:

In [18]: import numpy as np

In [19]: np.spacing(1)
Out[19]: 2.2204460492503131e-16

In [20]: np.spacing(np.single(1))
Out[20]: 1.1920929e-07

In [21]: np.spacing(1e6)
Out[21]: 1.1641532182693481e-10

In [22]: np.spacing(np.single(1e6))
Out[22]: 0.0625

topic

Is there an equivalent function in Java (so a JVM language like clojure)?

reference resources

> [1] http://www.mathworks.com/help/matlab/ref/eps.html >[2] document on https://github.com/numpy/numpy/blob/master/numpy/core/code_generators/ufunc_docstrings.py#L2905.

>The actual function implementation in C seems to be https://github.com/numpy/numpy/blob/master/numpy/core/src/npymath/ieee754.c.src#L323 (npy_the license banner before nextafter satirized my question.)

Solution

You want Java lang.math. ulp:

In addition, it can be used for floats Some background on ULPS

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>