micmania1 Posted October 6, 2009 Share Posted October 6, 2009 -54162.76 + 54162.76 = -1.45519152284E-11 and number_format(-1.45519152284E-11, 2) = -0.00 Why is PHP doing this? Link to comment https://forums.phpfreaks.com/topic/176662-solved-problem-with-negative-numbers/ Share on other sites More sharing options...
kickstart Posted October 6, 2009 Share Posted October 6, 2009 Hi Think what is happening is that php is using binary arithmatic, and a number ending .76 cannot be represented in binary. Suspect that a +ve and -ve version of the number land up represented with a very slightly different binary number. All the best Keith Link to comment https://forums.phpfreaks.com/topic/176662-solved-problem-with-negative-numbers/#findComment-931369 Share on other sites More sharing options...
micmania1 Posted October 6, 2009 Author Share Posted October 6, 2009 Thanks for the reply. Is there a way of solving the problem? Link to comment https://forums.phpfreaks.com/topic/176662-solved-problem-with-negative-numbers/#findComment-931372 Share on other sites More sharing options...
kickstart Posted October 6, 2009 Share Posted October 6, 2009 Hi Suspect not really avoidable short of writing custom functions for such calculations. It is a pretty common issue with computer systems that do not support decimal arithmatic (old mainframe langauges such as PL/1 and Cobol supported decimal formats to avoid issues like this). If you really wanted to then you could process the number as an array of single digit decimal numbers and do all the addition / subtraction / multiplication / division a digit at a time. All the best Keith Link to comment https://forums.phpfreaks.com/topic/176662-solved-problem-with-negative-numbers/#findComment-931381 Share on other sites More sharing options...
micmania1 Posted October 6, 2009 Author Share Posted October 6, 2009 function decimalAddition() { $args = func_get_args(); $number = array(); $decimals = array(); foreach ($args as $v) { $exp = explode('.', $v); $number[] = $exp[0]; if ($exp[0] < 0) { $decimals[] = 0 - $exp[1]; } else { $decimals[] = $exp[1]; } } if (!empty($number)) { $number = array_sum($number); } else { $number = 0; } if (!empty($decimals)) { $decimals = array_sum($decimals) / 100; } else { $decimals = 0; } return $number + $decimals; } Problem Solved. Thanks for your time. Link to comment https://forums.phpfreaks.com/topic/176662-solved-problem-with-negative-numbers/#findComment-931398 Share on other sites More sharing options...
micmania1 Posted October 6, 2009 Author Share Posted October 6, 2009 Don't use above code - its got bugs. function decimalAddition() { $args = func_get_args(); $number = array(); $decimals = array(); foreach ($args as $v) { $round = number_format($v, 0); $number[] = $round; $decimals[] = ($v - $round); } if (!empty($number)) { $number = array_sum($number); } else { $number = 0; } if (!empty($decimals)) { $decimals = array_sum($decimals); } else { $decimals = 0; } return $number + $decimals; } Link to comment https://forums.phpfreaks.com/topic/176662-solved-problem-with-negative-numbers/#findComment-931485 Share on other sites More sharing options...
Mark Baker Posted October 6, 2009 Share Posted October 6, 2009 use bcmath if its available Link to comment https://forums.phpfreaks.com/topic/176662-solved-problem-with-negative-numbers/#findComment-931532 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.