这个简单的“双重”计算有什么问题? [英] Whats wrong with this simple 'double' calculation?

查看:149
本文介绍了这个简单的“双重”计算有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java中这个简单的'double'计算有什么问题?

Whats wrong with this simple 'double' calculation in java?

我知道一些十进制数字不能用float / double二进制格式正确表示,但是变量d3,java能够存储和显示2.64没有问题。

I know some decimal numbers can not be represented in float / double binary formats properly, but with the variable d3, java is able to store and display 2.64 with no problems.

double d1 = 4.64;
double d2 = 2.0;
double d3 = 2.64;
double d4 = d1 - d2;

System.out.println("d1      : " + d1);
System.out.println("d2      : " + d2);
System.out.println("d3      : " + d3);
System.out.println("d4      : " + d4);
System.out.println("d1 - d2 : " + (d1 - d2));

答案,

d1      : 4.64
d2      : 2.0
d3      : 2.64
d4      : 2.6399999999999997
d1 - d2 : 2.6399999999999997


推荐答案

问题



在二进制2.64中是10.10100011110101110000101000111101重复出现换句话说,二进制不能完全表示,因此错误。 Java对d3非常友好,但只要涉及实际计算,它就必须依靠实际表示。

The problem

In binary 2.64 is 10.10100011110101110000101000111101 recurring, in other words not exactly representable in binary, hence the small error. Java is being kind to you with d3 but as soon as actual calculations are involved it has to fall back on the real representation.

二进制计算器

更多:

2.64= 10.10100011110101110000101000111101
4.64=100.1010001111010111000010100011110 

现在,即使.64在两种情况下都是相同的,它仍然保持不同的精度,因为4 = 100比使用双重有效数字更多2 = 10, so 当你说4.64-2.0和2.64时.64在两种情况下用不同的舍入误差表示,这个丢失的信息无法恢复到最终答案。

Now, even though the .64 is the same in both cases, it is held to different precisions because the 4=100 uses up more of the double significant figures than 2=10, so when you say 4.64-2.0 and 2.64 the .64 is represented with a different rounding error in both cases, this lost information cannot be recovered for the final answer.

N.B。我这里没有使用数字的有效数字,无论二进制计算器会产生什么,但无论有效数字的数量如何,效果都是相同的

N.B. I'm not using the double number of significant figures here, just whatever the binary calculator would produce, however the effect is the same whatever the number of significant figures

永远不要假设双值是精确的(尽管它们的不准确性是微观的,只是因为某些数字不能用二进制精确表示)。

Never assume double values are exact (although their inaccuracies are microscopic and caused only because certain numbers can't be exactly expressed in binary).


虽然你应该总是期望双精度数在最后几位小数时会出现小错误,但将二进制表示视为坏或者更糟糕的是十进制表示错误。

While you should always expect that doubles will have small errors for the last few decimal places it would be wrong to think of binary representations as "bad" or worse that decimal.

我们都习惯于某些数字(例如1/3)不完全可以用十进制表示,我们接受这样的数字最终会变成0.333333333333而不是真值(没有无限的空间,我不能写下来;在这种情况下,二进制数不能准确表达。 1/10是一个不能用二进制精确表示的数字;这只是因为我们习惯于十进制

We are all used to certain numbers (like 1/3 for example) not being exactly representable in decimal and we accept that such a number will end up as 0.333333333333 rather than the true value (which I cannot write down without infinite space); it is within that context that binary numbers cannot be exactly expressed. 1/10 is such a number that cannot be exactly expressed in binary; this suprises us only because we are used to decimal

这篇关于这个简单的“双重”计算有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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