0

Why the code's result is that? -3.0531133177192E-16

I think the result should be zero.

        $a = floatval('-0.92');
        $b = floatval('-0.55');
        $c = floatval('1.38');
        $d = floatval('0');
        $e = floatval('0.09');
        $temp=$a+$b+$c+$d+$e;
        var_dump($temp);
        die();

I'm a php fresher, I'm really confused by this result, So any answer will be appreciated.

Dan
  • 51
  • 1
  • 6
  • 2
    This question is really language agnostic and the behavior is typical of floating point arithmetic. A quick google will reveal plenty of articles on both SO and other sites that cover how this happens. – Daniel May 15 '14 at 01:53
  • 2
    http://en.wikipedia.org/wiki/Float_(computing)#Accuracy_problems – CBroe May 15 '14 at 01:53
  • You can do. Some languages have exact-precision types, but PHP, unfortunately, does not have. for more info http://in3.php.net/manual/en/function.bcadd.php $temp= bcadd(bcadd(bcadd(bcadd($a, $b, 2),$c,2),$d,2),$e,2); echo $temp; output: 0.00 – user3470953 May 15 '14 at 02:35

0 Answers0