Re: [PHP] Re: bcmath integer type?
On Thu, 2009-05-07 at 21:45 -0700, Michael A. Peters wrote: Michael A. Peters wrote: I'm having a problem with db2 and prepared statements. var_dump indicates that some variables that should be type int are type text. These variables are the output of bcmath equations, and are integer. Does bcmath for some reason output a text type? I can fix it by adding 0 but I want to know if I'm using bcmath incorrectly. LOL - I would like to know if bcmath is suppose to return int (I'm guessing yes so it can deal with numbers outside of fp math) but I don't think that was the issue. $foo[] = Array($var1,$var2) was my problem ... notice the [] ;) From the documentation at: http://ca2.php.net/manual/en/function.bcadd.php We see that bcadd has the prototype: string bcadd ( string $left_operand , string $right_operand [, int $scale ] ) See the string, that's because it takes strings and returns strings. The reason why is in the description: Add two arbitrary precision numbers That arbitrary part rules out integers or even floats since they are fixed width datatypes (fixed width with respect to the number of bits available to represent them). It may not be the case with what your doing, but with large enough (positive or negative numbers), or numbers with sufficient decimal places) you WILL lose precision by converting to a real integer or floating point value. If you don't need arbitrary precision functionality, then don't use the bcxxx() functions since they are MUCH slower than doing normal math. 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] Re: bcmath integer type?
Robert Cummings wrote: On Thu, 2009-05-07 at 21:45 -0700, Michael A. Peters wrote: Michael A. Peters wrote: I'm having a problem with db2 and prepared statements. var_dump indicates that some variables that should be type int are type text. These variables are the output of bcmath equations, and are integer. Does bcmath for some reason output a text type? I can fix it by adding 0 but I want to know if I'm using bcmath incorrectly. LOL - I would like to know if bcmath is suppose to return int (I'm guessing yes so it can deal with numbers outside of fp math) but I don't think that was the issue. $foo[] = Array($var1,$var2) was my problem ... notice the [] ;) From the documentation at: http://ca2.php.net/manual/en/function.bcadd.php We see that bcadd has the prototype: string bcadd ( string $left_operand , string $right_operand [, int $scale ] ) See the string, that's because it takes strings and returns strings. The reason why is in the description: Add two arbitrary precision numbers That arbitrary part rules out integers or even floats since they are fixed width datatypes (fixed width with respect to the number of bits available to represent them). It may not be the case with what your doing, but with large enough (positive or negative numbers), or numbers with sufficient decimal places) you WILL lose precision by converting to a real integer or floating point value. If you don't need arbitrary precision functionality, then don't use the bcxxx() functions since they are MUCH slower than doing normal math. I started using them because there were a few cases where normal operations caused imprecision in unit conversions. With bcmath I could specify extra precision and then round to the precision I needed as the last step. Maybe there's a better way to do it but it solved the problem. I probably don't need them in this case, I can just round down the result to get the integer result, but the use is so small that the performance hit probably isn't worth changing them and pushing it through testing again. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: bcmath integer type?
Michael A. Peters wrote: I'm having a problem with db2 and prepared statements. var_dump indicates that some variables that should be type int are type text. These variables are the output of bcmath equations, and are integer. Does bcmath for some reason output a text type? I can fix it by adding 0 but I want to know if I'm using bcmath incorrectly. LOL - I would like to know if bcmath is suppose to return int (I'm guessing yes so it can deal with numbers outside of fp math) but I don't think that was the issue. $foo[] = Array($var1,$var2) was my problem ... notice the [] ;) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: An integer?
Why not just use a regular expression match to see if there is anything other than numeric characters? That will tell you if it is an integer before you do the type casting. (sorry if that sounds a little obvious but no-one seems to have suggested it) - Dan Unknown Sender [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED]... This may be a silly question, but I can't spot and easy way to do it, I want to check if the value inside a vriable is an integer. If I do is_int($variable) I always get false, because this variable qwas read in from a form so its a string, if I make the variable an integer using settype() I just end up with 0 if it was invalid, the number otherwise. I need something that will actually tell me if there the conversion was successfull. Am I missing something? Thanks, Shaun -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]