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".
Matlab的内置eps函数[1]可以取一个数值X,并返回“从abs(X)到下一个更大的浮点数的正距离”。
>> 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]:
等效地,Numpy提供了间距函数[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)?
在Java中是否有等价的函数(因此JVM语言类似Clojure)?
References
- [1] http://www.mathworks.com/help/matlab/ref/eps.html
- [1]http://www.mathworks.com/help/matlab/ref/eps.html
- [2] Documentation at https://github.com/numpy/numpy/blob/master/numpy/core/code_generators/ufunc_docstrings.py#L2905.
- The actual function implementation in C seems to be at https://github.com/numpy/numpy/blob/master/numpy/core/src/npymath/ieee754.c.src#L323 (the license banner before npy_nextafter gives my question some irony).
- C中实际的功能实现似乎是在https://github.com/numpy/numpy/blob/master/numpy/core/core/npymath/npymath/npymath/npymath/ieee754.c.src #L323 (npy_nextafter之前的许可证横幅给我的问题带来了一些讽刺)。
- [2]在https://github.com/numpy/numpy/blob/master/numpy/core/code_generators/ufunc_docstrings.py L2905文档。C中实际的功能实现似乎是在https://github.com/numpy/numpy/blob/master/numpy/core/core/npymath/npymath/npymath/npymath/ieee754.c.src #L323 (npy_nextafter之前的许可证横幅给我的问题带来了一些讽刺)。
1 个解决方案
#1
1
You want java.lang.math.ulp:
你想要java.lang.math.ulp:
Returns the size of an ulp of the argument. An ulp of a
double
value is the positive distance between this floating-point value and thedouble
value next larger in magnitude. Note that for non-NaN x,ulp(-x) == ulp(x)
返回参数的ulp大小。一个双值的ulp是这个浮点值与下一个较大的值之间的正距离。注意,对于非nan x, ulp(-x) == ulp(x)
Also, available for floats. Some background on ULPs.
同时,用于浮点数。一些背景就ulp进行。
#1
1
You want java.lang.math.ulp:
你想要java.lang.math.ulp:
Returns the size of an ulp of the argument. An ulp of a
double
value is the positive distance between this floating-point value and thedouble
value next larger in magnitude. Note that for non-NaN x,ulp(-x) == ulp(x)
返回参数的ulp大小。一个双值的ulp是这个浮点值与下一个较大的值之间的正距离。注意,对于非nan x, ulp(-x) == ulp(x)
Also, available for floats. Some background on ULPs.
同时,用于浮点数。一些背景就ulp进行。