Last active
June 9, 2019 17:32
-
-
Save vbarbarosh/d6297f367bd6c98193521898836e9a81 to your computer and use it in GitHub Desktop.
php_math_float_error – Rounding errors in PHP https://codescreens.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# Rounding errors in PHP | |
# docker run --rm -i php:5.6 php < php_math_float_error.php | |
# docker run --rm -i php:7.1 php < php_math_float_error.php | |
# docker run --rm -i php:7.2 php < php_math_float_error.php | |
# docker run --rm -i php:7.3 php < php_math_float_error.php | |
define('FLOAT_ERROR', 0.00001); | |
$a = 4.99998; | |
$b = 4.99999; | |
var_dump(($b - $a) <= FLOAT_ERROR); | |
# false | |
var_dump(($b*1E6 - $a*1E6) <= FLOAT_ERROR*1E6); | |
# true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment