Jump to content

Conversion to integer in php


rajitha

Recommended Posts

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.8)*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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.