Very simple problem with adding PHP

I think I looked at this for too long. Why does this code print “no”, it should print “yes”, right? I tried this on PHP 5.3 and PHP 5.2, and both print "no."

<?php $total = 14.05; $var1 = 0; $var2 = 0.11; $var3 = 13.94; if(($var1 + $var2 + $var3) == $total) { echo 'yes'; } else { echo 'no'; } ?> 
+4
source share
10 answers

See Comparison of floating point numbers .

This does not work because floating point numbers are not displayed exactly. For the equality operator error, a small rounding error is sufficient.

+6
source

Like everyone else, do not compare floats directly. Just do if (abs ($ float1- $ float2) <0.0000001)

or similair

In your case

 <?php $total = 14.05; $var1 = 0; $var2 = 0.11; $var3 = 13.94; if (abs(($var1 + $var2 + $var3)-$total)<0.000001) { echo 'yes'; } else { echo 'no'; } ?> 
+2
source

A very lazy way around this is:

 $var4 = $var1 + $var2 + $var3; $var4 = number_format($var4, 2); $total = number_format($total, 2); if($var4 == $total) { echo 'yes'; } else { echo 'no'; } 

http://php.net/number_format

+1
source

If you need to do exact floating point math, you should use a library like GMP or BCMath

+1
source

As mentioned in other answers, be careful when comparing floats. Try instead:

 echo ((int)(($var1+$var2+$var3)-$total)==0)?"yes":"no"; 
+1
source

Because ... you shouldn't compare floats for equality ... it's a matter of rounding ... something like 14.05 is actually presented as 14.0499999 ... all you could do would be to compare with a certain tolerance, for example. t = 0.01, then a = b will be considered true if a + t> = b> = at.

0
source

I suspect this is due to floating point precision .

Floating point numbers cannot be stored with full precision, and sometimes the results are not what you expect. To compare floating point numbers, you must allow some tolerance. If you are sure that each number has exactly two decimal places, you can multiply by 100 to make them integer values ​​and compare the total.

0
source

This is caused by floating point precision. A floating point number represents the actual value, but only with a certain accuracy. Instead, try the following:

 <? $total = 14.05; $var1 = 0; $var2 = 0.11; $var3 = 13.94; if((($var1 + $var2 + $var3) > ($total - 0.0000001)) && (($var1 + $var2 + $var3) < ($total + 0.0000001))) { echo 'yes'; } else { echo 'no'; } ?> 
0
source

This is the main mathematical problem with floating point.

Computers work with binary math. There are no problems for integer values. But for decimal places, he uses a technique called floating point math. The problem is that although floating points are good, they do not bind directly to decimal values; even relatively simple decimal values ​​may not be possible to represent exactly in binary format.

If you always work with two decimal places - for example, for currencies - it is often better to understand your values ​​as integers (for example, store cents, not dollars, or pence, not pounds), so that all your math is done using integers, and the results will be accurate. You only need a decimal value for the display, which you can do at the moment you need it.

0
source

Floating point values ​​have limited precision. Therefore, the value may not have the same string representation after any processing. It also includes writing a floating point value in a script and directly print it without any math operations.

If you want to know more about the “floats” and the IEEE 754, read this:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

you can use num_formattig or string casting before compilation

0
source

Source: https://habr.com/ru/post/1347116/


All Articles