float: Comparing floats

<?php
$a = 1.23456789;
$b = 1.23456780;
$epsilon = 0.00001;

if(abs($a-$b) < $epsilon) {
    echo "true";
}
?>

Testing floating point values for equality is problematic, due to the way that they are represented internally.

To test floating point values for equality, an upper bound on the relative error due to rounding is used. This value is known as the machine epsilon, or unit roundoff, and is the smallest acceptable difference in calculations.

float: Comparing floats — Structure map

Clickable & Draggable!

float: Comparing floats — Related pages: