#!/usr/bin/perl use warnings; use strict; use lib '.'; use Tools; # local == Data::IEEE754::Tools use constant { POS_ZERO => '0'.'00000000000'.'0000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000', POS_DENORM_1ST => '0'.'00000000000'.'0000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000001', POS_DENORM_LST => '0'.'00000000000'.'1111'.'11111111'.'11111111'.'11111111'.'11111111'.'11111111'.'11111111', POS_NORM_1ST => '0'.'00000000001'.'0000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000', POS_NORM_LST => '0'.'11111111110'.'1111'.'11111111'.'11111111'.'11111111'.'11111111'.'11111111'.'11111111', POS_INF => '0'.'11111111111'.'0000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000', POS_SNAN_1ST => '0'.'11111111111'.'0000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000001', POS_SNAN_LST => '0'.'11111111111'.'0111'.'11111111'.'11111111'.'11111111'.'11111111'.'11111111'.'11111111', POS_IND => '0'.'11111111111'.'1000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000', POS_QNAN_1ST => '0'.'11111111111'.'1000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000001', POS_QNAN_LST => '0'.'11111111111'.'1111'.'11111111'.'11111111'.'11111111'.'11111111'.'11111111'.'11111111', NEG_ZERO => '1'.'00000000000'.'0000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000', NEG_DENORM_1ST => '1'.'00000000000'.'0000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000001', NEG_DENORM_LST => '1'.'00000000000'.'1111'.'11111111'.'11111111'.'11111111'.'11111111'.'11111111'.'11111111', NEG_NORM_1ST => '1'.'00000000001'.'0000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000', NEG_NORM_LST => '1'.'11111111110'.'1111'.'11111111'.'11111111'.'11111111'.'11111111'.'11111111'.'11111111', NEG_INF => '1'.'11111111111'.'0000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000', NEG_SNAN_1ST => '1'.'11111111111'.'0000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000001', NEG_SNAN_LST => '1'.'11111111111'.'0111'.'11111111'.'11111111'.'11111111'.'11111111'.'11111111'.'11111111', NEG_IND => '1'.'11111111111'.'1000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000', NEG_QNAN_1ST => '1'.'11111111111'.'1000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000000'.'00000001', NEG_QNAN_LST => '1'.'11111111111'.'1111'.'11111111'.'11111111'.'11111111'.'11111111'.'11111111'.'11111111', }; my @list = ( POS_ZERO, POS_DENORM_1ST, POS_DENORM_LST, POS_NORM_1ST, POS_NORM_LST, POS_INF, POS_SNAN_1ST, POS_SNAN_LST, POS_IND, POS_QNAN_1ST, POS_QNAN_LST, NEG_ZERO, NEG_DENORM_1ST, NEG_DENORM_LST, NEG_NORM_1ST, NEG_NORM_LST, NEG_INF, NEG_SNAN_1ST, NEG_SNAN_LST, NEG_IND, NEG_QNAN_1ST, NEG_QNAN_LST ); sub bitsToDouble{ unpack 'd', pack 'b64', scalar reverse $_[0] } sub bitsToInts{ reverse unpack 'VV', pack 'b64', scalar reverse $_[0] } sub doubleToBits{ scalar reverse unpack 'b64', pack 'd', $_[0] } printf "%23.16g : %08x%08x >=there-and-back-again=> %08x%08x\n", bitsToDouble( $_ ), bitsToInts( $_ ), bitsToInts( doubleToBits( bitsToDouble( $_ ) ) ) for @list; print "\n"; use Test::More tests => 22; is( doubleToBits( bitsToDouble( $_ ) ) , $_ , "bitsToDouble(doubleToBits($_))" ) for @list; #### 0 : 0000000000000000 >=there-and-back-again=> 0000000000000000 4.940656458412465e-324 : 0000000000000001 >=there-and-back-again=> 0000000000000001 2.225073858507201e-308 : 000fffffffffffff >=there-and-back-again=> 000fffffffffffff 2.225073858507201e-308 : 0010000000000000 >=there-and-back-again=> 0010000000000000 1.797693134862316e+308 : 7fefffffffffffff >=there-and-back-again=> 7fefffffffffffff Inf : 7ff0000000000000 >=there-and-back-again=> 7ff0000000000000 NaN : 7ff0000000000001 >=there-and-back-again=> 7ff8000000000001 NaN : 7ff7ffffffffffff >=there-and-back-again=> 7fffffffffffffff NaN : 7ff8000000000000 >=there-and-back-again=> 7ff8000000000000 NaN : 7ff8000000000001 >=there-and-back-again=> 7ff8000000000001 NaN : 7fffffffffffffff >=there-and-back-again=> 7fffffffffffffff -0 : 8000000000000000 >=there-and-back-again=> 8000000000000000 -4.940656458412465e-324 : 8000000000000001 >=there-and-back-again=> 8000000000000001 -2.225073858507201e-308 : 800fffffffffffff >=there-and-back-again=> 800fffffffffffff -2.225073858507201e-308 : 8010000000000000 >=there-and-back-again=> 8010000000000000 -1.797693134862316e+308 : ffefffffffffffff >=there-and-back-again=> ffefffffffffffff -Inf : fff0000000000000 >=there-and-back-again=> fff0000000000000 NaN : fff0000000000001 >=there-and-back-again=> fff8000000000001 NaN : fff7ffffffffffff >=there-and-back-again=> ffffffffffffffff NaN : fff8000000000000 >=there-and-back-again=> fff8000000000000 NaN : fff8000000000001 >=there-and-back-again=> fff8000000000001 NaN : ffffffffffffffff >=there-and-back-again=> ffffffffffffffff ... not ok 7 - bitsToDouble(doubleToBits(0111111111110000000000000000000000000000000000000000000000000001)) # Failed test 'bitsToDouble(doubleToBits(0111111111110000000000000000000000000000000000000000000000000001))' # at ...\buk.pl line 52. # got: '0111111111111000000000000000000000000000000000000000000000000001' # expected: '0111111111110000000000000000000000000000000000000000000000000001' not ok 8 - bitsToDouble(doubleToBits(0111111111110111111111111111111111111111111111111111111111111111)) # Failed test 'bitsToDouble(doubleToBits(0111111111110111111111111111111111111111111111111111111111111111))' # at ...\buk.pl line 52. # got: '0111111111111111111111111111111111111111111111111111111111111111' # expected: '0111111111110111111111111111111111111111111111111111111111111111' ... not ok 18 - bitsToDouble(doubleToBits(1111111111110000000000000000000000000000000000000000000000000001)) # Failed test 'bitsToDouble(doubleToBits(1111111111110000000000000000000000000000000000000000000000000001))' # at ...\buk.pl line 52. # got: '1111111111111000000000000000000000000000000000000000000000000001' # expected: '1111111111110000000000000000000000000000000000000000000000000001' not ok 19 - bitsToDouble(doubleToBits(1111111111110111111111111111111111111111111111111111111111111111)) # Failed test 'bitsToDouble(doubleToBits(1111111111110111111111111111111111111111111111111111111111111111))' # at ...\buk.pl line 52. # got: '1111111111111111111111111111111111111111111111111111111111111111' # expected: '1111111111110111111111111111111111111111111111111111111111111111' ...