Re: IEEE 754 Floating Point
From: Oli Filth (oli_filth_at_eatspam.hotmail.com)
Date: 11/15/04
- Previous message: lallous: "Re: Newbie q: Make user select "yes" to advance to specified URL"
- In reply to: ShipiboConibo: "Re: IEEE 754 Floating Point"
- Next in thread: ShipiboConibo: "Re: IEEE 754 Floating Point"
- Reply: ShipiboConibo: "Re: IEEE 754 Floating Point"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 15 Nov 2004 12:41:40 GMT
ShipiboConibo wrote:
> THANK YOU! You have no idea how glad I am to get this working! I mostly
> know PHP as relating to web design, but I had managed to get myself into
> a situation that kind of over shot my expertise I guess, haha.
>
> It seemed like this line concerning the padding was the main solution to
> why things worked inconstantly before:
>
> $bin = str_pad(base_convert($strHex, 16, 2), 32, "0", STR_PAD_LEFT);
>
> Thanks a lot for simplifying my code too! If you get time, would you
> mind elaborating on exactly how this line works?
>
> $dec = $man * pow(2, $exp - 23) * ($sign ? -1 : 1);
>
> It blew my mind you did it all without the for loop, I'd just like to
> understand how it is working fully though. Don't want to ask too much,
> I'm happy that it just works, but if you get a minute...
>
> Thanks again,
>
> Adam
>
In your original code, your loop was creating the decimal mantissa by taking
each bit of the binary mantissa in turn, multiplying it by the relevant power of
2, and then summing the results, i.e. along the lines of:
22
$dec = Sum($man[i] * (1 << i))
i=0
However, the string $man is already in that form, just in binary rather than
decimal. So you can do the conversion by converting $man from binary to decimal
using bindec() and adding 2^22 (the equivalent of the implied "1." in binary),
hence the line:
$man = (2 << 22) + bindec(substr($bin, 9, 23));
This gives an answer that's 2^23 times to big, so you need to scale down by a
factor of 2^23, which can be combined with scaling up by the exponential value.
The sign conversion is done on the same line by multiplying by -1 when $sign =
1, hence:
$dec = $man * pow(2, $exp - 23) * ($sign ? -1 : 1);
\___/ \_______________/ \______________/
| | |
| | |
| scale by 2^$exp convert to -ve
| ------ if $sign = 1
| 2^23
|
mantissa * 2^23
I hope this comes out OK on your screen!
Oli
- Previous message: lallous: "Re: Newbie q: Make user select "yes" to advance to specified URL"
- In reply to: ShipiboConibo: "Re: IEEE 754 Floating Point"
- Next in thread: ShipiboConibo: "Re: IEEE 754 Floating Point"
- Reply: ShipiboConibo: "Re: IEEE 754 Floating Point"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|