Re: [PHP] Help. Floats turning into really small numbers? x.xxxxxxxxxxxxxxxxxxxxxxE-xx- Narrowed it down!
What you see as round numbers in base 10, are not so in binary. Numbers such as .5, .25, .125, .0625 and so on, multiples of one half, are round numbers in binary, though they don't look so in decimal. Others which look pretty simple in decimal are not, for example, 0.1 gives you an infinite series 0.11001100110011... in binary. All this numbers give you rounding errors. That is why many languages have introduced 'currency' or 'money' data types, they are either integer data types scaled back to a fixed number of decimal places, or they are floats rounded of the least significant digits. You can do either by yourself, that is, have all currency internally represented as cents and scale it to full units when showing it, or round off at two cents after each calculation. Satyam Anthony Tippett [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] btw, thanks for your response. I'm not sure as if I understand why. It's not like I'm using a very precise number when dealing with the hundreths place. Even without the multiplication the number gets messed up. eg. $a = 17.00; $a+= 1.10; $a+= 0.32; $a+= 0.07; print $a.br; // 18.49 var_dump($a); // float(18.49) var_dump($a-18.49); // float(3.5527136788005E-15) I'm just trying to add money amounts? Can I not rely on floats to do this? Richard Lynch wrote: Floats are NEVER going to be coming out even reliably. You'll have to check if the difference is less than X for whatever number X you like. Or you can look at something like BC_MATH where precision can be carried out as far as you like... But what you are seeing is to be expected. That's just the way computers work, basically. On Mon, April 4, 2005 5:07 pm, Anthony Tippett said: Ok i've narrowed it down a little bit but still can't figure it out.. Here's the code and what I get for the output. Does anyone know what's going on? Can someone else run it on their computer and see if they get the same results? ?php $a = 17.00 * 1; $a+= 1.10 * 1; $a+= 0.32 * 1; $a+= 0.07 * 1; print $a.br; // 18.49 var_dump($a); // float(18.49) var_dump($a-18.49); // float(3.5527136788005E-15) ? Anthony Tippett wrote: I'm having trouble figuring out why subtraction of two floats are giving me a very small number. I'm thinking it has something to do with the internals of type casting, but i'm not sure. If anyone has seen this or can give me some suggestions, please. I have 2 variables that go through a while loop and are added/subtracted/ multipled. InvAmt and InvPay are shown as floats but when they are subtracted, they give me a really small number // code var_dump($InvAmt); var_dump($InvPay); var_dump($InvAmt-$InvPay); // output float(18.49) float(18.49) float(2.1316282072803E-14) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Help. Floats turning into really small numbers? x.xxxxxxxxxxxxxxxxxxxxxxE-xx
Anthony Tippett wrote: I'm having trouble figuring out why subtraction of two floats are giving me a very small number. I'm thinking it has something to do with the internals of type casting, but i'm not sure. If anyone has seen this or can give me some suggestions, please. I have 2 variables that go through a while loop and are added/subtracted/ multipled. InvAmt and InvPay are shown as floats but when they are subtracted, they give me a really small number // code var_dump($InvAmt); var_dump($InvPay); var_dump($InvAmt-$InvPay); // output float(18.49) float(18.49) float(2.1316282072803E-14) Computers are not able to represent floating point numbers precisely. If you are doing financial stuff I would suggest using integer math and working completely in pennies. -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Help. Floats turning into really small numbers? x.xxxxxxxxxxxxxxxxxxxxxxE-xx
I'm having trouble figuring out why subtraction of two floats are giving me a very small number. I'm thinking it has something to do with the internals of type casting, but i'm not sure. If anyone has seen this or can give me some suggestions, please. I have 2 variables that go through a while loop and are added/subtracted/ multipled. InvAmt and InvPay are shown as floats but when they are subtracted, they give me a really small number // code var_dump($InvAmt); var_dump($InvPay); var_dump($InvAmt-$InvPay); // output float(18.49) float(18.49) float(2.1316282072803E-14) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Help. Floats turning into really small numbers? x.xxxxxxxxxxxxxxxxxxxxxxE-xx - Narrowed it down!
Ok i've narrowed it down a little bit but still can't figure it out.. Here's the code and what I get for the output. Does anyone know what's going on? Can someone else run it on their computer and see if they get the same results? ?php $a = 17.00 * 1; $a+= 1.10 * 1; $a+= 0.32 * 1; $a+= 0.07 * 1; print $a.br; // 18.49 var_dump($a); // float(18.49) var_dump($a-18.49); // float(3.5527136788005E-15) ? Anthony Tippett wrote: I'm having trouble figuring out why subtraction of two floats are giving me a very small number. I'm thinking it has something to do with the internals of type casting, but i'm not sure. If anyone has seen this or can give me some suggestions, please. I have 2 variables that go through a while loop and are added/subtracted/ multipled. InvAmt and InvPay are shown as floats but when they are subtracted, they give me a really small number // code var_dump($InvAmt); var_dump($InvPay); var_dump($InvAmt-$InvPay); // output float(18.49) float(18.49) float(2.1316282072803E-14) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Help. Floats turning into really small numbers? x.xxxxxxxxxxxxxxxxxxxxxxE-xx - Narrowed it down!
Floats are NEVER going to be coming out even reliably. You'll have to check if the difference is less than X for whatever number X you like. Or you can look at something like BC_MATH where precision can be carried out as far as you like... But what you are seeing is to be expected. That's just the way computers work, basically. On Mon, April 4, 2005 5:07 pm, Anthony Tippett said: Ok i've narrowed it down a little bit but still can't figure it out.. Here's the code and what I get for the output. Does anyone know what's going on? Can someone else run it on their computer and see if they get the same results? ?php $a = 17.00 * 1; $a+= 1.10 * 1; $a+= 0.32 * 1; $a+= 0.07 * 1; print $a.br; // 18.49 var_dump($a); // float(18.49) var_dump($a-18.49); // float(3.5527136788005E-15) ? Anthony Tippett wrote: I'm having trouble figuring out why subtraction of two floats are giving me a very small number. I'm thinking it has something to do with the internals of type casting, but i'm not sure. If anyone has seen this or can give me some suggestions, please. I have 2 variables that go through a while loop and are added/subtracted/ multipled. InvAmt and InvPay are shown as floats but when they are subtracted, they give me a really small number // code var_dump($InvAmt); var_dump($InvPay); var_dump($InvAmt-$InvPay); // output float(18.49) float(18.49) float(2.1316282072803E-14) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Help. Floats turning into really small numbers? x.xxxxxxxxxxxxxxxxxxxxxxE-xx - Narrowed it down!
btw, thanks for your response. I'm not sure as if I understand why. It's not like I'm using a very precise number when dealing with the hundreths place. Even without the multiplication the number gets messed up. eg. $a = 17.00; $a+= 1.10; $a+= 0.32; $a+= 0.07; print $a.br; // 18.49 var_dump($a); // float(18.49) var_dump($a-18.49); // float(3.5527136788005E-15) I'm just trying to add money amounts? Can I not rely on floats to do this? Richard Lynch wrote: Floats are NEVER going to be coming out even reliably. You'll have to check if the difference is less than X for whatever number X you like. Or you can look at something like BC_MATH where precision can be carried out as far as you like... But what you are seeing is to be expected. That's just the way computers work, basically. On Mon, April 4, 2005 5:07 pm, Anthony Tippett said: Ok i've narrowed it down a little bit but still can't figure it out.. Here's the code and what I get for the output. Does anyone know what's going on? Can someone else run it on their computer and see if they get the same results? ?php $a = 17.00 * 1; $a+= 1.10 * 1; $a+= 0.32 * 1; $a+= 0.07 * 1; print $a.br; // 18.49 var_dump($a); // float(18.49) var_dump($a-18.49); // float(3.5527136788005E-15) ? Anthony Tippett wrote: I'm having trouble figuring out why subtraction of two floats are giving me a very small number. I'm thinking it has something to do with the internals of type casting, but i'm not sure. If anyone has seen this or can give me some suggestions, please. I have 2 variables that go through a while loop and are added/subtracted/ multipled. InvAmt and InvPay are shown as floats but when they are subtracted, they give me a really small number // code var_dump($InvAmt); var_dump($InvPay); var_dump($InvAmt-$InvPay); // output float(18.49) float(18.49) float(2.1316282072803E-14) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Help. Floats turning into really small numbers? x.xxxxxxxxxxxxxxxxxxxxxxE-xx - Narrowed it down!
Thanks to everyone that helped. After further googling, bug 9288 had a good explanation of what was going on ( which is not a bug) http://bugs.php.net/bug.php?id=9288edit=3 I'll just include the answer for anyone that comes upon this tread on a search engine. [15 Feb 2001 2:55pm CET] hholzgra at php rot dot fot net short answer: never use floats or doubles for financial data! longer answer: the internal number format in modern computers is binary (base 2) and not decimal (base 10) for performance and complexity reasons while it is possible to convert decimal numbers into binaries and back this does not hold true for fractions something like 0.3 (decimal) would be a periodic binary fraction like 10/3 is 0.... in decimal this leads to loss of precision when calculation with decimal fractions as you have when storing currency values solution: if you just summ up values then you should store values in the smalest unit your currency has (pennies?) instead of what you are used to (Pounds?) to totally avoid fractions if you cannot avoid fractions (like when dealing with percentage calculations or currency conversions) you should just be aware of the (usually very small) internal conversion differences (0.27755575615629 in your example) or use the bcmath extension, although for monetary values you should go perfectly fine with using round(...,2) on your final results Anthony Tippett wrote: btw, thanks for your response. I'm not sure as if I understand why. It's not like I'm using a very precise number when dealing with the hundreths place. Even without the multiplication the number gets messed up. eg. $a = 17.00; $a+= 1.10; $a+= 0.32; $a+= 0.07; print $a.br; // 18.49 var_dump($a); // float(18.49) var_dump($a-18.49); // float(3.5527136788005E-15) I'm just trying to add money amounts? Can I not rely on floats to do this? Richard Lynch wrote: Floats are NEVER going to be coming out even reliably. You'll have to check if the difference is less than X for whatever number X you like. Or you can look at something like BC_MATH where precision can be carried out as far as you like... But what you are seeing is to be expected. That's just the way computers work, basically. On Mon, April 4, 2005 5:07 pm, Anthony Tippett said: Ok i've narrowed it down a little bit but still can't figure it out.. Here's the code and what I get for the output. Does anyone know what's going on? Can someone else run it on their computer and see if they get the same results? ?php $a = 17.00 * 1; $a+= 1.10 * 1; $a+= 0.32 * 1; $a+= 0.07 * 1; print $a.br; // 18.49 var_dump($a); // float(18.49) var_dump($a-18.49); // float(3.5527136788005E-15) ? Anthony Tippett wrote: I'm having trouble figuring out why subtraction of two floats are giving me a very small number. I'm thinking it has something to do with the internals of type casting, but i'm not sure. If anyone has seen this or can give me some suggestions, please. I have 2 variables that go through a while loop and are added/subtracted/ multipled. InvAmt and InvPay are shown as floats but when they are subtracted, they give me a really small number // code var_dump($InvAmt); var_dump($InvPay); var_dump($InvAmt-$InvPay); // output float(18.49) float(18.49) float(2.1316282072803E-14) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php