Java 双 epsilon
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25180950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java double epsilon
提问by nonsensation
I'm currently in the need of an epsilonof type double
(preferred are constants in java's libraries instead of own implementations/definitions)
我目前需要一个epsilon类型double
(首选是 java 库中的常量而不是自己的实现/定义)
As far as I can see Double
has MIN_VALUE
and MAX_VALUE
as static members.
据我所知,Double
有MIN_VALUE
和MAX_VALUE
作为静态成员。
Why there is no EPSILON
?
为什么没有EPSILON
?
What would a epsilon<double>
be?
会epsilon<double>
是什么?
Are there any differences to a std::numeric_limits< double >::epsilon()
?
与 a 有什么区别std::numeric_limits< double >::epsilon()
吗?
Epsilon: The difference between 1 and the smallest value greater than 1 that is representable for the data type.
Epsilon:1 与数据类型可表示的大于 1 的最小值之间的差值。
回答by Smalltown2k
I'm presuming you mean epsilon in the sense of the error in the value. I.e this.
我假设你的意思是 epsilon 在值的错误意义上。即这个。
If so then in Java it's referred to as ULP (unit in last place). You can find it by using the java.lang.Math
package and the Math.ulp()
method. See javadocs here.
如果是这样,那么在 Java 中它被称为 ULP(最后一个单位)。您可以通过使用java.lang.Math
包和Math.ulp()
方法找到它。请参阅此处的 javadoc。
The value isn't stored as a static member because it will be different depending on the double you are concerned with.
该值不会存储为静态成员,因为它会根据您关注的双精度而有所不同。
EDIT:By the OP's definition of epsilon now in the question, the ULP of a double of value 1.0 is 2.220446049250313E-16 expressed as a double. (I.e. the return value of Math.ulp(1.0)
.)
编辑:根据问题中现在 OP 对 epsilon 的定义,值为 1.0 的双精度值的 ULP 是 2.220446049250313E-16,表示为双精度值。(即 . 的返回值Math.ulp(1.0)
)
回答by Maria Sakharova
Without using Math package:
不使用 Math 包:
Double.longBitsToDouble(971l << 52)
That's 2^-52 (971 = 1023(double exponent bias) - 52, shift by 52 is because mantissa is stored on the first 52 bits).
那是 2^-52(971 = 1023(双指数偏差)- 52,移位 52 是因为尾数存储在前 52 位上)。
It's a little quicker than Math.ulp(1.0);
它比 Math.ulp(1.0) 快一点;
Also, if you need this to compare double values, there's a really helpful article: https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
此外,如果您需要它来比较双精度值,这里有一篇非常有用的文章:https: //randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
回答by Christian Borgelt
By the edit of the question, explaining what is meant by EPSILON
, the question is now clear, but it might be good to point out the following:
通过编辑问题,解释 是什么意思EPSILON
,问题现在很清楚了,但最好指出以下几点:
I believe that the original question was triggered by the fact that in C there is a constant DBL_EPSILON
, defined in the standard header file float.h
, which captures what the question refers to. The same standard header file contains definitions of constants DBL_MIN
and DBL_MAX
, which clearly correspond to Double.MIN_VALUE
and Double.MAX_VALUE
, respectively, in Java. Therefore it would be natural to assume that Java, by analogy, should also contain a definition of something like Double.EPSILON
with the same meaning as DBL_EPSILON
in C. Strangely, however, it does not. Even more strangely, C# doescontain a definition double.EPSILON
, but it has a different meaning, namely the one that is covered in C by the constant DBL_MIN
and in Java by Double.MIN_VALUE
. Certainly a situation that can lead to some confusion, as it makes the term EPSILON
ambiguous.
我相信最初的问题是由以下事实触发的:在 C 中有一个常量DBL_EPSILON
,定义在标准头文件中float.h
,它捕获了问题所指的内容。相同的标准头文件包含常数的定义DBL_MIN
和DBL_MAX
,这清楚地对应于Double.MIN_VALUE
与Double.MAX_VALUE
分别在Java中。因此,通过类推,很自然地假设 Java 也应该包含Double.EPSILON
与DBL_EPSILON
C 中含义相同的类似定义。然而,奇怪的是,它没有。更奇怪的是,C#确实包含一个定义double.EPSILON
,但它有不同的含义,即 C 中的常量DBL_MIN
和 Java 中的定义Double.MIN_VALUE
. 当然,这种情况会导致一些混淆,因为它使术语EPSILON
含糊不清。
回答by Martin Frank
double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.
double:double 数据类型是双精度 64 位 IEEE 754 浮点数。它的值范围超出了本讨论的范围,但在 Java 语言规范的浮点类型、格式和值部分中进行了指定。对于十进制值,此数据类型通常是默认选择。如上所述,这种数据类型不应用于精确值,例如货币。
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
looking up at IEEE 754 you'll find the precision of epsion...
抬头看看 IEEE 754,你会发现 epsion 的精度......
http://en.wikipedia.org/wiki/IEEE_floating_point
http://en.wikipedia.org/wiki/IEEE_floating_point
binary64:
二进制64:
- Base(b)=2
- precision(p)=53
- machineEpsion(e) (b^-(p-1))/2=2^-53=1.11e-16
- machineEpsilon(e) b^-(p-1)=2^-52=2.22e-16
- 基数(b)=2
- 精度(p)=53
- machineEpsion(e) (b^-(p-1))/2=2^-53=1.11e-16
- machineEpsilon(e) b^-(p-1)=2^-52=2.22e-16