rajitha Posted December 25, 2008 Share Posted December 25, 2008 echo (int) ((0.1 + 0.7) * 10); Explain me how it works? Quote Link to comment https://forums.phpfreaks.com/topic/138352-conversion-to-integer-in-php/ Share on other sites More sharing options...
rhodesa Posted December 25, 2008 Share Posted December 25, 2008 what do you need explained? the (int) forces it to an integer. Quote Link to comment https://forums.phpfreaks.com/topic/138352-conversion-to-integer-in-php/#findComment-723453 Share on other sites More sharing options...
corbin Posted December 25, 2008 Share Posted December 25, 2008 I'm guessing you're question is how does this happen: C:\Users\Corbin>php -r "echo ((0.1 + 0.7)*10);" 8 C:\Users\Corbin>php -r "echo (int)((0.1 + 0.7)*10);" 7 C:\Users\Corbin>php -r "echo (int)((0.1 + 0.*10);" 9 (Yeah, that last one really messes with you, doesn't it?) Binary is a bitch, is how it happens. 00 00 00 41 is the representation in hexadecimal of what ((0.1 + 0.7)*10) would be in memory as a float. (Unless my math just went to hell, and I wouldn't doubt that it did.) Single precision floating digits are stored like this: p eeeeeeee sssssssssssssssssssssss Then they are calculated, in human readable format, as: (-1)^p * 2^e * (1.sssssssssssssssssssssss) e in that context would actually be the value of eeeeeeee - 127. (127 is added so that a greater range of exponents can be handled.) Anyway, I don't feel like getting down and dirty (and by dirty I really mean mathy), but to put it shortly, floats are weird, and converting float to int can be weird. 00 00 00 41 is actually little endian, so to put it in the format from earlier, it would be: 41 00 00 00 Which would be 0 10000010 00000000000000000000000 Which would give you (-1)^0 * 2^130-127 * 1.0 Or exactly 8, but somewhere in converting float to int, the computer loses a tiny fraction which makes it round down to 7. Integers are entirely different in binary. Integers in binary are straight up base 2, just like decimal is base 10. 1245 = 1*10^3 + 2*10^2 + 4*10^1 + 4*10^0 Similarly 111 in binary = 7 in decimal because 1*2^2 + 1*2^1 + 1*2^0 = 7 So, hopefully you can see where a computer could run into problems converting the two ;p. Quote Link to comment https://forums.phpfreaks.com/topic/138352-conversion-to-integer-in-php/#findComment-723475 Share on other sites More sharing options...
rajitha Posted December 25, 2008 Author Share Posted December 25, 2008 You would expect that the expression (int)((0.1 + 0.7) * 10) would evaluate to 8 (and, in fact, if you print it out with the integer conversion it evaluates to 7. Could anyone explain me how it echoes 7? Quote Link to comment https://forums.phpfreaks.com/topic/138352-conversion-to-integer-in-php/#findComment-723482 Share on other sites More sharing options...
corbin Posted December 25, 2008 Share Posted December 25, 2008 It must be a left over of how either PHP or C converts floats to integers. Oddly enough, in C++: float f = (0.1+0.7)*10; cout << ((int) f); Gives 8.... So, it has to be something to do with PHP or C. (Well, compiles could act differently, or so on, but chances are, that's not it.) It essentially happens because of the conversion form. I told you the [single precision] float format, and I told you the integer format. Surely you can see why there would be problems. I must agree with you though. It's quite odd that it would give 7 when the float representation is exact. I'm sure if I read for a few minutes I could find how computer convert float to int, but.... lol Quote Link to comment https://forums.phpfreaks.com/topic/138352-conversion-to-integer-in-php/#findComment-723623 Share on other sites More sharing options...
rhodesa Posted December 25, 2008 Share Posted December 25, 2008 just use this instead: echo round((0.1 + 0.7)*10,0); Quote Link to comment https://forums.phpfreaks.com/topic/138352-conversion-to-integer-in-php/#findComment-723626 Share on other sites More sharing options...
rhodesa Posted December 25, 2008 Share Posted December 25, 2008 p.s. - PHP covers this here: http://us.php.net/manual/en/language.types.integer.php#language.types.integer.casting Quote Link to comment https://forums.phpfreaks.com/topic/138352-conversion-to-integer-in-php/#findComment-723628 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.