Reputation: 51
Laravel 8 , php 8
I saw a very strange thing in Laravel and actually in PHP that I can't understand why this is happening in PHP.
I have two arrays that are almost identical and have only one very small difference. And the result of the operation of adding all the values of the items of each of them is 100. But PHP considers the sum of the values to be 100 for one of the arrays but not for the other. How is this possible?
Of course, I know a little about PHP_FLOAT_EPSILON l. But it seems that this variable has nothing to do with the problem that arose for me.
$c = [
23.3,
33.4,
43.3,
];
$d = [
43.3,
23.4,
33.3,
];
$ccc = array_sum($c);
$ddd = array_sum($d);
dump($ccc); // output: 100.0
dump($ddd); // output: 100.0
if ($ccc != 100) {
dump('$ccc is not equal to 100');
} else {
dump('$ccc is equal to 100'); // This output is displayed.
}
if ($ccc != 100.0) {
dump('$ccc is not equal to 100.0');
} else {
dump('$ccc is equal to 100.0'); // This output is displayed.
}
if ($ddd != 100) {
dump('$ddd is not equal to 100'); // This output is displayed.
} else {
dump('$ddd is equal to 100');
}
if ($ddd != 100.0) {
dump('$ddd is not equal to 100.0'); // This output is displayed.
} else {
dump('$ddd is equal to 100.0');
}
Upvotes: 0
Views: 53