I was doing just a simple increment of floating number using PHP Laravel as API, like code below;
Code:
public function testArrIncrement(){
$arr["test"]["words"] = "The quick brown fox jumps over a lazy dog";
$arr["test"]["number"] = 0;
for($i=0; $i<11; $i++) $arr["test"]["number"] += 0.01;
return response()->json($arr, 200);
}
Result:
{
"test": {
"words": "The quick brown fox jumps over a lazy dog",
"number": 0.10999999999999999
}
}
the strange thing is, why the value of $arr["test"]["number"] is not 0.11, but instead 0.10999999999999999 ?
But if I tried the same code just using plain single file PHP without any framework everything is normal the value of $arr["test"]["number"] is indeed 0.11.
Code:
$arr["test"]["words"] = "The quick brown fox jumps over a lazy dog";
$arr["test"]["number"] = 0;
for($i=0; $i<11; $i++) $arr["test"]["number"] += 0.01;
var_dump($arr);
Result:
array(1) { ["test"]=> array(2) { ["words"]=> string(41) "The quick brown fox jumps over a lazy dog" ["number"]=> float(0.11) } }
Plese help why this happen??