为什么不以全精度打印浮点值? [英] Why are floating point values not printed with full precision?
问题描述
如果我有一个浮点变量,其值是3.1415927(二进制表示形式0b01000000010010010000111111111011011)并打印出来,则得到3.141593
If I have a floating point variable with the value 3.1415927 (binary representation 0b01000000010010010000111111011011) and print it I get 3.141593
如果我再次将3.141593转换为二进制表示形式,则会得到0b010000000100100100001111111011100
And if I convert 3.141593 to binary representation again I get 0b01000000010010010000111111011100
您看到了不同之处:
0b01000000010010010000111111011 011 = 3.1415927
0b01000000010010010000111111011011 = 3.1415927
0b01000000010010010000111111011 100 = 3.141593
0b01000000010010010000111111011100 = 3.141593
所以我实际上失去了一点精度.
So I actually lost a little bit of precision.
如何强制C#Console.WriteLine()输出浮点值而又不丢失精度?
How can I force C# Console.WriteLine() to output the floating point value with no lost precision?
我使用网页https://www.h-schmidt.net/FloatConverter/IEEE754.html进行了检查.
I used the web page https://www.h-schmidt.net/FloatConverter/IEEE754.html to check this.
推荐答案
如果使用十进制或双精度,则将保持精度.就个人而言,我更喜欢十进制...请阅读我的博客文章,了解在对双变量进行数学运算时可能遇到的问题(这就是为什么我更喜欢十进制): http://geek-goddess-bonnie.blogspot.com/2013/09/double-vs-decimal.html
You will keep your precision if you use either decimal or double. Personally, I prefer decimal ... please read my blog post about problems you might encounter doing math on double variables (which is why I prefer decimal): http://geek-goddess-bonnie.blogspot.com/2013/09/double-vs-decimal.html
这篇关于为什么不以全精度打印浮点值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!