问题描述
我正在开发一个在linux环境下运行的C ++应用程序。我需要存储一个大值与6 deciaml地方。我用了一个双。但是在赋值后,double变量不包含确切的值。已舍入。
I'm developing a C++ application which runs on linux environment. I need to store a large value with 6 deciaml places. I used a double for that. But after assigning, the double variable doesn't contains the exact value. It has rounded off.
例如:
double dValue = 79447461534242.913072; //Assignement of value
但是之后,当我看到dValue中的值时,它就像79447461534242.906
But after that , when I see the value in dValue it is something like 79447461534242.906
有人可以告诉我为什么会发生这种情况,并建议我使用正确的数据类型,它可以保存精确的值,而不会丢失精度。
Can some one let me know why this is happening and suggest me the correct data type which can hold the exact value with out losing the precision.
推荐答案
C ++使用IEEE精确到一定程度的设计的浮点类型。如果您需要任意精确度,请查看例如或任何其他任意精确度库。
C++ uses IEEE floating point types which are only precise to a certain degree by design. If you need arbitary precision take a look at eg GNU MP Bignum or any other arbitary precision library.
这篇关于双C ++的精度损失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!