Guess: console.writeline (5000.88 + 5000 == 10000.88); Result is TRUE or FALSE?

xiaoxiao2021-03-06  82

By Ben

Deadly errors appear in a project practice: console.writeline (5000.88 5000 == 10000.88); result

False

Analysis: Use "==" to compare the types of two Double should be equal, and return true value is completely uncertain. The principle of calculating the computer's calculation of floating point numbers is to ensure that the necessary accuracy is correct, for example: Double A = 0.9 0.2; then only 1 of this accuracy can only be correct, the computer calculates the result of this additive A Value may be 1.100000001, may also be 1.100000002 may also be 1.1, that is, it does not guarantee that it is correct after the computer calculation is correct, so it is not feasible to use == directly.

Compare whether the number of two doubles should be equal to whether the absolute value is less than a certain value, this value is generally less than two degrees

For example: console.writeline (Math.abs (5000 5000.88-10000.88) <0.001);

Detailed explanation:

http://www.yoda.arachsys.com/csharp/floatingpoint.html

It is now telling this question in .NET. Everyone can refer to it.

转载请注明原文地址:https://www.9cbs.com/read-108491.html

New Post(0)