phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

Drop comparison /w `PHP_FLOAT_EPSILON`

Open mvorisek opened this issue 3 years ago • 2 comments

fixes https://github.com/sebastianbergmann/phpunit/issues/4966

The removed code is residual from https://github.com/sebastianbergmann/phpunit/commit/136fae39306c8e6cda3fb5ec8fcc0ff8cff8cf4e and even a difference by PHP_FLOAT_EPSILON should be reported as a failure.

The PHP_FLOAT_EPSILON constant equals to 2.220446049250313E-16 (see 3v4l link in a post below) and is usable only when the abs(a - b) is done on normalized values, but when done so, the comparison is useless or satisfied because of the normalization rounding error solely. On the other side, imagine comparing very small numbers like 1e-100 and 1e-200. The difference between these two normalized values is astronomically large, but currently the phpunit handles such numbers as same!

mvorisek avatar May 21 '22 21:05 mvorisek

marking as draft, the one failing test should fail - https://3v4l.org/rIJBO,

/**
 * Cast float to string with lossless precision.
 */
final public static function castFloatToString(float $value): string
{
    $precisionBackup = ini_get('precision');
    ini_set('precision', '-1');
    try {
        return (string) $value;
    } finally {
        ini_set('precision', $precisionBackup);
    }
}

must be put everywhere where float to string is casted (for print, for weak comparison, ...)

mvorisek avatar May 21 '22 21:05 mvorisek

PR is done

mvorisek avatar Sep 14 '22 13:09 mvorisek