?(1.0-0.9-0.1)
-0.000000000000000027755575615628914
?((双)1.0-(双)0.9-(双)0.1)
-0.000000000000000027755575615628914
?((双)1.0-(双)0.9-(双)0.1).GetType()
{名称=双全名=System.Double}
?((双)1.0-(双)0.9-(双)0.1)的ToString()
-2,77555756156289E-17
如何在 Double.ToString()
显示更多的字符(32)的双的precision(15-16)?
我希望 MyObject.ToString()
重presents 只是 为MyObject 的而不是为MyObject + SomeTrashFromComputer
为什么
?0.1
0.1
?0.2-0.1
0.1
?0.1-0.1
0.0
但
?1.0-0.9-0.1
-0.000000000000000027755575615628914
为什么
?1.0-0.1-0.9
0.0
但
?1.0-0.9-0.1
-0.000000000000000027755575615628914
解决方案
如何一Double.ToString()显示更多的字符(32)双的precision(15-16)?
这是不是显示32,则显示17,前导零不计。 浮动的点意味着它可以从价值变动分别跟踪变化的幅度。
我希望MyObject.ToString()重新presents只是为MyObject
,它确实可能会有细微的差别,由于浮点数的机制,但真正的数字再由字符串precisely psented $ P $。
不为MyObject + SomeTrashFromComputer
有没有垃圾,没有浮点误差。它存在于小数也写下 1/3
为十进制数完全吻合。你不能,它涉及到一个重复的小数位。双的存储在基地2个,所以即使 0.1
创建一个重复的十进制。
另外请注意,你得到两个不同的重presentations因为你正在访问两种不同的显示方式。 的ToString
有特定的语义,而你的调试窗口可能有不同的人。此外仰望科学记数法,如果你想知道什么电子
表示。
?(1.0-0.9-0.1)
-0.000000000000000027755575615628914
?((double)1.0-(double)0.9-(double)0.1)
-0.000000000000000027755575615628914
?((double)1.0-(double)0.9-(double)0.1).GetType()
{Name = "Double" FullName = "System.Double"}
?((double)1.0-(double)0.9-(double)0.1).ToString()
"-2,77555756156289E-17"
How a Double.ToString()
displays more chars(32) that double's precision(15-16)?
I expect that MyObject.ToString()
represents just MyObject and not MyObject+SomeTrashFromComputer
Why
?0.1
0.1
?0.2-0.1
0.1
?0.1-0.1
0.0
BUT
?1.0-0.9-0.1
-0.000000000000000027755575615628914
WHY
?1.0-0.1-0.9
0.0
BUT
?1.0-0.9-0.1
-0.000000000000000027755575615628914
解决方案
How a Double.ToString() displays more chars(32) that double's precision(15-16)?
It isn't displaying 32, it is displaying 17, leading zeros don't count. Floating point means it can keep track of changes in magnitude separately from changes in value.
I expect that MyObject.ToString() represents just MyObject
It does, there may be a slight difference due to the mechanics of floating point numbers, but the true number is represented by the string precisely.
not MyObject+SomeTrashFromComputer
There is no trash, there is floating point inaccuracy. It exists in decimal too, write down 1/3
as a decimal number exactly. You can't, it involves a repeating decimal place. Double's are stored in base 2, so even 0.1
creates a repeating "decimal".
Also note that you are getting two different representations because you are calling two different display methods. ToString
has specific semantics, while your debugging window probably has different ones. Also look up scientific notation if you want to know what the E
means.