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?
How do you make two floats out of 104-bits?
Moderator: General Moderators
- 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?
If you want to use floating-point then you will lose information with that many bits. To keep everything you can use integer strings.kaisellgren wrote:...to two PHP floats. I need it to be perfect and not lose information.
Suggestion: convert the 104-bit number into a different base (hexadecimal would be easiest) then base_convert the two halves into decimal.
- 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?
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:If you want to use floating-point then you will lose information with that many bits.kaisellgren wrote:...to two PHP floats. I need it to be perfect and not lose information.
What I need is a float.tasairis wrote:To keep everything you can use integer strings.
Edit: I'm talking about IEEE 754 64-bit double precision which is what PHP uses.