I've used this:

sub double_to_bin { my $n = shift; my ( $s, $e, $m ) = unpack "a1 a11 a52", unpack "B64", pack "d>", $n; $e = oct( "0b$e" ); if ( $e == 0x7FF ) { if ( $m =~ /^0+\z/ ) { return $s ? "-inf" : "+inf" ; } else { return "nan"; } } my $n_bin = ( $e ? "1" : "" ) . $m; $e -= 1023; if ( $e < 0 ) { $n_bin = "0." . ( "0" x ( -$e - 1 ) ) . $n_bin; } else { my $z = ( $e + 1 ) - length( $n_bin ); if ( $z >= 0 ) { $n_bin .= "0" x $z; } else { substr( $n_bin, $e+1, 0, "." ); } } $n_bin =~ s/^0+(?=[01])//; if ( $n_bin =~ /\./ ) { #$n_bin =~ s/0+\z//; $n_bin =~ s/\.\z//; } $n_bin = ( $s ? "-" : "" ) . $n_bin; return $n_bin; }

Handles subnormals, infinities and NaNs. [Updated to handle the latter two.]


To print the exact value in decimal, you need up to 1074 decimal places (%.1074f) or 751 in scientific notation (%.751g) for IEEE doubles.

$ perl -e'printf "%.751g\n", unpack "d", pack "Q", 1' 4.94065645841246544176568792868221372365059802614324764425585682500675 +507270208751865299836361635992379796564695445717730926656710355939796 +398774796010781878126300713190311404527845817167848982103688718636056 +998730723050006387409153564984387312473397273169615140031715385398074 +126238565591171026658556686768187039560310624931945271591492455329305 +456544401127480129709999541931989409080416563324524757147869014726780 +159355238611550134803526493472019379026810710749170333222684475333572 +083243193609238289345836806010601150616980975307834227731832924790498 +252473077637592724787465608477820373446969953364701797267771758512566 +055119913150489110145103786273816725095583738973359899366480994116420 +5702637090279242767544565229087538682506419718265533447265625e-324

In reply to Re: Introspection into floats/NV by ikegami
in thread Introspection into floats/NV by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.