Java: Double machine epsilon is not the smallest x such that 1+x != 1?

别说谁变了你拦得住时间么 提交于 2019-12-03 22:35:11

using the definition of it being the smallest representable double value x such that 1.0 + x != 1.0, just as in C/C++

This has never been the definition, not in Java and not in C and not in C++.

The definition is that the machine epsilon is the distance between one and the smallest float/double larger than one.

Your “definition” is wrong by a factor of nearly 2.

Also, the absence of strictfp only allows a larger exponent range and should not have any impact on the empirical measurement of epsilon, since that is computed from 1.0 and its successor, each of which and the difference of which can be represented with the standard exponent range.

I'm not sure your experimental method / theory is sound. The documentation for the Math class states:

For a given floating-point format, an ulp of a specific real number value is the distance between the two floating-point values bracketing that numerical value

The documentation for the ulp method says:

An ulp of a double value is the positive distance between this floating-point value and the double value next larger in magnitude

So, if you want the smallest eps value such that 1.0 + eps != 1.0, your eps really should generally be less than Math.ulp(1.0), since at least for any value greater than Math.ulp(1.0) / 2, the result will be rounded up.

I think the smallest such value will be given by Math.nextAfter(eps/2, 1.0).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!