RE: [PHP] More math fun
1.7763568394E-15 is 0. The computer just had a rounding error somewhere. We had this discussion a few weeks ago, the solution being to decide to what accuracy you want the answer. Upto 14 decimal places will give you 0 here!! Alex -Original Message- From: Robert Cummings [mailto:[EMAIL PROTECTED] Sent: 13 August 2008 05:39 To: Micah Gersten Cc: Jim Lucas; Jay Blanchard; php-general@lists.php.net Subject: Re: [PHP] More math fun On Tue, 2008-08-12 at 23:23 -0500, Micah Gersten wrote: Robert, when you do yours, it's performing the same function on two different variable types and has to do a conversion before the function works. He was doing a numeric function on a string which might be giving the funky results. That's what Jim tried and got 0 and double... I confirmed Jim's results (I didn't originally test Jay's problem) and then tried the alternative that I might have thought this morning. Either way, it seems to come up as 0. Maybe Jay is running a buggy version of PHP. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php No virus found in this incoming message. Checked by AVG - http://www.avg.com Version: 8.0.138 / Virus Database: 270.6.1/1608 - Release Date: 12/08/2008 16:59 No virus found in this outgoing message. Scanned by AVG Free 8.0 Checked by AVG - http://www.avg.com Version: 8.0.138 / Virus Database: 270.6.1/1608 - Release Date: 12/08/2008 16:59 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] More math fun
On Wed, 2008-08-13 at 08:14 +0100, Alex Chamberlain wrote: 1.7763568394E-15 is 0. The computer just had a rounding error somewhere. We had this discussion a few weeks ago, the solution being to decide to what accuracy you want the answer. Upto 14 decimal places will give you 0 here!! I thought we had a different discussion a couple of weeks ago about floating point storage precision. How come only Jay is getting *this* rounding error? Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] More math fun
[snip] 1.7763568394E-15 is 0. The computer just had a rounding error somewhere. We had this discussion a few weeks ago, the solution being to decide to what accuracy you want the answer. Upto 14 decimal places will give you 0 here!! I thought we had a different discussion a couple of weeks ago about floating point storage precision. How come only Jay is getting *this* rounding error? [/snip] Perhaps it is the PHP version? I have tried this on three separate PHP installations (all the same configs) and got the same results. I will look to see if there is an upgraded version. When applying round to the numbers everything works out as expected, but makes me uncomfortable. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] More math fun
Jay Blanchard wrote: [snip] 1.7763568394E-15 is 0. The computer just had a rounding error somewhere. We had this discussion a few weeks ago, the solution being to decide to what accuracy you want the answer. Upto 14 decimal places will give you 0 here!! I thought we had a different discussion a couple of weeks ago about floating point storage precision. How come only Jay is getting *this* rounding error? [/snip] Perhaps it is the PHP version? I have tried this on three separate PHP installations (all the same configs) and got the same results. I will look to see if there is an upgraded version. When applying round to the numbers everything works out as expected, but makes me uncomfortable. What is your precision in your php.ini file set to? Default is 14. If it is set to 16 or greater, then it would display the reseults you are seeing. Mine is set to 14, so it would automatically round it to 0. I think. I will test, brb.. hmmm weird, if I set my precision to 20 and restart, I still get 0 for the value. Odd, I would have thought it would have then shown me the expected value. -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] More math fun
On Wed, 2008-08-13 at 07:47 -0700, Jim Lucas wrote: Jay Blanchard wrote: [snip] 1.7763568394E-15 is 0. The computer just had a rounding error somewhere. We had this discussion a few weeks ago, the solution being to decide to what accuracy you want the answer. Upto 14 decimal places will give you 0 here!! I thought we had a different discussion a couple of weeks ago about floating point storage precision. How come only Jay is getting *this* rounding error? [/snip] Perhaps it is the PHP version? I have tried this on three separate PHP installations (all the same configs) and got the same results. I will look to see if there is an upgraded version. When applying round to the numbers everything works out as expected, but makes me uncomfortable. What is your precision in your php.ini file set to? Default is 14. If it is set to 16 or greater, then it would display the reseults you are seeing. Mine is set to 14, so it would automatically round it to 0. I think. I will test, brb.. hmmm weird, if I set my precision to 20 and restart, I still get 0 for the value. Odd, I would have thought it would have then shown me the expected value. I tried the highest precision setting that PHP appears to honour... 54. I still get 0 :) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] More math fun
abs($balanceTest) = 15.22 abs($oldLineArray[16]) = 15.22 $diff = abs($balanceTest) - abs($oldLineArray[16]); echo abs($diff) . \n; 1.7763568394E-15 WTF? This should be a big fat 0 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] More math fun
On Tue, 2008-08-12 at 15:18 -0500, Jay Blanchard wrote: abs($balanceTest) = 15.22 abs($oldLineArray[16]) = 15.22 $diff = abs($balanceTest) - abs($oldLineArray[16]); echo abs($diff) . \n; 1.7763568394E-15 WTF? This should be a big fat 0 Please provide the list with the following output: ?php var_dump( $balanceTest ); var_dump( $oldLineArray[16] ); ? Methinks you have different data types. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] More math fun
[snip] Please provide the list with the following output: ?php var_dump( $balanceTest ); var_dump( $oldLineArray[16] ); ? Methinks you have different data types. [/snip] string(5) 15.22 float(15.22) You're right*smacks forehead*. Sometimes this whole forest and trees thing kills me. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] More math fun
[snip] ?php var_dump( $balanceTest ); var_dump( $oldLineArray[16] ); ? Methinks you have different data types. [/snip] string(5) 15.22 float(15.22) You're right*smacks forehead*. Sometimes this whole forest and trees thing kills me. [/snip] settype($balanceTest, float) had no effect -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] More math fun
[snip] string(5) 15.22 float(15.22) You're right*smacks forehead*. Sometimes this whole forest and trees thing kills me. [/snip] settype($balanceTest, float) had no effect [/snip] $diff = round(abs($balanceTest), 2) - round(abs($oldLineArray[16]), 2); Works? Does round convert the string to a float? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] More math fun
Jay Blanchard wrote: abs($balanceTest) = 15.22 abs($oldLineArray[16]) = 15.22 $diff = abs($balanceTest) - abs($oldLineArray[16]); echo abs($diff) . \n; 1.7763568394E-15 WTF? This should be a big fat 0 I do not see how it makes any difference if $balanceTest and $oldLineArray[16] are strings in the beginning. In line that does the subtraction, they would have been converted to floats automagically by abs(). If you look at the manual for this function, you will see that the returned value is either a float or integer bases on the input type. Float would return a float. Anything else would return a integer. BTW - if I setup the following: ?php $balanceTest = '15.22'; # A string $oldLineArray[16] = 15.22; # A float $diff = abs($balanceTest) - abs($oldLineArray[16]); echo abs($diff) . \n; echo gettype(abs($diff)) . \n; # 1.7763568394E-15 ? I get 0 and 'double' -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] More math fun
On Tue, 2008-08-12 at 14:59 -0700, Jim Lucas wrote: Jay Blanchard wrote: abs($balanceTest) = 15.22 abs($oldLineArray[16]) = 15.22 $diff = abs($balanceTest) - abs($oldLineArray[16]); echo abs($diff) . \n; 1.7763568394E-15 WTF? This should be a big fat 0 I do not see how it makes any difference if $balanceTest and $oldLineArray[16] are strings in the beginning. In line that does the subtraction, they would have been converted to floats automagically by abs(). If you look at the manual for this function, you will see that the returned value is either a float or integer bases on the input type. Float would return a float. Anything else would return a integer. BTW - if I setup the following: ?php $balanceTest = '15.22'; # A string $oldLineArray[16] = 15.22; # A float $diff = abs($balanceTest) - abs($oldLineArray[16]); echo abs($diff) . \n; echo gettype(abs($diff)) . \n; # 1.7763568394E-15 ? I get 0 and 'double' Yeah, I was probably thinking abs( $balanceTest - $oldLineArray[16] ) versus what was actually present. But either way, I get 0 doing the above also when I set up the values... so I have no idea how it's not working for Jay. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] More math fun
Robert, when you do yours, it's performing the same function on two different variable types and has to do a conversion before the function works. He was doing a numeric function on a string which might be giving the funky results. Thank you, Micah Gersten onShore Networks Internal Developer http://www.onshore.com Robert Cummings wrote: On Tue, 2008-08-12 at 14:59 -0700, Jim Lucas wrote: Jay Blanchard wrote: abs($balanceTest) = 15.22 abs($oldLineArray[16]) = 15.22 $diff = abs($balanceTest) - abs($oldLineArray[16]); echo abs($diff) . \n; 1.7763568394E-15 WTF? This should be a big fat 0 I do not see how it makes any difference if $balanceTest and $oldLineArray[16] are strings in the beginning. In line that does the subtraction, they would have been converted to floats automagically by abs(). If you look at the manual for this function, you will see that the returned value is either a float or integer bases on the input type. Float would return a float. Anything else would return a integer. BTW - if I setup the following: ?php $balanceTest = '15.22'; # A string $oldLineArray[16] = 15.22; # A float $diff = abs($balanceTest) - abs($oldLineArray[16]); echo abs($diff) . \n; echo gettype(abs($diff)) . \n; # 1.7763568394E-15 ? I get 0 and 'double' Yeah, I was probably thinking abs( $balanceTest - $oldLineArray[16] ) versus what was actually present. But either way, I get 0 doing the above also when I set up the values... so I have no idea how it's not working for Jay. Cheers, Rob. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] More math fun
On Tue, 2008-08-12 at 23:23 -0500, Micah Gersten wrote: Robert, when you do yours, it's performing the same function on two different variable types and has to do a conversion before the function works. He was doing a numeric function on a string which might be giving the funky results. That's what Jim tried and got 0 and double... I confirmed Jim's results (I didn't originally test Jay's problem) and then tried the alternative that I might have thought this morning. Either way, it seems to come up as 0. Maybe Jay is running a buggy version of PHP. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php