Click to See Complete Forum and Search --> : Float to integer conversion.


bokeh
08-13-2007, 10:43 AM
echo (int)143133271933; // 1399351165 How is that conversion done? What is the formula?

knowj
08-13-2007, 10:49 AM
http://privacy.cs.cmu.edu/courses/java1/lectures/lecture10/sld084.htm

i don't know if that will be any help. i tried to get my head around it but its a bit confusing :S

it might be off track

ellisgl
08-13-2007, 10:54 AM
echo intval(143133271933);
of course the problem is that if it's running in 32 bit mode the max is:
2147483647 for a signed integer.

On a 64 bit system:
9223372036854775807 for a signed integer.


Why you are getting 1399351165 is beyoned me.

NogDog
08-13-2007, 10:55 AM
You've probably exceeded the max integer size for your platform. To check:

echo PHP_INT_MAX; // I get 2147483647 on my PC

MrCoder
08-13-2007, 10:58 AM
I think the problem is that an "int" in PHP cannot store such a large number.

Hence the inconstancy when type casting.

Try..


$i = (int)1399351165;
$i += 1;
echo $i;

bokeh
08-13-2007, 11:27 AM
Hi all, thanks for your answers. MrCoder it's not a problem I was just trying to get to the bottom of the conversion.You've probably exceeded the max integer size for your platform. To check:

echo PHP_INT_MAX; // I get 2147483647 on my PC
Hi Nogdog, your result is not down to the platform although it would be if the platform didn't support 32 bit integers. The result you have is the biggest signed integer it is possible to hold in a 32 bit word (PHP or C integer). Here's how it is formed:2^32 = 4294967296

// divide by 2 to allow for positive and negative values
4294967296/2 = 2147483648

// less one slot to allow for 0
2147483648 - 1 = 2147483647
Which is your result.

What I don't understand though is how the conversion is done.

Also can everyone run the example I posted above to see if they return the same result.

knowj
08-13-2007, 12:17 PM
i got the same result as you.

NogDog
08-13-2007, 03:10 PM
It's just a case of the left-most 7 bits being dropped to get it down to 31 bits (32 minus the plus/minus bit):

<?php
header('Content-Type: text/plain');
// Binary value of 143133271933:
echo "10000101010011011010000110011101111101\n";
// See what PHP does with it:
echo ' ' . decbin((int)143133271933);

Output:

10000101010011011010000110011101111101
1010011011010000110011101111101

bokeh
08-13-2007, 06:43 PM
Thanks!

It works in hex too!

143133271933 = 215368677d
1399351165 = 5368677d