简体   繁体   English

Java双epsilon

[英]Java double epsilon

I'm currently in the need of an epsilon of type double (preferred are constants in java's libraries instead of own implementations/definitions) 我目前需要一个double类型的epsilon (首选是java库中的常量而不是自己的实现/定义)

As far as I can see Double has MIN_VALUE and MAX_VALUE as static members. 据我所知, DoubleMIN_VALUEMAX_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() ? 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的最小值之间的差异,可以表示数据类型。

I'm presuming you mean epsilon in the sense of the error in the value. 我认为你的意思是价值错误意义上的epsilon。 Ie this . 就是这个

If so then in Java it's referred to as ULP (unit in last place). 如果是这样,那么在Java中它被称为ULP(最后一个单位)。 You can find it by using the java.lang.Math package and the Math.ulp() method. 您可以使用java.lang.Math包和Math.ulp()方法找到它。 See javadocs here . 在这里查看javadocs

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. 编辑:根据OP现在在问题中对epsilon的定义,1.0的两倍的ULP是2.220446049250313E-16,表示为double。 (Ie the return value of Math.ulp(1.0) .) (即Math.ulp(1.0)的返回值Math.ulp(1.0) 。)

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 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. 我相信原始问题是由于在C中有一个常量DBL_EPSILON ,在标准头文件float.h定义,它捕获了问题所指的内容。 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. 相同的标准头文件包含常量DBL_MINDBL_MAX定义,它们分别清楚地对应于Java中的Double.MIN_VALUEDouble.MAX_VALUE 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. 因此,很自然地假设Java也应该包含类似Double.EPSILON的定义,其含义与C中的DBL_EPSILON相同。但是,事实并非如此。 Even more strangely, C# does contain 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 . 更奇怪的是,C# 确实包含一个定义double.EPSILON ,但它有不同的含义,即C中由常量DBL_MIN覆盖的DBL_MIN ,在Java中由Double.MIN_VALUE覆盖。 Certainly a situation that can lead to some confusion, as it makes the term EPSILON ambiguous. 当然这种情况可能导致一些混乱,因为它使EPSILON这个词含糊不清。

double: The double data type is a double-precision 64-bit IEEE 754 floating point. double:双精度数据类型是双精度64位IEEE 754浮点数。 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. 其值范围超出了本讨论的范围,但在Java语言规范的浮点类型,格式和值部分中指定。 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. 如上所述,此数据类型不应用于精确值,例如货币。

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: binary64:

  • Base(b)=2 碱(B)= 2
  • precision(p)=53 精度(P)= 53
  • machineEpsion(e) (b^-(p-1))/2=2^-53=1.11e-16 machineEpsion(e)(b ^ - (p-1))/ 2 = 2 ^ -53 = 1.11e-16
  • machineEpsilon(e) b^-(p-1)=2^-52=2.22e-16 machineEpsilon(e)b ^ - (p-1)= 2 ^ -52 = 2.22e-16

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM