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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.