Page 1 of 1

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

Posted: Sun Sep 12, 2010 6:18 am
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?

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

Posted: Sun Sep 12, 2010 7:23 am
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.

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

Posted: Sun Sep 12, 2010 9:00 am
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.