Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Stringification of FP numbers.

by gam3 (Curate)
on Jun 12, 2009 at 15:54 UTC ( #771006=perlquestion: print w/replies, xml ) Need Help??
gam3 has asked for the wisdom of the Perl Monks concerning the following question:

Can anyone explain to me why 0.0001 is stringified as "0.0001" but 0.00001 is stringified as "1e-05"?

This would make sense if "100000.0" was printed as 1e05, but is seems negative exponents are treated in a different and rather arbitrary way.

print 0.0001, " ", 0.00001, "\n"
-- gam3
A picture is worth a thousand words, but takes 200K.

Replies are listed 'Best First'.
Re: Stringification of FP numbers.
by ikegami (Pope) on Jun 12, 2009 at 15:57 UTC
    If you're not happy with the default, you can easily control the format using [s]printf
Re: Stringification of FP numbers.
by JavaFan (Canon) on Jun 12, 2009 at 16:24 UTC
    Well, people like to see 0.1 instead of 1e-1. But they prefer 1e-50 over 0.00000000000000000000000000000000000000000000000001. Saves space, and counting digits. Obviously, the more leading zero's the rather people see "scientific" notation.

    The happy medium (flip point) was set at 4 leading zeros.

Re: Stringification of FP numbers.
by ig (Vicar) on Jun 12, 2009 at 17:26 UTC

    The conversion is usually done by gconvert(), gcvt() or printf with %g format, depending on what Configure determined and, for example, on CentOS5 the %g format is described as:

    The double argument is converted in style f or e (or F or E for G conversions). The precision specifies the number of significant digits. If the precision is missing, 6 digits are given; if the precision is zero, it is treated as 1. Style e is used if the exponent from its conversion is less than -4 or greater than or equal to the precision. Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit.

    The emphasis is mine. This is consistent with the result you are seeing.

      I think that this is the answer I was looking for. It is dependent on the clib then if %g is being used.

      However %g in a C program gives this output:

      I guess I'll have have to get the source code and look. Thanks.
      -- gam3
      A picture is worth a thousand words, but takes 200K.
Re: Stringification of FP numbers.
by syphilis (Chancellor) on Jun 12, 2009 at 23:58 UTC
    This would make sense if "100000.0" was printed as 1e05

    Yes - I, too, would prefer to see the same rule applied to both leading and trailing zeroes. Unfortunately, that's not the way it's being done. Another way of avoiding the scientific notation (no matter how many leading zeroes) is to convert to a Math::BigFloat object:
    perl -MMath::BigFloat -wle "print Math::BigFloat->new(1e-10)"
    (Replace double quotes with single quotes on nix.)


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://771006]
Approved by wfsp
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2017-06-23 11:17 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (543 votes). Check out past polls.