Detailed explanation of double precision multiplication accuracy problem in .NET
In C#, the result of the expression double i = 10 * 0.69;
is that i
is assigned a value of 6.8999999999999999999999999999999999995, rather than the expected 6.9. This raises the question: Is there an error in double precision multiplication in .NET?
Binary representation and floating point operation
]To understand this behavior, you must have a deep understanding of the subtleties of binary representations and floating-point operations. Floating point formats approximately represent real numbers as finite binary values, which can lead to precision limitations.
The seemingly simple 0.69 cannot be represented accurately in binary. It requires an infinite loop of binary numbers. The binary representation stored in the double
data type is an approximate value of 0.69.
Constants optimized and stored by compiler
]In the provided code snippet, the compiler optimizes the expression by performing multiplication and storing the result as a constant in the executable. It does this because these values are known at compile time. The stored value is a floating point approximation of 6.9, which is slightly smaller than the actual value.
Solutions and Alternative Data Types
]To solve this problem and get more precise results, the decimal
data type can be used, which is designed to represent non-repetitive decimal values with higher accuracy. The expression decimal i = 10m * 0.69m;
will produce a result of 6.9m.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3