Thanks for that. I won't be using it directly as I don't need its flexibility and I'll need to extend it for my extended precision numbers; but it made a nice reference.
This is my version for standard doubles:
sub fmt{
my $bin = unpack 'Q', pack 'd', $_[0];
my $sign = ( $bin & 0x8000000000000000 );
my $exp = ( $bin & 0x7FF0000000000000 ) >> 52; $exp -= 1023;
my $mant = ( $bin & 0x000FFFFFFFFFFFFF );
sprintf "%s0x1.%xp%d", ( $sign ? '-' : '' ), $mant, $exp;
}
Some outputs: C:\test>fmt -- -1/9
-0x1.c71c71c71c71cp-4
C:\test>fmt -- 1/9
0x1.c71c71c71c71cp-4
C:\test>fmt -- 1/3
0x1.5555555555555p-2
C:\test>fmt -- 1/11
0x1.745d1745d1746p-4
C:\test>fmt -- "( 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64 + 1/128 + 1/256
+ + 1/512 + 1/1024 + 1/2048 + 1/4096 + 1/8192 + 1/16384 + 1/32768 + 1/
+65536 + 1/131072 + 1/262144 + 1/524288 )"
0x1.ffffc00000000p-1
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.
|