简体   繁体   English

从包含大于Double.MaxValue的值的字符串解析double

[英]Parsing a double from a string which holds a value greater than Double.MaxValue

Consider the following java code: 考虑以下java代码:

String toParse = "1.7976931348623157E308"; //max value of a double in java        
double parsed = Double.parseDouble(toParse);
System.out.println(parsed);

For the mentioned value of 1.7976931348623157E308 everything makes sense and one gets the correct output. 对于上面提到的值1.7976931348623157E308一切都有意义,一个得到正确的输出。

Now, if one tries to parse 1.7976931348623158E308 (last digit before E incremented) you still get the maximum value printed into the console! 现在,如果有人试图解析1.7976931348623158E308E递增之前的最后一位数字),您仍然可以获得打印到控制台的最大值!
Only after trying to parse 1.7976931348623159E308 (again the last digit got incremented) and greater one gets Infinity . 只有在尝试解析1.7976931348623159E308 (再次最后一位数增加)后,才能获得Infinity
Same behaviour for the corresponding negative values. 相应负值的行为相同。

Why is ... 8E308 parsed to ... 7E308 and not Infinity ? 为什么... 8E308解析为... 7E308而不是Infinity

The SE 7 version of the parseDouble documentation refers to the valueOf documentation which says: serseDouble文档的SE 7版本引用了valueOf文档,其中说:

Note that the round-to-nearest rule also implies overflow and underflow behaviour; 请注意,舍入到最近的规则也意味着溢出和下溢行为; if the exact value of s is large enough in magnitude (greater than or equal to (MAX_VALUE + ulp(MAX_VALUE)/2), rounding to double will result in an infinity and if the exact value of s is small enough in magnitude (less than or equal to MIN_VALUE/2), rounding to float will result in a zero. 如果s的精确值在幅度上足够大(大于或等于(MAX_VALUE + ulp(MAX_VALUE)/ 2),则舍入为double将导致无穷大,并且如果s的精确值在幅度上足够小(更少)等于或等于MIN_VALUE / 2),舍入到浮点将导致零。

This is consistent with the statement that the rounding to type double is by the usual round-to-nearest rule of IEEE 754 floating-point arithmetic. 这与对double类型的舍入是通过IEEE 754浮点运算的通常舍入到最接近的规则的说法是一致的。

You have to imagine the conversion being done by first calculating the nearest floating point number ignoring the exponent limitation, and then checking whether the exponent fits. 您必须通过首先计算忽略指数限制的最近浮点数,然后检查指数是否适合来设想转换。 Double.MAX_VALUE is the closest number under that rule to some numbers that are strictly greater than it. Double.MAX_VALUE是该规则下最接近某些严格大于它的数字的数字。

To confirm this is normal rounding behavior, consider the following program: 要确认这是正常的舍入行为,请考虑以下程序:

    public class Test {
      public static void main(String[] args) {
        double ulp = Math.ulp(Double.MAX_VALUE);
        System.out.println(ulp);
        System.out.println(Double.MAX_VALUE);
        System.out.println(Double.MAX_VALUE+ulp/2.0000000001);
        System.out.println(Double.MAX_VALUE+ulp/2);
      }
    }

It outputs: 它输出:

1.9958403095347198E292
1.7976931348623157E308
1.7976931348623157E308
Infinity

Adding something even slightly less than half a ulp to Double.MAX_VALUE does not change it. 向Double.MAX_VALUE添加甚至略低于半个ulp的东西不会改变它。 Adding half a ulp overflows to infinity. 添加半个ulp溢出到无穷大。

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

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