为什么Visual Studio 2008会告诉我.9-.8999999999999995 = 0.00000000000000055511151231257827? [英] Why does Visual Studio 2008 tell me .9 - .8999999999999995 = 0.00000000000000055511151231257827?

查看:91
本文介绍了为什么Visual Studio 2008会告诉我.9-.8999999999999995 = 0.00000000000000055511151231257827?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Visual Studio 2008即时窗口中键入以下内容时:

When I type this into the Visual Studio 2008 immediate window:

? .9 - .8999999999999995

这给了我答案:

0.00000000000000055511151231257827

文档说双精度数的精度为15到16位,但它给我的结果为32位精度.所有这些额外的精度来自哪里?

The documentation says that a double has 15-16 digits of precision, but it's giving me a result with 32 digits of precision. Where is all that extra precision coming from?

推荐答案

答案中的 只有15到16位数字.所有这些前导零都不算在内.该数字实际上更像是5.5511151231257827× 10 -16 .尾数部分有15到16位数字.指数(-16)用于将小数点移16位,但不会改变总数的位数.

There are only 15-16 digits in the answer. All those leading zeroes don't count. The number is actually more like 5.5511151231257827 × 10-16. The mantissa portion has 15-16 digits in it. The exponent (-16) serves to shift the decimal point over by 16 places, but doesn't change the number of digits in the overall number.

在收到一些评论后,我现在对真正发生的事情感到好奇.我将有问题的号码插入了此 IEEE-754转换器.自由地将最后的"27"舍入为"30",但我认为这不会改变结果.

After getting some comments, I'm curious now about what's really going on. I plugged the number in question into this IEEE-754 Converter. It took the liberty of rounding the last "27" into "30", but I don't think that changes the results.

转换器将数字分解为三个二进制部分:

The converter breaks down the number into its three binary parts:

符号:0(正)
指数:-51
有效位数:1.0100000000000000000000000000000000000000000000000000000000(二进制为1.25 10 )

Sign: 0 (positive)
Exponent: -51
Significand: 1.0100000000000000000000000000000000000000000000000000 (binary for 1.2510)

所以这个数字是1.01 2 × 2 -51 或1.25 10 × 2 -51 .由于仅存储了三个有效的二进制数字,因此这暗示着Lars可能在某物上.它们不能是随机噪声",因为每次转换数字时它们都是相同的.

So this number is 1.012 × 2-51, or 1.2510 × 2-51. Since there are only three significant binary digits being stored, that would suggest that Lars may be onto something. They can't be "random noise" since they are the same each time the number is converted.

数据表明,唯一存储的数字为"5".前导零来自指数,其余看似随机的数字来自计算2 -51 .

The data suggests that the only stored digit is "5". The leading zeros come from the exponent and the rest of the seemingly-random digits are from computing 2-51.

这篇关于为什么Visual Studio 2008会告诉我.9-.8999999999999995 = 0.00000000000000055511151231257827?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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