Re: IEEE 754 Floating Point

From: Oli Filth (oli_filth_at_eatspam.hotmail.com)
Date: 11/15/04

  • Next message: Barton: "Sablotron or SimpleXML (PHP5) ?"
    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


  • Next message: Barton: "Sablotron or SimpleXML (PHP5) ?"

    Relevant Pages

    • Re: Two Click disassembly/reassembly
      ... :4 to EAX on each iteration of that loop. ... currently writing a conversion tool. ... As you may know, some years ago, I wrote a text adventure game engine, for ... However, the 16 hours, or so, which I have spent writing the ...
      (alt.lang.asm)
    • Re: Macro for multiple Table --> Text conversion
      ... because the loop-ending condition of the For loop is evaluated only once. ... >> will stop the macro. ... so each conversion affects the ... >> Sub TablesToText() ...
      (microsoft.public.word.tables)
    • Re: How to speed up a code loop with INSERT INTO query?
      ... I start by converted the PDF file to a html file. ... Later, I can then loop through and parse these 800,000 strings into ... The problem I have is that the conversion of the text file, ... set rsEWB = currentdb.openrecordset ...
      (comp.databases.ms-access)
    • Re: Skybucks TryStrToUint64 (Version 0.03, register/simple version)
      ... Here is the main loop one more time by simply using the Borland/Common ... So a benchmark would have to prove which one is fastest;) ... conversion speeds take your pick and benchmark! ... conversion routine for some debugging purposes;) ...
      (alt.comp.lang.borland-delphi)
    • How to speed up a code loop with INSERT INTO query?
      ... Needing to import and parse data from a large PDF file into an Access ... I start by converted the PDF file to a html file. ... Later, I can then loop through and parse these 800,000 strings into ... The problem I have is that the conversion of the text file, ...
      (comp.databases.ms-access)