((171.36 / 1.19) == 144) false?

Possible duplicate:
PHP math accuracy

Here is a sample code in PHP

echo (171.36/1.19) //[PHP] result: 144 //[JavaScript] result: 144.00000000000003 //[Manual] result : 144 $var1 = 144; $converted = 171.36/1.19 //Variable 1 is less than Converted? echo (($var1 < $converted)?"Yes":"No") //result: Yes //Variable 1 is equal to Converted? echo (($var1 == $converted)?"Yes":"No") //result: No echo (($converted == 144)?"Yes":"No") //--> NO echo (("144.00" == 144)?"Yes":"No") //--> YES 

Could you give me a direct explanation / answer and tell me that PHP is not buggy.

+6
math floating-point php
source share
5 answers

This is the result of floating point calculations inherent in a certain number of errors. Arbitrary real numbers cannot be exactly stored in the floating point format used by most languages ​​(including PHP). You will see similar results in many other languages.

http://php.net/manual/en/language.types.float.php (read the big pink box)

+16
source share

You see that the result is close enough to 144, that the value is displayed when converting it to a string, but it's not exactly 144, so you get "NO" on the penultimate line.

This is the same issue with floating point numbers that people run over and over again.

Neither 171.36 nor 1.19 can be represented exactly in binary floating-point types. Therefore, PHP uses a very close approximation to them. When you do arithmetic, the result will be as accurate as possible, given the limitations of the associated data types and source data (i.e., not exactly what you expected).

Bottom line: Do not compare floating point values ​​for equality directly, except in special circumstances. It is usually best to test them within a certain tolerance (for example, this value is between 144-0.00001 and 144 + 0.00001).

+19
source share

From the floating point guide :

Why aren't my numbers, like 0.1 + 0.2, adding 0.3 to a good round, and instead I get a weird result like 0.30000000000000004?

Because computers internally use a format (binary floating point) that cannot exactly represent a number, such as 0.1, 0.2, or 0.3.

When the code is compiled or interpreted, your "0.1" is already rounded to the nearest number in this format, which leads to a small rounding error even before the calculation takes place.

Basically, when you write the literal 171.36 into your code and the code is interpreted, the number that will be used by the computer is not really 171.36, so it is not surprising that when using int, the calculation.

+5
source share

Not too vicious, the suggestion to read floating-point questions is a good suggestion. Wikipedia article is not bad .

+4
source share

PHP is not buggy. It displays 144 only because it rounds to 12 significant digits when converting a floating-point number to a string for printing. A.

If you print a number up to 17 decimal places, you will see that the result is really 144.00000000000003, the same as the Javascript result.

(Other answers explained why the result is not entirely accurate; I will not repeat it here.)

See http://www.ideone.com/u0yB8 for sample code.

+1
source share

All Articles