%g format in printf

Chris Torek chris at mimsy.UUCP
Thu Sep 14 09:10:00 AEST 1989


>>..."1e+03".

In article <10832 at riks.csl.sony.co.jp> diamond at csl.sony.co.jp (Norman Diamond)
writes:
>Why does the exponent ... have a leading zero?

Hysterical Raisins, er, historical reasons.  Way back when, exponents were
always two digits.  Now they can be more, but not less.  In other words, it
is a misfeature retained for backwards compatibility.

(Personally, I think the exponent format should be controlled by another
optional field, something like `%.1.+02g', although this may be overly
hairy.  There seems to be no particularly good reason for the plus sign
either; `1e3' would do fine.)

As long as I have the ears, er, eyes, of the collective USENET entity,
I think I should digress for a bit.  When I said earlier that `the
number of significant digits is the number of digits written', I meant
that this rule, used by some of the scientific community, *should* be
used by *all* of the scientific community.  Some people will argue that
the number `0010' has only two significant digits---that the leading
zeros are not significant.  This then leads into a morass: how many
digits are significant in `0.1'?  How about `00.01'?  Do we ignore
leading zeros after decimal points?  What about the one just before the
decimal point?  The rules quickly get confusing: hard to work with,
hard to remember.  The other rule---all digits of the mantissa are
significant---is easy to remember, easy to work with, and allow all the
same numbers we could write before.  Instead of `0010 to two digits',
we write `1.0e1'; instead of `0.1 to 1 digit', we write `1e-1'; and so
forth.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.std.c mailing list