非常小的Double值的加法(Java) [英] Addition of very small Double values (Java)

查看:295
本文介绍了非常小的Double值的加法(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,为什么我简单地将一些double值相加会导致Java产生以下结果:

I was wondering, why my simple addition of some double values leads to the following results in Java:

double a = 1 + 1E-10; // 1.0000000001 (as expected)
double b = 1 + 1E-15; // 1.000000000000001 (as expected)
double c = 1 + 1E-20; // 1.0 (why?)

我认为我至少可以添加一个Double.MIN_VALUE的值,似乎是4.9E-324.

I thought I can at least add a value of Double.MIN_VALUE, which seems to be 4.9E-324.

我在这里做错了什么?

What am I doing wrong here?

推荐答案

@ Turing85指出,double具有11位指数和53位尾数.

As @Turing85 points out, a double has 11 bits of exponent and 53 bits of mantissa.

我们在这里计算的是1.0 + 1E-20.为了表示该数字(更精确地大于1.0),我们需要至少21位精度的十进制数字或71位.这比double在尾数中提供的精度更高.

What we are calculating here is 1.0 + 1E-20. To represent that number (more precisely than 1.0) we need at least 21 decimal digits of precision or 71 bits. That is more precision than a double provides in the mantissa.

因此,与1.0 + 1E-20最接近的可表示的 double编号是.... 1.0.这就是您得到的结果.

So, the nearest representable double number to 1.0 + 1E-20 is .... 1.0. And that's the result you get.

欢迎来到浮点运算的神秘世界.

Welcome to mysterious world of floating point arithmetic.

这篇关于非常小的Double值的加法(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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