After seeing many PHP questions about comparing the equality of floats where the answer is to simply choose an arbitrary value for Epsilon and then do if( abs($a-$b) < 0.000001 )
.
The trouble is that Epsilon is typically much smaller than the values people tend to choose [2.22e-16 on my machine] and is actually quite simple to calculate:
$macheps = (float) 1.0;
do {
$macheps /= (float) 2.0;
} while( (float) (1.0 + ($macheps/2.0)) != 1.0 );
printf("Epsilon: %0.25f\n", $macheps);
C++ has std::numeric_limits<double>::epsilon()
, Python has sys.float_info.epsilon
, so why does PHP leave it up in the air?
PHP_FLOAT_EPSILON
was added in PHP 7.2: Smallest representable positive number x, so that x + 1.0 != 1.0