Jump to content

php maths issues


edzillion

Recommended Posts

Hi

I am having some issues with multiplying doubles. I presume php isn't the best for maths, but I want to use it to keep things simple.

Basically:

If I multiply one double by another like so:

 

function format_price($price){
$doubresult = $price * 31.1034768;
return $doubresult;
}

 

format_price(15.7389); = 466.552152

format_price(15.7103); = 466.552152

 

I presume this is because of the way that php rounds down when multiplying numbers with a large amount of decimal places.

Any way of doing this accurately?

 

Thanks

Ed

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

it works better in php than my calc (on linux):

 

489.53451100752 (php)  or (489.534511008 calc)

488.64495157104 (php)  or (488.644951571 calc)

 

But you can typecast like this...

function format_price($price){
//$doubresult = doubleval($price * 31.1034768);
$doubresult = (double)$price * (double)31.1034768;
return $doubresult;
}
print format_price(15.7389)."<br>";
print format_price(15.7103)."<br>";

Link to comment
Share on other sites

Just to further note

 

Everything is in PHP is a string, kinda like in ASP everything is a DIM until you do something that is TYPE Specific, like math functions or type casting.

 

$IamString = '42';
$IamString *= 24.5; // Now I'm an float (there are no doubles just the names for historical reasons in PHP)
$IamString .= 'two for tuesday';  // Now I'm a string again

 

So just remember PHP is type-less language unless explicitly defined.

 

Link to comment
Share on other sites

Sorry for the offtopic-ness.

 

So if you do:

 


$num = 5;
echo var_dump($num);

 

Why does it say int?  Everything isn't a string, conversions just happen as needed.  For example, if you look into the incrementing code of the php, ($i++, $i += x), you'll see that at some point it checks if the new number is greater than the signed integer max (2^31-1) (I don't remember exactly how it checks, but I would imagine it would check if i is 1 less than the max since checking after it rolled over would cause issues lol), and if it is, it will convert it to a float.

 

In the same sense:

 

$i = 5;
$i2 = 10;
//both numbers are stored as integers
$new = $i*i2;
echo "{$i} * {$i2} = {$new}";
//or
echo $i . ' * ' . $i2 . ' = ' . $new;
//The integers are temporarily converted to strings so that they can be concatenated
//same sense:
$si = '5';
$si2 = '10';
$snew = $si*$si2; //$si and $si2 are converted to a numeric data type, in this case an integer so that they can be multiplied
echo "{$si} * {$si2} = {$snew}";
//in this case, si and si2 are already strings, but snew has to be temporarily converted to a string.

 

Therefore, I think it is incorrect to say PHP is a type-less language, but rather it should be said that PHP transitions between types as needed without explicit coder instruction.

 

Then again, I could be entirely wrong, and PHP does store everything in a string; that's just how I've gathered that it works.

 

 

 

On topic:

 

PHP has a certain setting for how far it keeps numerical accuracy (I think it defaults to 14), so any decimal places after that are just chopped off I think.

 

Example:

 

C:\Users\Corbin>php -r "echo (double)5.00000000000005;"
5
C:\Users\Corbin>php -r "echo (double)5.0000000000005;"
5.0000000000005

 

(It's worth noting that without the five, it handles the first one fine...)

 

So, once you get past a certain number of decimal places, it's very difficult to do mathematical operations accurately with the built-in php operators.

 

"I presume php isn't the best for maths"

 

In general, no computer language is good for very minute numbers.  C++, C (The 'engine' behind PHP is in C if I remember correctly), Java, and I'm sure other language would run into the exact same problem.

 

Every datatype has it's limitations math wise.  A signed integer's happens to be 2^31-1, while the double's max is gigantic (http://en.wikipedia.org/wiki/Double_precision) (The decimal max is dependant on certain factors, but in PHP it seems to always be the configuration directive).

 

(P.S. for number_format(15.7389), Windows calc (calc.exe) gives me 489.53451100752)

Link to comment
Share on other sites

Sorry for the offtopic-ness.

So if you do:

$num = 5;

echo var_dump($num);

Why does it say int?  Everything isn't a string, conversions just happen as needed.  For example, if you look into the incrementing code of the php, ($i++, $i += x), you'll see that at some point it checks if the new number is greater than the signed integer max (2^31-1) (I don't remember exactly how it checks, but I would imagine it would check if i is 1 less than the max since checking after it rolled over would cause issues lol), and if it is, it will convert it to a float.

 

There is a difference between these two statements

 

$num = 5;

 

and

 

$num = '5';

 

Also it goes more as to how PHP stores the information that is really what I was trying to illustrate.  The fact is $num is typeless you are declaring type in the above to statements. 

Link to comment
Share on other sites

Sorry for the offtopic-ness.

So if you do:

$num = 5;

echo var_dump($num);

Why does it say int?  Everything isn't a string, conversions just happen as needed.  For example, if you look into the incrementing code of the php, ($i++, $i += x), you'll see that at some point it checks if the new number is greater than the signed integer max (2^31-1) (I don't remember exactly how it checks, but I would imagine it would check if i is 1 less than the max since checking after it rolled over would cause issues lol), and if it is, it will convert it to a float.

 

There is a difference between these two statements

 

$num = 5;

 

and

 

$num = '5';

 

Also it goes more as to how PHP stores the information that is really what I was trying to illustrate.  The fact is $num is typeless you are declaring type in the above to statements. 

 

No, $num is not typless unless you delare the type.  In the world of computers, there's no such thing as typeless.

 

"There is a difference between these two statements"

 

That's what I was saying!

 

"Everything is in PHP is a string, kinda like in ASP everything is a DIM until you do something that is TYPE Specific, like math functions or type casting."

 

That's what I was trying to correct, because that is flat out wrong.

 

"So just remember PHP is type-less language unless explicitly defined."

 

Also flat out wrong.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.