Re: [PHP] Loss of precision in intval()
Richard's right. You get the same result if you do the equivalent in ASP. Regards, Bruce >>> "Richard Lynch" <[EMAIL PROTECTED]> 8/08/2007 3:29:16 p.m. >>> On Wed, August 1, 2007 11:52 am, Mark Summers wrote: > This sort of thing really isn't helpful... > > > $a = 75.82 * 100; > > echo intval($a); > > ?> What did you get? What did you expect? Do you have ANY idea how floats are actually represented internally in every computer language? [*] If you expect to have a specific precision, then you need to write code to have that precision. For simple "money" just use cents instead of dollars and keep everything as an int except for final output. For anything more complicated, you have http://php.net/gmp or, in lder versions of PHP, BC Math. * Yeah, okay, there ARE languages where 75.82 really IS 75.82 and not some close approximation... But they are pretty esoteric languages, really. -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- 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] Loss of precision in intval()
On Wed, August 1, 2007 11:52 am, Mark Summers wrote: > This sort of thing really isn't helpful... > > > $a = 75.82 * 100; > > echo intval($a); > > ?> What did you get? What did you expect? Do you have ANY idea how floats are actually represented internally in every computer language? [*] If you expect to have a specific precision, then you need to write code to have that precision. For simple "money" just use cents instead of dollars and keep everything as an int except for final output. For anything more complicated, you have http://php.net/gmp or, in lder versions of PHP, BC Math. * Yeah, okay, there ARE languages where 75.82 really IS 75.82 and not some close approximation... But they are pretty esoteric languages, really. -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loss of precision in intval()
Those numbers must fall on the other side of the number. E.g., 75.81 ==> 75.8100011 (etc) so you get the expected results. Mark Summers wrote: > I like to think that I'm reasonably aware of the limitations of floating > point (famous last words). > > To my mind, the ridiculousness (probably not a word) of the example is > highlighted by the fact that 75.81 and 75.83 work perfectly. > > Roberto Mansfield wrote: >> Internally, 75.82 can't be stored exactly, so 75.82 * 100 is probably >> 7581.92 rather than the expected integer value of 7582. So intval is >> behaving properly. Sounds like you want intval(round($a)); >> >> >> >> [EMAIL PROTECTED] wrote: >> >>> Very weird and counter intuitive. Looking at the php manual, I see >>> this: >>> >>> Converting to integer from floating point: >>> >>> "When converting from float to integer, the number will be rounded >>> towards zero." >>> >>> But you'd think the multiplication would happen before the rounding. >>> >>> if you do: >>> $a = ceil(75.82 * 100); >>> >>> you should get the proper answer. >>> >>> This is what I used for testing: >>> >>> >> $x = 75.82; >>> $y = 100; >>> >>> echo "x is " . gettype($x) . "\n"; >>> echo "y is " . gettype($y) . "\n"; >>> >>> $a = ceil($x * $y); >>> >>> echo "a is " . gettype($a) . "\n"; >>> >>> echo "intval(a) is " . gettype(intval($a)) . "\n"; >>> >>> echo $a . " *** " . intval($a); >>> ?> >>> >>> Not sure that really helps, but seems to be some kind of order of >>> precedence issue. >>> >>> -TG >>> >>> = = = Original message = = = >>> >>> This sort of thing really isn't helpful... >>> >>> >> >>> $a = 75.82 * 100; >>> >>> echo intval($a); >>> >>> ?> >>> >>> >>> ___ >>> Sent by ePrompter, the premier email notification software. >>> Free download at http://www.ePrompter.com. >>> >> >> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loss of precision in intval()
I like to think that I'm reasonably aware of the limitations of floating point (famous last words). To my mind, the ridiculousness (probably not a word) of the example is highlighted by the fact that 75.81 and 75.83 work perfectly. Roberto Mansfield wrote: Internally, 75.82 can't be stored exactly, so 75.82 * 100 is probably 7581.92 rather than the expected integer value of 7582. So intval is behaving properly. Sounds like you want intval(round($a)); [EMAIL PROTECTED] wrote: Very weird and counter intuitive. Looking at the php manual, I see this: Converting to integer from floating point: "When converting from float to integer, the number will be rounded towards zero." But you'd think the multiplication would happen before the rounding. if you do: $a = ceil(75.82 * 100); you should get the proper answer. This is what I used for testing: \n"; echo "y is " . gettype($y) . "\n"; $a = ceil($x * $y); echo "a is " . gettype($a) . "\n"; echo "intval(a) is " . gettype(intval($a)) . "\n"; echo $a . " *** " . intval($a); ?> Not sure that really helps, but seems to be some kind of order of precedence issue. -TG = = = Original message = = = This sort of thing really isn't helpful... ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loss of precision in intval()
Roberto Mansfield wrote: Internally, 75.82 can't be stored exactly, so 75.82 * 100 is probably 7581.92 rather than the expected integer value of 7582. So intval is behaving properly. Sounds like you want intval(round($a)); [EMAIL PROTECTED] wrote: Very weird and counter intuitive. Looking at the php manual, I see this: Converting to integer from floating point: "When converting from float to integer, the number will be rounded towards zero." But you'd think the multiplication would happen before the rounding. if you do: $a = ceil(75.82 * 100); you should get the proper answer. This is what I used for testing: \n"; echo "y is " . gettype($y) . "\n"; $a = ceil($x * $y); echo "a is " . gettype($a) . "\n"; echo "intval(a) is " . gettype(intval($a)) . "\n"; echo $a . " *** " . intval($a); ?> Not sure that really helps, but seems to be some kind of order of precedence issue. -TG = = = Original message = = = This sort of thing really isn't helpful... ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. Better yet: $a + 0, or (int)$a. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loss of precision in intval()
On 8/1/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Probably return "7582" instead of "7581". > > = = = Original message = = = > > On 8/1/07, Mark Summers <[EMAIL PROTECTED]> wrote: > > This sort of thing really isn't helpful... > > > > > > > $a = 75.82 * 100; > > > > echo intval($a); > > > > ?> > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > What exactly were you expecting it to do? :) > > > > ___ > Sent by ePrompter, the premier email notification software. > Free download at http://www.ePrompter.com. > > http://us.php.net/manual/en/function.intval.php#63627 erics:~ eric$ php -r 'var_dump(printf("%.13f", (75.82 * 100)));' 7581.1int(18) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loss of precision in intval()
Internally, 75.82 can't be stored exactly, so 75.82 * 100 is probably 7581.92 rather than the expected integer value of 7582. So intval is behaving properly. Sounds like you want intval(round($a)); [EMAIL PROTECTED] wrote: > Very weird and counter intuitive. Looking at the php manual, I see this: > > Converting to integer from floating point: > > "When converting from float to integer, the number will be rounded towards > zero." > > But you'd think the multiplication would happen before the rounding. > > if you do: > $a = ceil(75.82 * 100); > > you should get the proper answer. > > This is what I used for testing: > > $x = 75.82; > $y = 100; > > echo "x is " . gettype($x) . "\n"; > echo "y is " . gettype($y) . "\n"; > > $a = ceil($x * $y); > > echo "a is " . gettype($a) . "\n"; > > echo "intval(a) is " . gettype(intval($a)) . "\n"; > > echo $a . " *** " . intval($a); > ?> > > Not sure that really helps, but seems to be some kind of order of precedence > issue. > > -TG > > = = = Original message = = = > > This sort of thing really isn't helpful... > > > $a = 75.82 * 100; > > echo intval($a); > > ?> > > > ___ > Sent by ePrompter, the premier email notification software. > Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loss of precision in intval()
It is most definitely not if what you want is the square root, or the hyperbolic cosine or any other of a zillion things. - Original Message - From: "Mark Summers" <[EMAIL PROTECTED]> To: Sent: Wednesday, August 01, 2007 6:52 PM Subject: [PHP] Loss of precision in intval() This sort of thing really isn't helpful... -- 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 Free Edition. Version: 7.5.476 / Virus Database: 269.11.0/929 - Release Date: 31/07/2007 17:26 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loss of precision in intval()
Probably return "7582" instead of "7581". = = = Original message = = = On 8/1/07, Mark Summers <[EMAIL PROTECTED]> wrote: > This sort of thing really isn't helpful... > > > $a = 75.82 * 100; > > echo intval($a); > > ?> > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > What exactly were you expecting it to do? :) ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loss of precision in intval()
Very weird and counter intuitive. Looking at the php manual, I see this: Converting to integer from floating point: "When converting from float to integer, the number will be rounded towards zero." But you'd think the multiplication would happen before the rounding. if you do: $a = ceil(75.82 * 100); you should get the proper answer. This is what I used for testing: \n"; echo "y is " . gettype($y) . "\n"; $a = ceil($x * $y); echo "a is " . gettype($a) . "\n"; echo "intval(a) is " . gettype(intval($a)) . "\n"; echo $a . " *** " . intval($a); ?> Not sure that really helps, but seems to be some kind of order of precedence issue. -TG = = = Original message = = = This sort of thing really isn't helpful... ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Loss of precision in intval()
On 8/1/07, Mark Summers <[EMAIL PROTECTED]> wrote: > This sort of thing really isn't helpful... > > > $a = 75.82 * 100; > > echo intval($a); > > ?> > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > What exactly were you expecting it to do? :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Loss of precision in intval()
This sort of thing really isn't helpful... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php