6

Background

Matlab's built-in eps function [1] can take a numeric value X and return "the positive distance from abs(X) to the next larger in magnitude floating point number of 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

Equivalently, Numpy provides the 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

Question

Is there an equivalent function in Java (and therefore JVM languages like Clojure)?

References

4

1 回答 1

3

你想要java.lang.math.ulp

返回参数的 ulp 的大小。一个值的 ulpdouble是这个浮点值和 double下一个更大的值之间的正距离。请注意,对于非 NaN x, ulp(-x) == ulp(x)

此外,可用于floatsULP的一些背景知识。

于 2013-06-12T15:55:51.017 回答