0

I have an array:

$test =array('49'=> '-0','51'=> '-0','50'=> '0','53'=> '-1.69','55'=> '0','57'=> '-2','59'=> '-6','60'=> '-12','65'=> '0','66'=> '0','67'=> '21.69','69'=> '0','70'=> '0','71'=> '0',); 

echo "\n".'===== First Method ========';
echo "\n\n".print_r($test);
echo "\n array_sum: ".array_sum($test);
echo "\n\n".'===== Second Method ========';

$total = 0;foreach($test as $value) $total += $value;
echo "\n foreach:".$total."\n";

the result is

gd@gd:~/Desktop$ php test.php

===== First Method ========Array
(
    [49] => -0
    [51] => -0
    [50] => 0
    [53] => -1.69
    [55] => 0
    [57] => -2
    [59] => -6
    [60] => -12
    [65] => 0
    [66] => 0
    [67] => 21.69
    [69] => 0
    [70] => 0
    [71] => 0
)


1
 array_sum: 3.5527136788005E-15

===== Second Method ========
 foreach:3.5527136788005E-15

it is wrong, the result should be 0, not 3.5527136788E-15, how to fix it ?

2
  • 2
    You should reformat that code with linebreaks so that it is easily readable. Also, first thought, you're summing strings which are potentially going to be subject to casting mistakes. This feels a lot like a floating point precision error. Commented Aug 23, 2013 at 23:58
  • so, what kind of shells should Men use ? Commented Aug 24, 2013 at 4:59

4 Answers 4

2

This is just your standard floating point arithmetic precision error.

php -r "echo -1.69 + -2 + -6 + -12 +21.69;"
3.5527136788005E-15%

You can fix it by using ints rather than floats. For example, if you always expect 2 digits of precision, multiply all your numbers by 100, round them off to ints, sum them, and divide by 100.

php -r "echo (-169 + -200 -1200 +2169 + -600) / 100;"
0%                                               
Sign up to request clarification or add additional context in comments.

Comments

1

You are doing array_sum with strings. Remove the quotes on the values, or covert them to integers before using array_sum - I imagine it is converting the strings to integers incorrectly - only on my phone so can't check specifics.

Hope this helps.

Comments

0

Why not just give the values as floating point numbers rather than enclosing them in quotes. That would basically make it a string summation and strange result is expected. I think in before PHP version 4.something it used to convert the string to numbers. It might especially be problem with decimal numbers.

2 Comments

integers? really? is 1.69 an integer?
PHP automatically casts strings to numbers when you attempt to do mathematic operations on them - he'd get exactly the same result if he didn't use quotes.
0

This is just an example of floating point imprecision. It's impossible to represent .69 exactly in binary (much like it's impossible to represent 1/3 exactly in decimal).

If you need exact numbers, you can look into using the bcmath php extension.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.