How do you make two floats out of 104-bits?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

How do you make two floats out of 104-bits?

Post by kaisellgren »

Hi everybody,

I am playing around with maths, bits and such. Now for academic purposes, I want to know how to convert some data that is exactly 104-bits (or in other words 13 bytes) to two PHP floats. I need it to be perfect and not lose information. So, the function should be capable of doing exactly two floats with 2^52 different float values out of the 104-bits.

PHP uses http://en.wikipedia.org/wiki/Double_pre ... int_format and thus a float can have 2^52 different possibilities.

If it were 2^56 and not 2^52, I could just substr() the first 7 bytes, loop them and sum with ord(), and divide the final sum by 2^56 and have my float. However, the precision is 2^52, so I need to get first 7 bytes and rid of the 4-bits and use them for the second float and what I exactly need to do is a bit unclear to me. Help?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do you make two floats out of 104-bits?

Post by requinix »

kaisellgren wrote:...to two PHP floats. I need it to be perfect and not lose information.
If you want to use floating-point then you will lose information with that many bits. To keep everything you can use integer strings.

Suggestion: convert the 104-bit number into a different base (hexadecimal would be easiest) then base_convert the two halves into decimal.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: How do you make two floats out of 104-bits?

Post by kaisellgren »

tasairis wrote:
kaisellgren wrote:...to two PHP floats. I need it to be perfect and not lose information.
If you want to use floating-point then you will lose information with that many bits.
Are you sure -- according to Wikipedia, a float has 52-bits of significand precision, holds up 52-bits of information. Therefore, two floats can be made out of 104-bits without losing information I presume.
tasairis wrote:To keep everything you can use integer strings.
What I need is a float.

Edit: I'm talking about IEEE 754 64-bit double precision which is what PHP uses.
Post Reply