|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2017-05-03 10:58 UTC] requinix@php.net
-Status: Open
+Status: Not a bug
[2017-05-03 10:58 UTC] requinix@php.net
[2017-05-03 11:00 UTC] kelunik@php.net
-Summary: 123
+Summary: Floating point precision issue
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 06 09:00:02 2026 UTC |
Description: ------------ I have array with numbers and i do simple math in loop and in results i see -6.9388939039072E-17 instead 0 Test script: --------------- <?php $h = 0.05; $x = [-1,-0.5,0,0.5,1]; for($i=0;$i<4;$i++) { for($j=$x[$i]+$h;$j<$x[$i+1];$j+=$h) { $x2[$i][] = $j; } } echo "<pre>".print_r($x2,1)."</pre>"; Expected result: ---------------- Array ( [0] => Array ( [0] => -0.95 [1] => -0.9 [2] => -0.85 [3] => -0.8 [4] => -0.75 [5] => -0.7 [6] => -0.65 [7] => -0.6 [8] => -0.55 ) [1] => Array ( [0] => -0.45 [1] => -0.4 [2] => -0.35 [3] => -0.3 [4] => -0.25 [5] => -0.2 [6] => -0.15 [7] => -0.1 [8] => -0.05 [9] => 0 ) [2] => Array ( [0] => 0.05 [1] => 0.1 [2] => 0.15 [3] => 0.2 [4] => 0.25 [5] => 0.3 [6] => 0.35 [7] => 0.4 [8] => 0.45 [9] => 0.5 ) [3] => Array ( [0] => 0.55 [1] => 0.6 [2] => 0.65 [3] => 0.7 [4] => 0.75 [5] => 0.8 [6] => 0.85 [7] => 0.9 [8] => 0.95 ) ) Actual result: -------------- Array ( [0] => Array ( [0] => -0.95 [1] => -0.9 [2] => -0.85 [3] => -0.8 [4] => -0.75 [5] => -0.7 [6] => -0.65 [7] => -0.6 [8] => -0.55 ) [1] => Array ( [0] => -0.45 [1] => -0.4 [2] => -0.35 [3] => -0.3 [4] => -0.25 [5] => -0.2 [6] => -0.15 [7] => -0.1 [8] => -0.05 [9] => -6.9388939039072E-17 ) [2] => Array ( [0] => 0.05 [1] => 0.1 [2] => 0.15 [3] => 0.2 [4] => 0.25 [5] => 0.3 [6] => 0.35 [7] => 0.4 [8] => 0.45 [9] => 0.5 ) [3] => Array ( [0] => 0.55 [1] => 0.6 [2] => 0.65 [3] => 0.7 [4] => 0.75 [5] => 0.8 [6] => 0.85 [7] => 0.9 [8] => 0.95 ) )