加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java相当于Matlab的`eps`或Numpy / Python的`spacing`函数(浮点

发布时间:2020-12-14 19:35:07 所属栏目:Java 来源:网络整理
导读:背景 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 同样,Numpy提供了间距函数[2]: I
背景

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

同样,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

Java中是否存在等效函数(因此像Clojure这样的JVM语言)?

参考

> [1] http://www.mathworks.com/help/matlab/ref/eps.html
> [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/src/npymath/ieee754.c.src#L323(npy_nextafter之前的许可证横幅给我的问题一些讽刺).

解决方法

你想要 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 the
double value next larger in magnitude. Note that for non-NaN x,
ulp(-x) == ulp(x)

此外,可用于floats.一些background on ULPs.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读