[comp.std.c] %g format in printf

daniels@grue.dec.com (Bradford R. Daniels) (09/06/89)

What should the default number of significant digits be for the
%g format specifier in printf?  The standard says that an explicit
0 should be treated as a 1, but doesn't say anything about what to
do if no precision is specified.

Right now, the VAX C RTL uses 6 as the default precision.  This
seems reasonable, since 6 is the default precision for the %e
and %f specifiers.  However, precision has a different meaning
with %g than with those other specifiers.  Is VAXCRTL's current
behavior correct?

Also, the definition of significant digits I learned in my high
school science classes says that if I am asked to print out 1024
with 1 significant digit, I should get 1000 printed out.  Is that
correct?

Thanks,
- Brad

-----------------------------------------------------------------
Brad Daniels			|  Digital Equipment Corp. almost
DEC Software Devo		|  definitely wouldn't approve of
"VAX C RTL Whipping Boy"	|  anything I say here...

mcgrath@saffron.Berkeley.EDU (Roland McGrath) (09/06/89)

In article <1439@hiatus.dec.com> daniels@grue.dec.com (Bradford R. Daniels) writes:

   What should the default number of significant digits be for the
   %g format specifier in printf?  The standard says that an explicit
   0 should be treated as a 1, but doesn't say anything about what to
   do if no precision is specified.

   Right now, the VAX C RTL uses 6 as the default precision.  This
   seems reasonable, since 6 is the default precision for the %e
   and %f specifiers.  However, precision has a different meaning
   with %g than with those other specifiers.  Is VAXCRTL's current
   behavior correct?

Yes.  The ANSI standard does specify that the default precision is 6.
--
	Roland McGrath
	Free Software Foundation, Inc.
roland@ai.mit.edu, uunet!ai.mit.edu!roland

chris@mimsy.UUCP (Chris Torek) (09/06/89)

In article <1439@hiatus.dec.com> daniels@grue.dec.com (Bradford R. Daniels)
writes:
>Also, the definition of significant digits I learned in my high
>school science classes says that if I am asked to print out 1024
>with 1 significant digit, I should get 1000 printed out.  Is that
>correct?

No: `1000' has four significant digits (as does `1.000'); you need `1e3'.
I have seen arguments on both sides of this, but only believe the one
that says `the number of digits written is the number of significant
digits'.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) (09/06/89)

In article <1439@hiatus.dec.com>, daniels@grue.dec.com (Bradford R. Daniels) writes:

|  Also, the definition of significant digits I learned in my high
|  school science classes says that if I am asked to print out 1024
|  with 1 significant digit, I should get 1000 printed out.  Is that
|  correct?

  I learned the same thing. The term used in K&R 1st ed is "precision,"
and the book states that it is not the same as significance. I once (in
error) wrote a printf in which the %g value did specify significant
digits. I was wrong in my reading of the standard, but it was useful.
-- 
bill davidsen	(davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon

daniels@grue.dec.com (Bradford R. Daniels) (09/07/89)

In article <MCGRATH.89Sep5175335@saffron.Berkeley.EDU>,
mcgrath@saffron.Berkeley.EDU (Roland McGrath) writes:
> Yes.  The ANSI standard does specify that the default precision is 6.

Huh?  Where?  I am working from document X3J11/88-159, which says
under %g:

  "The double argument is converted in the style f or e (or in
   style E in the case of a G conversion specifier), with the
   precision specifying the number of digits.  If the precision
   is zero, it is taken as 1.  The style used depends on the
   value converted..."

It then goes on to describe when weach format is used, and that
trailing zeroes, etc. should be removed.  What do you see that I
don't?

- Brad
-----------------------------------------------------------------
Brad Daniels			|  Digital Equipment Corp. almost
DEC Software Devo		|  definitely wouldn't approve of
"VAX C RTL Whipping Boy"	|  anything I say here...

daniels@grue.dec.com (Bradford R. Daniels) (09/07/89)

> I have seen arguments on both sides of this, but only believe the one
> that says `the number of digits written is the number of significant
> digits'.

Ack.  You're right.  I hang my head in shame...  How quickly we
forget the things we learn in high school.  It should have been
obvious to me from the sentence "... style e (or E) will be used
only if the exponent ... is less than -4 or greater than or equal
to the precision."

BTW - Shouldn't the "only if" in that sentence be "if and only
if"?  It seems like the way it is currently phrased you could
use f format in those cases if you want to.

- Brad

-----------------------------------------------------------------
Brad Daniels			|  Digital Equipment Corp. almost
DEC Software Devo		|  definitely wouldn't approve of
"VAX C RTL Whipping Boy"	|  anything I say here...

gwyn@smoke.BRL.MIL (Doug Gwyn) (09/07/89)

