Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare floats in php

People also ask

How do you compare two floats?

To compare two floating point or double values, we have to consider the precision in to the comparison. For example, if two numbers are 3.1428 and 3.1415, then they are same up to the precision 0.01, but after that, like 0.001 they are not same.

Is float in PHP example?

A float is a number with a decimal point or a number in exponential form. 2.0, 256.4, 10.358, 7.64E+5, 5.56E-5 are all floats. The float data type can commonly store a value up to 1.7976931348623E+308 (platform dependent), and have a maximum precision of 14 digits.

How do you check if a number is int or float in PHP?

The is_float() function checks whether a variable is of type float or not. This function returns true (1) if the variable is of type float, otherwise it returns false.


If you do it like this they should be the same. But note that a characteristic of floating-point values is that calculations which seem to result in the same value do not need to actually be identical. So if $a is a literal .17 and $b arrives there through a calculation it can well be that they are different, albeit both display the same value.

Usually you never compare floating-point values for equality like this, you need to use a smallest acceptable difference:

if (abs(($a-$b)/$b) < 0.00001) {
  echo "same";
}

Something like that.


Read the red warning in the manual first. You must never compare floats for equality. You should use the epsilon technique.

For example:

if (abs($a-$b) < PHP_FLOAT_EPSILON) { … }

where PHP_FLOAT_EPSILON is constant representing a very small number (you have to define it in old versions of PHP before 7.2)


Or try to use bc math functions:

<?php
$a = 0.17;
$b = 1 - 0.83; //0.17

echo "$a == $b (core comp oper): ", var_dump($a==$b);
echo "$a == $b (with bc func)  : ", var_dump( bccomp($a, $b, 3)==0 );

Result:

0.17 == 0.17 (core comp oper): bool(false)
0.17 == 0.17 (with bc func)  : bool(true)

It would be better to use native PHP comparison:

bccomp($a, $b, 3)
// Third parameter - the optional scale parameter
// is used to set the number of digits after the decimal place
// which will be used in the comparison. 

Returns 0 if the two operands are equal, 1 if the left_operand is larger than the right_operand, -1 otherwise.


As said before, be very careful when doing floating point comparisons (whether equal-to, greater-than, or less-than) in PHP. However if you're only ever interested in a few significant digits, you can do something like:

$a = round(0.17, 2);
$b = round(1 - 0.83, 2); //0.17
if($a == $b ){
    echo 'a and b are same';
}
else {
    echo 'a and b are not same';
}

The use of rounding to 2 decimal places (or 3, or 4) will cause the expected result.


If you have floating point values to compare to equality, a simple way to avoid the risk of internal rounding strategy of the OS, language, processor or so on, is to compare the string representation of the values.

You can use any of the following to produce the desired result: https://3v4l.org/rUrEq

String Type Casting

if ( (string) $a === (string) $b) { … }

String Concatenation

if ('' . $a === '' . $b) { … }

strval function

if (strval($a) === strval($b)) { … }

String representations are much less finicky than floats when it comes to checking equality.