不精确的浮法除法 [英] Inexact float division

查看:106
本文介绍了不精确的浮法除法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看到这个,我正在练习一些c编程:

I were practicing some c programming when I saw this:

#include <stdio.h>

int main (void){
printf("result %.50lf",(double)10.0/10000000.0);
return 0;
}

其结果是0.00000099999999999999995474811182588628625868561393872

And the result was 0.00000099999999999999995474811182588625868561393872

有人可以解释一下为什么会这样吗?

Can somebody explain me why happens this?

推荐答案

double的内部表示是二进制浮点编码,无法精确表示所有(或实际上大多数)十进制小数,出于同样的原因,例如1/3不能精确地用十进制表示,10.0/10000000.0不能精确地用二进制表示.

The internal representation of double is a binary floating-point encoding that cannot precisely represent all (or indeed most) decimal fractions, for the same reason for example 1/3 cannot be exactly represented in decimal, 10.0/10000000.0 cannot be exactly represented in binary.

此外,64位双精度二进制浮点的精度约为15个十进制有效数字,因此,尝试输出50个十进制小数总是以无关紧要的数字结尾.在两倍精度范围内的实际结果是0.000000999999999999999.

Moreover 64-bit double precision binary floating point has a precision of around 15 decimal significant figures so attempting to output 50 decimal places is always going to end up in irrelevant and meaningless digits. Your actual result within the precision of a double is 0.000000999999999999999.

另请参见:每个程序员应了解的有关浮点算法的信息

这篇关于不精确的浮法除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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