ChenXiu Posted July 28, 2023 Share Posted July 28, 2023 Now I know why real programmers snicker a little bit when I mention PHP. $numbers = array( '9780393872439', '9780134130422', '30.50' ); foreach($numbers as $num) { $total += $num; } echo $total //WRONG! WRONG! WRONG! I'm frankly disappointed. Quote Link to comment https://forums.phpfreaks.com/topic/317130-cant-php-add-3-stupid-simple-numbers/ Share on other sites More sharing options...
Barand Posted July 28, 2023 Share Posted July 28, 2023 $total = 0; // add this line to define $total before trying to increment $numbers = array( '9780393872439', '9780134130422', '30.50' ); foreach($numbers as $num) { $total += $num; } echo $total ; And why are you defining the numbers as string values? Quote Link to comment https://forums.phpfreaks.com/topic/317130-cant-php-add-3-stupid-simple-numbers/#findComment-1610792 Share on other sites More sharing options...
ginerjm Posted July 28, 2023 Share Posted July 28, 2023 Without the setting of $total that Barand provided you, if you had bothered to turn on error checking with: error_reporting(E_ALL); ini_set('display_errors', '1'); You would have seen this message from your starting code: Notice: Undefined variable: total in /home/albany/public_html/homejg/test.php on line 27 Which would have given you a clue to solving the problem you were having. You should turn on error checking at the top of every script your run during development. Quote Link to comment https://forums.phpfreaks.com/topic/317130-cant-php-add-3-stupid-simple-numbers/#findComment-1610793 Share on other sites More sharing options...
requinix Posted July 28, 2023 Share Posted July 28, 2023 1 hour ago, ChenXiu said: Now I know why real programmers snicker a little bit when I mention PHP. Tell me you've never used any other programming languages without telling me that you've never used any other programming languages 🤣 https://docs.python.org/3/tutorial/floatingpoint.htmlhttps://www.codemag.com/article/1811041/JavaScript-Corner-Math-and-the-Pitfalls-of-Floating-Point-Numbershttps://stackoverflow.com/questions/1661273/floating-point-arithmetic-not-producing-exact-resultshttps://www.cprogramming.com/tutorial/floating_point/understanding_floating_point_representation.htmlhttps://stackoverflow.com/questions/753948/why-is-floating-point-arithmetic-in-c-sharp-imprecise and finally, https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html Quote Link to comment https://forums.phpfreaks.com/topic/317130-cant-php-add-3-stupid-simple-numbers/#findComment-1610794 Share on other sites More sharing options...
ChenXiu Posted July 28, 2023 Author Share Posted July 28, 2023 @barand, same result whether or not they are strings: $total = 9780393872439 + 9780134130422 + 30.50; echo $total; // WRONG TOTAL AGAIN @ginerjm, I have error reporting on, there are no errors. If you have errors ($total is undefined), then you forgot to initialize "$total = 0;" like I did. @requinix, PHP is the easiest programming language to learn, the others are too complex for me to understand. Just like 13 digit numbers are too complex for PHP to understand. C'est la vie. The correct answer is:When using numbers over 12 digits long, PHP may exhibit 'unexpected behavior' because of the way it handles floating points (euphemistically called "rounding errors"), therefore extensions like BCmath may help. Â Quote Link to comment https://forums.phpfreaks.com/topic/317130-cant-php-add-3-stupid-simple-numbers/#findComment-1610795 Share on other sites More sharing options...
jodunno Posted August 2, 2023 Share Posted August 2, 2023 "CAN'T PHP ADD 3 STUPID SIMPLE NUMBERS!???!???" ChenXiu, seriously? you have never heard of precision before? and the output of total is not "wrong, wrong, wrong", it is a product of the mathemaical process known as rounded up. PHP, as mentioned by the always helpful Requinix, is not the only programming language that is problematic with precision. The reasons lie beyond the scope of this forum but an additional tip is that even microprocessors have limitations. Maybe you have money for a supercomputer? And fractions are not 'simple' numbers by definition and they have no brains to be 'stupid' or smart for that matter. Just poking at you a bit (making fun but not being disrespectful). anyway, bcadd will help but now you may have to add additional code to determine fractions (30.50) from integers within the array, unless you intend to maintain the same format (id est: add two integers, then add a fraction to that total.) <?php $total = 0; $numbers = array( '9780393872439', '9780134130422', '30.50' ); $integers = strval($numbers[0] + $numbers[1]); $total = bcadd($integers, $numbers[2], 2); echo $total; ?> maybe you can make use of bcadd and be satisfied. But keep in mind that you will have mathematical problems with most programming languages and software restraints do not help either. Quote Link to comment https://forums.phpfreaks.com/topic/317130-cant-php-add-3-stupid-simple-numbers/#findComment-1610860 Share on other sites More sharing options...
Barand Posted August 2, 2023 Share Posted August 2, 2023 Just use bcadd() with a foreach loop $total = 0; $numbers = array( '9780393872439', '9780134130422', '30.50' ); foreach($numbers as $num) { $total = bcadd($total, $num, 2); } echo $total ; // 19560528002891.50 Quote Link to comment https://forums.phpfreaks.com/topic/317130-cant-php-add-3-stupid-simple-numbers/#findComment-1610861 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.