Jump to content

CAN'T PHP ADD 3 STUPID SIMPLE NUMBERS!???!???


ChenXiu

Recommended Posts

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.

Link to comment
Share on other sites

@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.
 

Link to comment
Share on other sites

"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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.