[PHP] Understanding intval() and types conversion
Hi, I'm trying to understand how the intval() function works, but I'm unable to understand how exactly it works. Take a look at this piece of code (Tested on PHP 5.0.3): ?php $a = (0.1 + 0.7) * 10; $b = intval($a); echo 'a - '. $a .' - '. gettype($a); // Prints: a - 8 - double echo 'br'; echo 'b - '. $b .' - '. gettype($b); // Prints: b - 7 - integer ? I also tested settype() and casting: settype($a, 'integer'); // New value for $a is 7 $b = (int) $a; // Value for $b is 7 I cannot understand it. If originally the value for $a is 8 (double), why when converting to integer I get 7? Any help or comment which helps me to understand that will be really welcome! Jordi. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Understanding intval() and types conversion
Jordi Canals wrote: I'm trying to understand how the intval() function works, but I'm unable to understand how exactly it works. Deep deep inside the guts of the computer, $a is represented as something not unlike: 80e0 In other words, 8.0 x 10 to the 0th power. Now, when the computer has to convert that to an integer, the mathematics involved are, well, pretty complex. Basically, you're looking at a good solid semester of college-level courses in computer science to *really* dig into all the guts of binary, ones-complement, twos-complement, and other internal representations. Also note that the way things are *really* stored is platform/OS dependent, so you could get different results from intval() on, say, Windows and Linux and Mac (and whatever else PHP runs on). You can be quite sure it will always be 7 or 8 for this example, as the number internally will be very very very close to 7.99... or 8.000... But (mostly) computers just don't think of decimal numbers and fractions the same way you and I do, without some pretty special-purpose coding for it. Some real-world suggestions: Use round(), floor() and ceil() as much as you can to convert if you are worried about this kind of stuff. If you *NEED* more precision for scientific purposes, the BC_MATH extension lets *you* decide how many decimal points to keep around, and you can get as precise as you want -- Up to the limits of the RAM on your machine, and at a severe performance penalty compared to the built-in OS-dependent math. It should also be noted that there are languages (EG Lisp) which actually *DO* understand fractions as part of the language specification, and which just maybe might be more suitable for whatever it is you are doing if PHP's math is frustrating you. I suspect somebody somewhere has some kind of fractional math package you could integate with PHP, though, if you dig for it. Keep in mind that it's not even really the way PHP thinks of numbers when you are trying to figure out what intval() is doing -- It's built into the hardware and the Operating System, and PHP pretty much just spits out whatever the OS tells it to for intval(). -- 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] Understanding intval() and types conversion
Jordi Canals wrote: Hi, I'm trying to understand how the intval() function works, but I'm you'd think offhand that that would be easy unable to understand how exactly it works. hmm. very odd. I took a look at your code and did a few more tests on PHP 5.0.2 (cli) (built: Oct 21 2004 13:52:27): OUTPUT $a = (0.1 + 0.7) * 10;var_dump($a); float(8) $b = intval($a);var_dump($b); int(7) $c = $a;settype($c, integer);var_dump($c); int(7) $d = intval($a);var_dump($d); int(8) $e = floatval($a);var_dump($e); float(8) $f = floatval($a);var_dump($f); float(8) $g = (int) floatval($a);var_dump($g); int(7) $h = $a;settype($h, integer);var_dump($h); int(8) var_dump($a,$b,$c,$d,$e,$f,$g,$h,$a); float(8) int(7) int(7) int(8) float(8) float(8) int(7) int(8) string(1) 8 /OUTPUT CMD-LINE-CODE php -r ' $a = (0.1 + 0.7) * 10; echo \n\$a = (0.1 + 0.7) * 10;var_dump(\$a);\n\n; var_dump($a); $b = intval($a); echo \n\$b = intval(\$a);var_dump(\$b);\n\n; var_dump($b); $c = $a;settype($c, integer); echo \n\$c = \$a;settype(\$c, \integer\);var_dump(\$c);\n\n; var_dump($c); $d = intval($a); echo \n\$d = intval(\\$a\);var_dump(\$d);\n\n; var_dump($d); $e = floatval($a); echo \n\$e = floatval(\$a);var_dump(\$e);\n\n; var_dump($e); $f = floatval($a); echo \n\$f = floatval(\\$a\);var_dump(\$f);\n\n; var_dump($f); $g = (int) floatval($a); echo \n\$g = (int) floatval(\$a);var_dump(\$g);\n\n; var_dump($g); $h = $a;settype($h, integer); echo \n\$h = \$a;settype(\\$h\, \integer\);var_dump(\$h);\n\n; var_dump($h); echo \n\nvar_dump(\$a,\$b,\$c,\$d,\$e,\$f,\$g,\$h,\\$a\);\n\n; var_dump($a,$b,$c,$d,$e,$f,$g,$h,$a); ' /CMD-LINE-CODE Take a look at this piece of code (Tested on PHP 5.0.3): ?php $a = (0.1 + 0.7) * 10; $b = intval($a); echo 'a - '. $a .' - '. gettype($a); // Prints: a - 8 - double echo 'br'; echo 'b - '. $b .' - '. gettype($b); // Prints: b - 7 - integer sidenote: if you use double quotes in your test code you make it a little easier for people to run the code off the cmdline ('php -r' on *nix). ? I also tested settype() and casting: settype($a, 'integer'); // New value for $a is 7 $b = (int) $a; // Value for $b is 7 I cannot understand it. If originally the value for $a is 8 (double), why when converting to integer I get 7? my guess this is a round error issue - never noticed this problem before... this is a problem right? anybody? Any help or comment which helps me to understand that will be really welcome! Jordi. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Understanding intval() and types conversion
Richard Lynch wrote: Jordi Canals wrote: I'm trying to understand how the intval() function works, but I'm unable to understand how exactly it works. Deep deep inside the guts of the computer, $a is represented as something not unlike: ... thanks Richard for that explaination! lots of light bulbs just went on. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Understanding intval() and types conversion
my guess this is a round error issue - never noticed this problem before... this is a problem right? anybody? You can say it is a round error issue. You can not say it is a problem. You simply have to be aware that the float number which you think of as 8.... inside of PHP may well be represented as 7.... down in the guts of the machine. It may help you feel better about this if you recall from grade school mathematics, that, in point of fact: 7.999... is EXACTLY equal to 8.000... Those who doubt this fact are requested to remember how to convert decimals such as: 0.333... to fractions (1/3), and then apply that exact same method to 7.999... You will quickly find yourself with a fraction (72/9) which is precisely equal to 8/1 which is 8. EVERY floating point number, then, that ends in x000... has a second representation, exactly equal, which ends in (x-1)999... Bottom line, however you look at it, is that you can NEVER be 100% certain that a computer's float number will convert to what you expect in an integer because their internal representation simply does not allow for perfection. This is a feature, not a bug. :-) -- 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] Understanding intval() and types conversion
so... you're saying that 7.9 (repeating) is equal to 8.0 i say prove it.. as i recall the numbers might be for all practical purposes the same, they are in fact vastly different... so, prove your assertion... -bruce -Original Message- From: Richard Lynch [EMAIL PROTECTED] Sent: Jan 25, 2005 2:57 PM To: Jochem Maas [EMAIL PROTECTED] Cc: Jordi Canals [EMAIL PROTECTED], PHP List php-general@lists.php.net Subject: Re: [PHP] Understanding intval() and types conversion my guess this is a round error issue - never noticed this problem before... this is a problem right? anybody? You can say it is a round error issue. You can not say it is a problem. You simply have to be aware that the float number which you think of as 8.... inside of PHP may well be represented as 7.... down in the guts of the machine. It may help you feel better about this if you recall from grade school mathematics, that, in point of fact: 7.999... is EXACTLY equal to 8.000... Those who doubt this fact are requested to remember how to convert decimals such as: 0.333... to fractions (1/3), and then apply that exact same method to 7.999... You will quickly find yourself with a fraction (72/9) which is precisely equal to 8/1 which is 8. EVERY floating point number, then, that ends in x000... has a second representation, exactly equal, which ends in (x-1)999... Bottom line, however you look at it, is that you can NEVER be 100% certain that a computer's float number will convert to what you expect in an integer because their internal representation simply does not allow for perfection. This is a feature, not a bug. :-) -- 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Understanding intval() and types conversion
On Tue, 2005-01-25 at 17:29, Bruce Douglas wrote: so... you're saying that 7.9 (repeating) is equal to 8.0 i say prove it.. as i recall the numbers might be for all practical purposes the same, they are in fact vastly different... so, prove your assertion... -bruce Check out this one: http://mathforum.org/dr.math/faq/faq.0..html Bret -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Understanding intval() and types conversion
Bruce Douglas wrote: so... you're saying that 7.9 (repeating) is equal to 8.0 i say prove it.. as i recall the numbers might be for all practical purposes the same, they are in fact vastly different... so, prove your assertion... he did. you have to consider the two the same, given that there is always a limit to the level of precision - the alternative would be to say no 2 numbers are ever equal and that make for boring mathematics. actually the underlying maths kinda makes my head spin :-) -bruce -Original Message- From: Richard Lynch [EMAIL PROTECTED] Sent: Jan 25, 2005 2:57 PM To: Jochem Maas [EMAIL PROTECTED] Cc: Jordi Canals [EMAIL PROTECTED], PHP List php-general@lists.php.net Subject: Re: [PHP] Understanding intval() and types conversion my guess this is a round error issue - never noticed this problem before... this is a problem right? anybody? You can say it is a round error issue. You can not say it is a problem. You simply have to be aware that the float number which you think of as 8.... inside of PHP may well be represented as 7.... down in the guts of the machine. It may help you feel better about this if you recall from grade school mathematics, that, in point of fact: 7.999... is EXACTLY equal to 8.000... Those who doubt this fact are requested to remember how to convert decimals such as: 0.333... to fractions (1/3), and then apply that exact same method to 7.999... You will quickly find yourself with a fraction (72/9) which is precisely equal to 8/1 which is 8. EVERY floating point number, then, that ends in x000... has a second representation, exactly equal, which ends in (x-1)999... Bottom line, however you look at it, is that you can NEVER be 100% certain that a computer's float number will convert to what you expect in an integer because their internal representation simply does not allow for perfection. This is a feature, not a bug. :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Understanding intval() and types conversion
Bruce Douglas wrote: so... you're saying that 7.9 (repeating) is equal to 8.0 i say prove it.. as i recall the numbers might be for all practical purposes the same, they are in fact vastly different... so, prove your assertion... Claim: 7.... (repeating) is EXACTLY equal to 8. Period. Remember converting repeating decimals to fractions back in 5th grade? Convert 0.33... to a fraction. Let x = 0.33... 10x = 3.... 10x = 3.333... - x = -0.333... = 9x = 3 x = 3/9 x = 1/3 You did that, right? Okay, now try it with 7.... Let x = 7.... 10x = 79.9... 10x = 79.99... - x = -7.999... = 9x = 72 x = 72/9 x = 8 QED You can do this with *ANY* repeating decimal to get a fractional respresentation EXACTLY equal to that repeating decimal. x = 0.... 10x = 6. - x = -. === 9x = 6 x = 6/9 x = 2/3 All rules of mathematics prove these are EXACTLY equal. Not close. EQUAL. TRY IT! 0.2 0.4 0.1212121212121212 (You have to use 100x on this one) You get the fraction which is EQUAL to the repeating decimal. Now go for: 0. 1.9 2.99 0.599 0.123499 If it ends in ???x99 you *WILL* find out that there is a corresponding ???(x-1)00... to match it. I *KNOW* it seems counter-intuitive, but there it is. PS I've often wondered why Cantor's Diagonal Theorom never bothered to take this into account -- Sure, you could always just pick a non-9 and non-0 digit instead of just the 'next' digit, but... A contrived counter-example to Cantor's Diagonal Theorom is trivial to create and dis-prove it as it is written in most textbooks. Oh well. Somebody Else's Problem. This is sooo off-topic! But it does sort of point out that even in the mathematics you've used every single day of your life, things are not always what they seem. It's just even more not what they seem inside a computer using PHP. :-) PPS Don't take my word for it. There are only a few zillion mathematicians and/or 5th-graders who can provide an abundance of examples. :-) :-) :-) -- 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] Understanding intval() and types conversion
Bruce Douglas wrote: no.. he didn't. Yes he did... he just used calculus to do it. I'm not going to give a full calculus lesson here (you can google for that), but basically the limit of 0.999 (repeating) as you go towards *infinity* decimal places is 1 8.0 = 8.0 7. (repeating) = 7. (repeating) 8.0 != 7. (repeating) In this case it is like 7 + 1 = 7 + .999 (repeating) Which goes to 1 = . (repeating) Which is true, if you know the calculus involved here. Seriously, google for it. now, if you want to get into conversations with regards to internal binary representations because of the limits of the register architecture in computers, then i might say the issue is the fact that the algorithms employed within the hardware are flawed... but let's not start screwing with basic math theory I wouldn't call it basic... but calculus is indeed a theory. Heck it might even be on the level of containing mathematical axioms, but I don't know that for sure ;) the statement that he uses where he states that there is a limit to the level of precision is lazy at best... (from a mathmatical perspective) or can i simply say, 15000 = 14,800 based upon my level of precision Not limit to precision... limit as you approach infinity. It assumes *no* arbitrary cutoff point of precision... whereas most OS's do indeed cut off after X number of decimal places. -bruce -- Teach a man to fish... NEW? | http://www.catb.org/~esr/faqs/smart-questions.html STFA | http://marc.theaimsgroup.com/?l=php-generalw=2 STFM | http://www.php.net/manual/en/index.php STFW | http://www.google.com/search?q=php LAZY | http://mycroft.mozdev.org/download.html?name=PHPsubmitform=Find+search+plugins -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Understanding intval() and types conversion
Jochem Maas wrote: Bruce Douglas wrote: so... you're saying that 7.9 (repeating) is equal to 8.0 i say prove it.. as i recall the numbers might be for all practical purposes the same, they are in fact vastly different... so, prove your assertion... he did. you have to consider the two the same, given that there is always a limit to the level of precision - the alternative would be to say no 2 numbers are ever equal and that make for boring mathematics. No, no, no. I'm going MUCH farther than that. I've claimed (and proved) that: 7.999... (repeating decimal) IS EXACTLY EQUAL TO 8.000... (repeating decimal) with absolutely no computer, rounding, fudging, or nothing about being close involved. They are EQUAL. If you don't believe me, you merely need to convert a bunch of repeating decimals to fractions the same as any grade-school kid would do, and then you'll see. Honest! I'm not making this [bleep] up. :-) PS BS in Honors Mathematics from Notre Dame is my qualification... Though any 5th-grade teacher can prove it just as fast as I can. Faster, actually, as they go through it every year, and have the same disbelief from their students as I'm seeing here. Go find a 5th-grade teacher and ask 'em. :-) :-) :-) -- 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] Understanding intval() and types conversion
Richard Lynch wrote: Jochem Maas wrote: Bruce Douglas wrote: so... you're saying that 7.9 (repeating) is equal to 8.0 i say prove it.. as i recall the numbers might be for all practical purposes the same, they are in fact vastly different... so, prove your assertion... he did. you have to consider the two the same, given that there is always a limit to the level of precision - the alternative would be to say no 2 numbers are ever equal and that make for boring mathematics. No, no, no. I'm going MUCH farther than that. I've claimed (and proved) that: 7.999... (repeating decimal) IS EXACTLY EQUAL TO 8.000... (repeating decimal) somewhat-back-on-topic So knowing this... is the fractional math how the BC Math extension for PHP works? I'm really curious now. It certainly makes things more clear for me if this is the case. /somewhat-back-on-topic -- Teach a man to fish... NEW? | http://www.catb.org/~esr/faqs/smart-questions.html STFA | http://marc.theaimsgroup.com/?l=php-generalw=2 STFM | http://www.php.net/manual/en/index.php STFW | http://www.google.com/search?q=php LAZY | http://mycroft.mozdev.org/download.html?name=PHPsubmitform=Find+search+plugins -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Understanding intval() and types conversion
somewhat-back-on-topic So knowing this... is the fractional math how the BC Math extension for PHP works? I'm really curious now. It certainly makes things more clear for me if this is the case. /somewhat-back-on-topic Somewhat. I'd more describe it as: You pick how many decimal points you want BC_MATH to use, and BC_MATH will juggle the string representations of your numbers to keep that many decimals valid So if you choose 10 decimal points, BC_MATH will represent 0.999... as: 0.99 and 1 as 1.00 and then it will add/subtract/multiply with that many decimal points. For scientific usage, to keep your rounding correct you always use an extra decimal place than you are claiming as your accuracy, so that any round-off errors happen in a decimal place you're not pretending is correct. Note that all the operations happen as string-parsing, so you're basically limited to the size of your RAM for your strings to fit. And, as you might imagine, it is NOT going to be anything like as fast as using built-in operators, since it has to munge all the strings to get answers. If you're an astrophysicist or a nuclear researcher, you NEED that precision, though. YMMV (but not by more than N decimal places) :-) -- 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] Understanding intval() and types conversion
Jochem Maas wrote: Bruce Douglas wrote: so... you're saying that 7.9 (repeating) is equal to 8.0 i say prove it.. as i recall the numbers might be for all practical purposes the same, they are in fact vastly different... so, prove your assertion... he did. you have to consider the two the same, given that there is always a limit to the level of precision - the alternative would be to say no 2 numbers are ever equal and that make for boring mathematics. actually the underlying maths kinda makes my head spin :-) Calculus, or did I miss something here? In any case it was a cool link all the same and I enjoyed reading it. Float - int conversions have been a problem in every language that I've coded in. -- Teach a man to fish... NEW? | http://www.catb.org/~esr/faqs/smart-questions.html STFA | http://marc.theaimsgroup.com/?l=php-generalw=2 STFM | http://www.php.net/manual/en/index.php STFW | http://www.google.com/search?q=php LAZY | http://mycroft.mozdev.org/download.html?name=PHPsubmitform=Find+search+plugins -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Understanding intval() and types conversion
no.. he didn't. 8.0 = 8.0 7. (repeating) = 7. (repeating) 8.0 != 7. (repeating) now, if you want to get into conversations with regards to internal binary representations because of the limits of the register architecture in computers, then i might say the issue is the fact that the algorithms employed within the hardware are flawed... but let's not start screwing with basic math theory the statement that he uses where he states that there is a limit to the level of precision is lazy at best... (from a mathmatical perspective) or can i simply say, 15000 = 14,800 based upon my level of precision -bruce -Original Message- From: Jochem Maas [EMAIL PROTECTED] Sent: Jan 25, 2005 3:42 PM To: Bruce Douglas [EMAIL PROTECTED] Cc: [EMAIL PROTECTED], Jordi Canals [EMAIL PROTECTED], PHP List php-general@lists.php.net Subject: Re: [PHP] Understanding intval() and types conversion Bruce Douglas wrote: so... you're saying that 7.9 (repeating) is equal to 8.0 i say prove it.. as i recall the numbers might be for all practical purposes the same, they are in fact vastly different... so, prove your assertion... he did. you have to consider the two the same, given that there is always a limit to the level of precision - the alternative would be to say no 2 numbers are ever equal and that make for boring mathematics. actually the underlying maths kinda makes my head spin :-) -bruce -Original Message- From: Richard Lynch [EMAIL PROTECTED] Sent: Jan 25, 2005 2:57 PM To: Jochem Maas [EMAIL PROTECTED] Cc: Jordi Canals [EMAIL PROTECTED], PHP List php-general@lists.php.net Subject: Re: [PHP] Understanding intval() and types conversion my guess this is a round error issue - never noticed this problem before... this is a problem right? anybody? You can say it is a round error issue. You can not say it is a problem. You simply have to be aware that the float number which you think of as 8.... inside of PHP may well be represented as 7.... down in the guts of the machine. It may help you feel better about this if you recall from grade school mathematics, that, in point of fact: 7.999... is EXACTLY equal to 8.000... Those who doubt this fact are requested to remember how to convert decimals such as: 0.333... to fractions (1/3), and then apply that exact same method to 7.999... You will quickly find yourself with a fraction (72/9) which is precisely equal to 8/1 which is 8. EVERY floating point number, then, that ends in x000... has a second representation, exactly equal, which ends in (x-1)999... Bottom line, however you look at it, is that you can NEVER be 100% certain that a computer's float number will convert to what you expect in an integer because their internal representation simply does not allow for perfection. This is a feature, not a bug. :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Understanding intval() and types conversion
On Tue, 2005-01-25 at 18:29, Bruce Douglas wrote: so... you're saying that 7.9 (repeating) is equal to 8.0 I do believe that the limit as the number of repeating 9s aproaches infinity is indeed 8. Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Understanding intval() and types conversion
Maybe its time to kill this thread? I think Jason and Richard (others) have done a brilliant job of explaining how AND why, many thanks to them. Anyone not yet convinced should probably take a maths course ] (i.e. pay someone for the teachings ;-) rgds, Jochem Jason Barnett wrote: Bruce Douglas wrote: no.. he didn't. ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php