Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Exploring IEEE754 floating point bit patterns.

by roboticus (Canon)
on Jul 28, 2012 at 12:05 UTC ( #984189=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: Exploring IEEE754 floating point bit patterns.
Download Code
Re^2: Exploring IEEE754 floating point bit patterns.
by BrowserUk (Pope) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://984189]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-11-27 18:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (186 votes), past polls