[php-src] Issue #15746: Weird Result from Decimal Calculation
| From: | zelosnhp | Date: | Wed, 04 Sep 2024 14:04:56 +0000 |
| Subject: | [php-src] Issue #15746: Weird Result from Decimal Calculation | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-247696@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/15746
Author: zelosnhp
Found out that the result was different when I execute below simple calculation.
$total_percentage = 33.4 + 33.3 + 33.3;
if ($total_percentage < 100) {
echo "Total less than " . $total_percentage;
} else {
if ($total_percentage > 100) {
echo "Total more than " . $total_percentage;
} else {
echo "Total is " . $total_percentage;
}
}
The result is weird: Total less than 100
But if I simply change the sequence to:
$total_percentage = 33.3 + 33.3 + 33.4;
Or value of first 2 to:
$total_percentage = 33.5 + 33.2 + 33.3;
Then both result is correct as expected: Total is 100
And if want to remain the sequence and i add with number_format:
if (number_format($total_percentage, 1) < 100) {
The result is also correct: Total is 100
This happen to all version of PHP from 4.0.6 to 8.3.10 and only if the value is 33.4 + 33.3 + 33.3;
What could be the possibility cause of this?