In article <19426@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
-In article <1439@hiatus.dec.com> daniels@grue.dec.com (Bradford R. Daniels)
-writes:
->Also, the definition of significant digits I learned in my high
->school science classes says that if I am asked to print out 1024
->with 1 significant digit, I should get 1000 printed out.  Is that
->correct?
-No: `1000' has four significant digits (as does `1.000'); you need `1e3'.
-I have seen arguments on both sides of this, but only believe the one
-that says `the number of digits written is the number of significant
-digits'.

While 1.000 indeed unambiguously has 4 significant digits, the number
of significant digits in 1000 is not evident from inspection; it could
be from 1 to 4.  When you ask printf() to format 1024 with 1 significant
digit in a fairly wide field, it's supposed to produce 1000 not 1e3.

gwyn@smoke.BRL.MIL (Doug Gwyn) (09/07/89)

In article <1442@hiatus.dec.com> daniels@grue.dec.com (Bradford R. Daniels) writes:
>...  It should have been obvious to me from the sentence "... style e
>(or E) will be used only if the exponent ... is less than -4 or greater
than or equal to the precision."

Oops.  This does indeed make the correct answer 1e+3 instead of 1000,
for %g format.  I would cancel my earlier posting on this except this
&^$(^%! news software won't let me!

msb@sq.sq.com (Mark Brader) (09/09/89)

[Quoting style in quoted messages changed for consistency.]
Bradford R. Daniels (daniels@grue.dec.com) writes:
> > I learned in my high school ... that if I am asked to print out "1024"
> > with 1 significant digit, I should get "1000" printed out.

Chris Torek (chris@mimsy.UUCP) writes:
> No: "1000" has four significant digits (as does "1.000"); you need "1e3".
> I have seen arguments on both sides of this, but only believe the one
> that says `the number of digits written is the number of significant
> digits'.

Well, my high school agreed with Bradford's; I learned that either "1e3"
or "1000" had *one* significant digit, and that to write a value of
1000 with four significant digits you had to say "1000." or "1.000e3".
(Where "e" is taken as an ASCIIfication of "times sign, 10, superscript:")

But such an interpretation of "significant digit" would make section
4.9.6.1 impossible to satisfy for a conversion specification of "%4g"
and a value of 1000 -- the wording requires %f style and no trailing period
-- so it's clear that the authors of the pANS must have had in mind the
interpretation that Chris gives.  Given that there seem to be two conflicting
interpretations it is a pity that they used the words "significant digits".

As for a value of 1024, of course "%.1g" of that should produce "1e3",
or rather, "1e+03".

-- 
Mark Brader			Summary of issue:  Fix FORTRAN-8x.
SoftQuad Inc., Toronto		Committee Response:  This proposal contains
utzoo!sq!msb				insurmountable technical errors.
msb@sq.com			-- X3J11 responses to 2nd public review

This article is in the public domain.

daniels@grue.dec.com (Bradford R. Daniels) (09/10/89)

Distribution: world
Organization: Digital Equipment Corporation

In article <1441@hiatus.dec.com>, daniels@grue.dec.com (Bradford R.
Daniels) writes:
> In article <MCGRATH.89Sep5175335@saffron.Berkeley.EDU>,
> mcgrath@saffron.Berkeley.EDU (Roland McGrath) writes:
> > Yes.  The ANSI standard does specify that the default precision is 6.
> 
> Huh?  Where?  I am working from document X3J11/88-159, which says
> under %g:
> 
>   "The double argument is converted in the style f or e (or in
>    style E in the case of a G conversion specifier), with the
>    precision specifying the number of digits.  If the precision
>    is zero, it is taken as 1.  The style used depends on the
>    value converted..."
> 
> It then goes on to describe when weach format is used, and that
> trailing zeroes, etc. should be removed.  What do you see that I
> don't?

I really would like a definitive answer (or at least some kind of
consensus) on this issue.  I appreciate all of the input on what
significant digits should mean in the context of %g, but now that
I'm pretty sure we handle that correctly, the default precision
issue is more important...

Thanks again,

- Brad

-----------------------------------------------------------------
Brad Daniels			|  Digital Equipment Corp. almost
DEC Software Devo		|  definitely wouldn't approve of
"VAX C RTL Whipping Boy"	|  anything I say here...

diamond@csl.sony.co.jp (Norman Diamond) (09/13/89)

In article <1989Sep9.013233.9939@sq.sq.com> msb@sq.com (Mark Brader) writes:

>As for a value of 1024, of course "%.1g" of that should produce "1e3",
>or rather, "1e+03".

Why does the exponent "rather" have a leading zero?  If the standard
mandates (or even allows) this, it is the only place where it specifies
a leading zero in a place where the programmer did not request it.

(And if so, why not pad it with more leading zeroes, so that a machine
that can handle exponents up to 75024 can say 1e+00003.)

--
-- 
Norman Diamond, Sony Corporation (diamond@ws.sony.junet)
  The above opinions are inherited by your machine's init process (pid 1),
  after being disowned and orphaned.  However, if you see this at Waterloo or
  Anterior, then their administrators must have approved of these opinions.

chris@mimsy.UUCP (Chris Torek) (09/14/89)

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

In article <10832@riks.csl.sony.co.jp> diamond@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@mimsy.umd.edu	Path:	uunet!mimsy!chris