在下面的代码中,索引5的参数必须为0。
但是echo($a[5])
的值为1.2325951644078E-32
。
该循环为所有其他参数输出正确的值,但第5个索引除外(根据我的需要,该索引必须为0)。
有人可以告诉我为什么会这样吗?
for($x=-2;$x<2.1;$x+=0.4){
$a[] = $x*$x;
}
echo($a[5]); //this is not printing 0 why?
输出为:-
Array
(
[0] => 4
[1] => 2.56
[2] => 1.44
[3] => 0.64
[4] => 0.16
[5] => 1.2325951644078E-32
[6] => 0.16
[7] => 0.64
[8] => 1.44
[9] => 2.56
[10] => 4
)
答案 0 :(得分:1)
它应该为零。 像这样修改您的代码,然后尝试。.
$a[] = number_format($x*$x,2);
答案 1 :(得分:0)
但是您忘了在数学之前转换为数字格式。
for($x=-2;$x<2.1;$x+=0.4){
$a[] = number_format($x)*number_format($x);
}
echo($a[5]); // 0
希望有帮助。