/    Sign up×
Community /Pin to ProfileBookmark

Float to integer conversion.

[code=php]echo (int)143133271933; // 1399351165[/code]

How is that conversion done? What is the formula?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@knowjAug 13.2007 — 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
Copy linkTweet thisAlerts:
@ellisglAug 13.2007 — 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.
Copy linkTweet thisAlerts:
@NogDogAug 13.2007 — You've probably exceeded the max integer size for your platform. To check:
[code=php]
echo PHP_INT_MAX; // I get 2147483647 on my PC
[/code]
Copy linkTweet thisAlerts:
@MrCoderAug 13.2007 — I think the problem is that an "int" in PHP cannot store such a large number.

Hence the inconstancy when type casting.

Try..

<i>
</i>$i = (int)1399351165;
$i += 1;
echo $i;
Copy linkTweet thisAlerts:
@bokehauthorAug 13.2007 — 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:
[code=php]
echo PHP_INT_MAX; // I get 2147483647 on my PC
[/code]
[/QUOTE]
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:[code=php]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[/code]

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.
Copy linkTweet thisAlerts:
@knowjAug 13.2007 — i got the same result as you.
Copy linkTweet thisAlerts:
@NogDogAug 13.2007 — 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):
[code=php]
<?php
header('Content-Type: text/plain');
// Binary value of 143133271933:
echo "10000101010011011010000110011101111101n";
// See what PHP does with it:
echo ' ' . decbin((int)143133271933);
[/code]

Output:
<i>
</i>10000101010011011010000110011101111101
1010011011010000110011101111101
Copy linkTweet thisAlerts:
@bokehauthorAug 13.2007 — Thanks!

It works in hex too!

[CODE]143133271933 = 215368677d
1399351165 = 5368677d[/CODE]
×

Success!

Help @bokeh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...