Re: IEEE 754 Floating Point
From: ShipiboConibo (shipiboconibo_at_[no_spam)
Date: 11/15/04
- Next message: tHatDudeUK: "Newbie q: Make user select "yes" to advance to specified URL"
- Previous message: ShipiboConibo: "Re: How to pass a a string containing & in php url ?"
- In reply to: Oli Filth: "Re: IEEE 754 Floating Point"
- Next in thread: Oli Filth: "Re: IEEE 754 Floating Point"
- Reply: Oli Filth: "Re: IEEE 754 Floating Point"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 14 Nov 2004 22:32:09 -0500
Oli Filth wrote:
> ShipiboConibo wrote:
>
>> I am trying to convert 32bit HEX values into IEEE 754 floating point
>> values. For example, when 'C45F82ED' is converted using this web page
>> http://babbage.cs.qc.edu/courses/cs341/IEEE-754hex32.html it comes out
>> to '-894.04572'. Yet, I can't seem to reproduce that result with any
>> related php functions...
>>
>> I tossed this script together from what I could understand after
>> reading this page about IEEE 754 floating point, but it doesn't seem
>> to be working for my values
>> .
>> http://www.duke.edu/~twf/cps104/floating.html
>>
>> <?php
>> $bin = decbin(hexdec('C45F82ED'));
>> $sign = bindec(substr($bin, 0, 1));
>> $exp = bindec(substr($bin, 1, 8)) - 127;
>> $man = 1 . substr($bin, 9, 23);
>> for ($i = 0, $c = 0; $i > -24; $i--, $c++) {
>> $pos = substr($man, $c, 1);
>> if ($pos == 1) {
>> $dec += bcpow(2, $i, 12) * bcpow(2, 2, 12);
>> }
>> }
>> if ($sign == 1) {
>> $dec = -$dec;
>> }
>> echo floatval($dec) . "E" . $exp;
>> ?>
>>
>> Oddly enough, this example works when using the value 'C0B40000',
>> which is used as an example on the previously mentioned IEEE FP site,
>> but not when using any of my own HEX values.
>>
>> Just FYI, the HEX data is coefficients stored in the eeprom of a
>> pressure transmitter. I am trying to toss together a script to allow
>> one to easily upload the coefficient dump file and have the hex values
>> converted, and also perform the mathematical modeling calculations on
>> a website.
>>
>> Any help is greatly appreciated!
>>
>> Thanks,
>>
>> Adam
>
>
>
> First thing I always do if some complicated sequence of functions are
> being used is to echo each result in turn. If you do this, you'll find
> that for whatever reason, decbin() is not working.
>
> Using baseconvert($n, 16, 2) instead, and changing and simplifying quite
> a lot of your code, I came up with:
>
> <?php
>
> $strHex = "C45F82ED";
>
> $bin = base_convert($strHex, 16, 2);
> $sign = substr($bin, 0, 1);
> $exp = base_convert(substr($bin, 1, 8), 2, 10) - 127;
> $man = substr($bin, 9, 23);
>
> $dec = 1;
> for ($i = 0, $f = 0.5; $i < 23; $i++, $f /= 2)
> {
> $dec += $man[$i] ? $f : 0;
> }
> $dec *= pow(2, $exp);
> $dec *= $sign ? -1 : 1;
>
> echo "\$dec = " . $dec . "<BR>\n";
>
> ?>
>
> This seems to give the right values.
>
> Hope this helps,
> Oli
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
- Next message: tHatDudeUK: "Newbie q: Make user select "yes" to advance to specified URL"
- Previous message: ShipiboConibo: "Re: How to pass a a string containing & in php url ?"
- In reply to: Oli Filth: "Re: IEEE 754 Floating Point"
- Next in thread: Oli Filth: "Re: IEEE 754 Floating Point"
- Reply: Oli Filth: "Re: IEEE 754 Floating Point"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]