Possible Duplicate:
Strange addition of numeric strings in PHP
$r = 1.0 - 0.8 - 0.2;
var_dump($r);
I get float -5.5511151231258E-17.
The same result i get in C++ and C#.
MySQL i get result 0.0 via query:
SELECT 1.0 - 0.8 - 0.2
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Let it be clear:
0.8has finite representation in decimal because it has value8/10¹.But in base 2, it has value: 1/2 + 1/4 + 0/8 + 0/16 + 1/32 + 1/64 + 0/128 + … and it has no end, like one third has no end in decimal representation (
0.333…) but would be0.1in base 3.Your computer stores floating point values in binary representation. So, there is a loss of precision when you add, subtract, multiply, etc. numbers.
C++, C and C# are compiled languages, and floats are stored on typically 4 or 8 bytes. You cannot store 0.8 in a float.
MySQL might tell you 0 because it truncates the results of the calculation.
If you want a library that performs calculations without losses of precision in any base, look up GMPlib.