http://www.perlmonks.org?node_id=984189


in reply to Exploring IEEE754 floating point bit patterns.

4.120300081267965e+103:

Pretty nifty!

2.914708259813678e+257

When your only tool is a hammer, all problems look like your thumb.

P.S. Too bad my perl isn't compiled for 64 bits. I had to use the hack below, as my (un)pack-fu isn't strong. (If I were going to redo any of perls features, it would probably be to make a comprehensible pack/unpack facility.)

$ cat t.cpp #include <stdio.h> #include <string.h> union f { char s[32]; double d; } g; int main(int, char **s) { memcpy(g.s, s[1], sizeof(g)); printf("%23.16g\n", g.d); }

I think I'll play around with it to make it accept bitstrings.

Replies are listed 'Best First'.
Re^2: Exploring IEEE754 floating point bit patterns.
by BrowserUk (Patriarch) on Jul 29, 2012 at 03:11 UTC
    Too bad my perl isn't compiled for 64 bits.

    Try this version:

    #! perl -slw use strict; use constant { POS_ZERO => '0'.'00000000000'.'0000'.'00000000'.'00000000'.'000 +00000'.'00000000'.'00000000'.'00000000', POS_DENORM_1ST => '0'.'00000000000'.'0000'.'00000000'.'00000000'.'000 +00000'.'00000000'.'00000000'.'00000001', POS_DENORM_LST => '0'.'00000000000'.'1111'.'11111111'.'11111111'.'111 +11111'.'11111111'.'11111111'.'11111111', POS_NORM_1ST => '0'.'00000000001'.'0000'.'00000000'.'00000000'.'000 +00000'.'00000000'.'00000000'.'00000000', POS_NORM_LST => '0'.'11111111110'.'1111'.'11111111'.'11111111'.'111 +11111'.'11111111'.'11111111'.'11111111', POS_INF => '0'.'11111111111'.'0000'.'00000000'.'00000000'.'000 +00000'.'00000000'.'00000000'.'00000000', POS_SNAN_1ST => '0'.'11111111111'.'0000'.'00000000'.'00000000'.'000 +00000'.'00000000'.'00000000'.'00000001', POS_SNAN_LST => '0'.'11111111111'.'0111'.'11111111'.'11111111'.'111 +11111'.'11111111'.'11111111'.'11111111', POS_QNAN_1ST => '0'.'11111111111'.'1000'.'00000000'.'00000000'.'000 +00000'.'00000000'.'00000000'.'00000000', POS_QNAN_LST => '0'.'11111111111'.'1111'.'11111111'.'11111111'.'111 +11111'.'11111111'.'11111111'.'11111111', NEG_ZERO => '1'.'00000000000'.'0000'.'00000000'.'00000000'.'000 +00000'.'00000000'.'00000000'.'00000000', NEG_DENORM_1ST => '1'.'00000000000'.'0000'.'00000000'.'00000000'.'000 +00000'.'00000000'.'00000000'.'00000001', NEG_DENORM_LST => '1'.'00000000000'.'1111'.'11111111'.'11111111'.'111 +11111'.'11111111'.'11111111'.'11111111', NEG_NORM_1ST => '1'.'00000000001'.'0000'.'00000000'.'00000000'.'000 +00000'.'00000000'.'00000000'.'00000000', NEG_NORM_LST => '1'.'11111111110'.'1111'.'11111111'.'11111111'.'111 +11111'.'11111111'.'11111111'.'11111111', NEG_INF => '1'.'11111111111'.'0000'.'00000000'.'00000000'.'000 +00000'.'00000000'.'00000000'.'00000000', NEG_SNAN_1ST => '1'.'11111111111'.'0000'.'00000000'.'00000000'.'000 +00000'.'00000000'.'00000000'.'00000001', NEG_SNAN_LST => '1'.'11111111111'.'0111'.'11111111'.'11111111'.'111 +11111'.'11111111'.'11111111'.'11111111', NEG_IND => '1'.'11111111111'.'1000'.'00000000'.'00000000'.'000 +00000'.'00000000'.'00000000'.'00000000', NEG_QNAN_1ST => '1'.'11111111111'.'1000'.'00000000'.'00000000'.'000 +00000'.'00000000'.'00000000'.'00000001', NEG_QNAN_LST => '1'.'11111111111'.'1111'.'11111111'.'11111111'.'111 +11111'.'11111111'.'11111111'.'11111111', }; sub bitsToDouble{ unpack 'd', pack 'b64', scalar reverse $_[0] } sub bitsToInts{ reverse unpack 'VV', pack 'b64', scalar reverse $_[0 +] } printf "%23.16g : %08x%08x\n", bitsToDouble( $_ ), bitsToInts( $_ ) for POS_ZERO, POS_DENORM_1ST, POS_DENORM_LST, POS_NORM_1ST, POS_NORM_L +ST, POS_INF, POS_SNAN_1ST, POS_SNAN_LST, POS_QNAN_1ST, POS_QNAN_LST, NEG_ZERO, NEG_DENORM_1ST, NEG_DENORM_LST, NEG_NORM_1ST, NEG_NORM_L +ST, NEG_INF, NEG_SNAN_1ST, NEG_SNAN_LST, NEG_IND, NEG_QNAN_1ST, NEG_QNAN_LST;

    Outputs:

    C:\test>\perl32\bin\perl ieee-32b.pl 0 : 0000000000000000 4.940656458412465e-324 : 0000000000000001 2.225073858507201e-308 : 000fffffffffffff 2.225073858507201e-308 : 0010000000000000 1.797693134862316e+308 : 7fefffffffffffff 1.#INF : 7ff0000000000000 1.#QNAN : 7ff0000000000001 1.#QNAN : 7ff7ffffffffffff 1.#QNAN : 7ff8000000000000 1.#QNAN : 7fffffffffffffff -0 : 8000000000000000 -4.940656458412465e-324 : 8000000000000001 -2.225073858507201e-308 : 800fffffffffffff -2.225073858507201e-308 : 8010000000000000 -1.797693134862316e+308 : ffefffffffffffff -1.#INF : fff0000000000000 -1.#QNAN : fff0000000000001 -1.#QNAN : fff7ffffffffffff -1.#IND : fff8000000000000 -1.#QNAN : fff8000000000001 -1.#QNAN : ffffffffffffffff

    Seems that the compiler used to build AS 5.8.9 didn't distinguish between SNaN & QNaN. But the oddball -1.#IND was there. Perhaps its presence in the current compiler is legacy code.

    The C version above should compile fine for 32-bit.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      BrowserUk:

      That one worked nicely:

      marco@Boink:~ $ perl 984255_bitvec_floats_2.pl 0 : 0000000000000000 4.940656458412465e-324 : 0000000000000001 2.225073858507201e-308 : 000fffffffffffff 2.225073858507201e-308 : 0010000000000000 1.797693134862316e+308 : 7fefffffffffffff inf : 7ff0000000000000 nan : 7ff0000000000001 nan : 7ff7ffffffffffff nan : 7ff8000000000000 nan : 7fffffffffffffff -0 : 8000000000000000 -4.940656458412465e-324 : 8000000000000001 -2.225073858507201e-308 : 800fffffffffffff -2.225073858507201e-308 : 8010000000000000 -1.797693134862316e+308 : ffefffffffffffff -inf : fff0000000000000 -nan : fff0000000000001 -nan : fff7ffffffffffff -nan : fff8000000000000 -nan : fff8000000000001 -nan : ffffffffffffffff

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.