C ++中的double类型数字 [英] double type digits in C++

查看:328
本文介绍了C ++中的double类型数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IEE754(64位)浮点数应该正确表示15位有效数字,尽管内部表示有17位数据。有没有办法强制第16和第17位数字为零?



Ref:
http://msdn.microsoft.com/en-us/library/system.double(VS.80)。 aspx



请记住,浮点数只能接近十进制数,而浮点数的精度决定了这个数的近似值十进制数字。默认情况下,Double值包含十五个精度的十进制数字,但内部最多保留17位数字。浮点数的精度有几个结果:



示例:
d1 = 97842111437.390091

d2 = 97842111437.390076

d1和d2小数点后16位和小数点17位的数字不应该是重要的。寻找方法迫使他们归零。即
d1 = 97842111437.390000
d2 = 97842111437.390000

解决方案

反例:将两个最接近的浮点数转换为有理数

  1.11111111111118 

(有十进制数字15)是

  1.1111111111111799942818834097124636173248291015625 
1.1111111111111802163264883347437717020511627197265625

换句话说,没有浮点数以 1.1111111111111800


The IEE754 (64 bits) floating point is supposed to correctly represent 15 significant digit although the internal representation has 17 ditigs. Is there a way to force the 16th and 17th digits to zero ??

Ref: http://msdn.microsoft.com/en-us/library/system.double(VS.80).aspx : . .

Remember that a floating-point number can only approximate a decimal number, and that the precision of a floating-point number determines how accurately that number approximates a decimal number. By default, a Double value contains 15 decimal digits of precision, although a maximum of 17 digits is maintained internally. The precision of a floating-point number has several consequences: . .

Example nos: d1 = 97842111437.390091
d2 = 97842111437.390076
d1 and d2 differ in 16th and 17th decimal places that are not supposed to be significant. Looking for ways to force them to zero. ie d1 = 97842111437.390000 d2 = 97842111437.390000

解决方案

No. Counter-example: the two closest floating-point numbers to a rational

1.11111111111118

(which has 15 decimal digits) are

1.1111111111111799942818834097124636173248291015625
1.1111111111111802163264883347437717020511627197265625

In other words, there is not floating-point number that starts with 1.1111111111111800.

这篇关于C ++中的double类型数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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