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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
"%.17g" formatting

I was surprised to see 16 digits after the decimal point in a "%.17" notation. Per perl's sprintf docs:

%g    a floating-point number, in %e or %f notation

But that's not what actually happens in Perl:

C:>perl -MConfig -le "print +5.3130431399669280e-34; printf qq($_\n), ++5.3130431399669280e-34 for qw/%.17f %.17e %.17g/; print qq($^V\t|\t$ +Config{myuname})" 5.31304313996693e-34 0.00000000000000000 5.31304313996692843e-34 5.3130431399669284e-34 v5.30.0 | Win32 strawberry-perl 5.30.0.1 #1 Thu May 23 12:20:46 +2019 x64

... In this instance, "%.17g" is neither equivalent to "%.17f" (17 digits after the fixed decimal point) nor to "%.17e" (17 digits after the floating decimal point).

I tried on an ancient linux machine with v5.8.5, with similar results:

% perl -MConfig -le 'print +5.3130431399669280e-34; printf qq($_\n), + +5.3130431399669280e-34 for qw/%.17f %.17e %.17g/; print qq($]\t|\t$Co +nfig{myuname})' 5.31304313996693e-34 0.00000000000000000 5.31304313996692843e-34 5.3130431399669284e-34 5.008005 | linux builder4.centos.org 2.6.9-55.0.9.elsmp # +1 smp thu sep 27 18:27:41 edt 2007 i686 i686 i386 gnulinux

The sprintf docs have used that phrasing at least as far back as v5.005 . Am I misunderstanding what I'm seeing? or has perl documentation about %g been wrong for that long?


In reply to TANGENT = %.17g formatting (Re^8:int() function) by pryrt
in thread int() function by geoperl

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-18 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found