PHP:awk的减法给指数值 [英] PHP : The awk subtraction giving exponential values
问题描述
我收到awk的结果,当我减去两个值,错误的是我得到2.7755575615629E-17指数值而不是0任何事情我错过申请,请建议。这与某些情况下发生的事情只有像0.66,0.67,0.33,
我使用下面给出的code的原型,
$ _SESSION ['X'] = 1;
$ _SESSION ['X'] = $ _SESSION ['X'] - 0.83;
回波($ _SESSION ['X'] - 0.17);
呼应'< BR>';
但在扭转值这一切都罚款0
$ _SESSION ['X'] = 1;
$ _SESSION ['X'] = $ _SESSION ['X'] - 0.17;
回波($ _SESSION ['X'] - 0.83);
呼应'< BR>';
它是在计算机语言中一个共同的问题 - 浮点值没有被重新presented准确。另请参见 http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems 。如果您有想正好与,你可以在PHP中使用 bcmath
函数计算出小数的特定量:
$ _ SESSION ['X'] = 1;
$ _SESSION [X'] = bcsub($ _ SESSION ['X'],0.83,10);
回声bcsub($ _ SESSION ['X'],0.17,10);
呼应'< BR>';
否则,你可以简单地使用你的计算并添加圆($结果,$ numberOfDecimalPlaces)
为您计算结果。
I am getting awk result when I am subtracting two values, the error is I am getting exponent value 2.7755575615629E-17 instead of 0. Anything I am missing to apply, please suggest. These is happening with some cases only like 0.66, 0.67, 0.33,
The prototype of the code I am using is given below,
$_SESSION['x'] = 1;
$_SESSION['x'] = $_SESSION['x'] - 0.83;
echo ( $_SESSION['x']- 0.17) ;
echo '<br>';
But on reversing the values It all fine with 0
$_SESSION['x'] = 1;
$_SESSION['x'] = $_SESSION['x'] - 0.17;
echo ( $_SESSION['x']- 0.83) ;
echo '<br>';
It is a common problem in computer languages - float values aren't represented exactly. See also http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems. If you have a particular amount of decimal places you want to exactly calculate with, you can use the bcmath
functions in PHP:
$_SESSION['x'] = 1;
$_SESSION['x'] = bcsub($_SESSION['x'], 0.83, 10);
echo bcsub($_SESSION['x'], 0.17, 10);
echo '<br>';
Otherwise you can simply use your calculation and add an round($result, $numberOfDecimalPlaces)
to you calculated result.
这篇关于PHP:awk的减法给指数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!