从一个大于Double.MaxValue的值的字符串中解析一个double [英] Parsing a double from a string which holds a value greater than Double.MaxValue

查看:181
本文介绍了从一个大于Double.MaxValue的值的字符串中解析一个double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下java代码:

Consider the following java code:

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

对于上述值 1.7976931348623157E308 有意义,一个得到正确的输出。

For the mentioned value of 1.7976931348623157E308 everything makes sense and one gets the correct output.

现在,如果尝试解析 1.7976931348623158E308 E 递增)您仍然得到最大值打印到控制台!

只有在尝试解析 1.7976931348623159E308 (再次是最后一位数字/ code>。

相应的负值相同的行为。

Now, if one tries to parse 1.7976931348623158E308 (last digit before E incremented) you still get the maximum value printed into the console!
Only after trying to parse 1.7976931348623159E308 (again the last digit got incremented) and greater one gets Infinity.
Same behaviour for the corresponding negative values.

为什么 ... 8E308 解析为 ... 7E308 而不是 Infinity

推荐答案

parseDouble文档的SE 7版本是指valueOf文档,它说: p>

The SE 7 version of the parseDouble documentation refers to the valueOf documentation which says:


请注意,round-to-nearest规则也意味着溢出和下溢行为;如果s的精确值足够大(大于或等于(MAX_VALUE + ulp(MAX_VALUE)/ 2)),则舍入到双精度会导致无穷大,如果s的精确值在数量级小(少超过或等于MIN_VALUE / 2),舍入到浮动将导致零。

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.

这与舍入的语句一致键入double是按照IEEE 754浮点算术的通常的最近到最近的规则。

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.MAX_VALUE是该规则中与某些数字相比最接近的数字。

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.

要确认这是正常的舍入行为,请考虑以下程序:

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);
      }
    }

它输出:

1.9958403095347198E292
1.7976931348623157E308
1.7976931348623157E308
Infinity

向Double.MAX_VALUE添加甚至略小于一半的ulp不会更改它。加入半个ulp溢出到无穷大。

Adding something even slightly less than half a ulp to Double.MAX_VALUE does not change it. Adding half a ulp overflows to infinity.

这篇关于从一个大于Double.MaxValue的值的字符串中解析一个double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