Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

printf and style g

0 views
Skip to first unread message

Vincent Lefevre

unread,
Jun 18, 2009, 8:47:04 PM6/18/09
to
The standard says for fprintf (7.19.6.1):

g,G A double argument representing a floating-point number is
converted in style f or e (or in style F or E in the case of
a G conversion specifier), depending on the value converted
and the precision. Let P equal the precision if nonzero, 6
if the precision is omitted, or 1 if the precision is zero.
Then, if a conversion with style E would have an exponent
of X:
- if P > X >= -4, the conversion is with style f (or F) and
precision P-(X+1).
- otherwise, the conversion is with style e (or E) and
precision P-1.

but it doesn't explicitly say what precision to choose for "if
a conversion with style E would have an exponent of X" (this is
important since X depends on the precision). It seems logical
to choose P-1 (as in the "otherwise" case), but shouldn't the
standard make this clear?

--
Vincent Lef�vre <vin...@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / Arenaire project (LIP, ENS-Lyon)

Mark Dickinson

unread,
Jun 19, 2009, 3:17:32 AM6/19/09
to
On Jun 19, 1:47 am, Vincent Lefevre <vincent+n...@vinc17.org> wrote:
> The standard says for fprintf (7.19.6.1):
>
>   g,G   A double argument representing a floating-point number is
>         converted in style f or e (or in style F or E in the case of
>         a G conversion specifier), depending on the value converted
>         and the precision. Let P equal the precision if nonzero, 6
>         if the precision is omitted, or 1 if the precision is zero.
>         Then, if a conversion with style E would have an exponent
>         of X:
>         - if P > X >= -4, the conversion is with style f (or F) and
>           precision P-(X+1).
>         - otherwise, the conversion is with style e (or E) and
>           precision P-1.
>
> but it doesn't explicitly say what precision to choose for "if
> a conversion with style E would have an exponent of X" (this is
> important since X depends on the precision). It seems logical
> to choose P-1 (as in the "otherwise" case), but shouldn't the
> standard make this clear?

So you're proposing that:

"if a conversion with style E would have an exponent of X: ..."

be replaced with:

"if a conversion with style E and precision P-1 would have an exponent
of X: ..."

?

Makes sense to me. I agree that the standard seems underspecified
here.

Mark

Vincent Lefevre

unread,
Jun 19, 2009, 3:54:27 PM6/19/09
to
In article <9f4e27ca-41a1-4a28...@n21g2000vba.googlegroups.com>,
Mark Dickinson <dick...@gmail.com> wrote:

> So you're proposing that:

> "if a conversion with style E would have an exponent of X: ..."

> be replaced with:

> "if a conversion with style E and precision P-1 would have an exponent
> of X: ..."

> ?

Yes.

Tim Rentsch

unread,
Jun 19, 2009, 8:00:15 PM6/19/09
to
Vincent Lefevre <vincen...@vinc17.org> writes:

> The standard says for fprintf (7.19.6.1):
>
> g,G A double argument representing a floating-point number is
> converted in style f or e (or in style F or E in the case of
> a G conversion specifier), depending on the value converted
> and the precision. Let P equal the precision if nonzero, 6
> if the precision is omitted, or 1 if the precision is zero.
> Then, if a conversion with style E would have an exponent
> of X:
> - if P > X >= -4, the conversion is with style f (or F) and
> precision P-(X+1).
> - otherwise, the conversion is with style e (or E) and
> precision P-1.
>
> but it doesn't explicitly say what precision to choose for "if
> a conversion with style E would have an exponent of X" (this is
> important since X depends on the precision). It seems logical
> to choose P-1 (as in the "otherwise" case), but shouldn't the
> standard make this clear?

I found this question very confusing. First, the quoted clause
serves to define X. There is no question about whether "a
conversion with style E would have an exponent of X". It would,
because that's how X is defined. Second, X does not depend on
the precision. A conversion using style E always has exactly one
digit before the decimal point, which constrains the exponent in
such a format to be exactly one number. The existing wording,
even if not immediately obvious, does seem clear about what
output will be produced for a g/G format item.

Can you give an example format and value to be output that
illustrates the ambiguity you think is there?

Mark Dickinson

unread,
Jun 20, 2009, 3:54:46 AM6/20/09
to
On Jun 20, 1:00 am, Tim Rentsch <t...@alumnus.caltech.edu> wrote:

> Vincent Lefevre <vincent+n...@vinc17.org> writes:
> > The standard says for fprintf (7.19.6.1):
>
> >   g,G   A double argument representing a floating-point number is
> >         converted in style f or e (or in style F or E in the case of
> >         a G conversion specifier), depending on the value converted
> >         and the precision. Let P equal the precision if nonzero, 6
> >         if the precision is omitted, or 1 if the precision is zero.
> >         Then, if a conversion with style E would have an exponent
> >         of X:
> >         - if P > X >= -4, the conversion is with style f (or F) and
> >           precision P-(X+1).
> >         - otherwise, the conversion is with style e (or E) and
> >           precision P-1.
>
> > but it doesn't explicitly say what precision to choose for "if
> > a conversion with style E would have an exponent of X" (this is
> > important since X depends on the precision). It seems logical
> > to choose P-1 (as in the "otherwise" case), but shouldn't the
> > standard make this clear?
>
> I found this question very confusing.  First, the quoted clause
> serves to define X.

That's the point. It doesn't serve to define X, because the
value of X can depend on what precision was used for the
style E conversion.

For clarity, I'll refer to the conversion in the quoted
clause as the 'would have' conversion, to distinguish
it from the eventual intended g-style conversion.

Example: support the value to be formatted is 99.75.
What's X? A 'would have' conversion using style E
and precision 1 would give '1.0E+2', giving X = 2.
A 'would have' conversion using style E and precision
2 would give '9.98E+1' (or possibly 9.97E+1 on some
systems), giving X = 1.

> [...]


>
> Can you give an example format and value to be output that
> illustrates the ambiguity you think is there?

Suppose we want to format 99.75 (again) with g-style
formatting and a precision of 2. Again, as above, X
depends on the precision used for the 'would give'
conversion: if we use precision 1 for that, then X is
2, and the result of the 'g' formatting (after stripping
trailing zeros as per the standard) is "1e+2". If we use
precision 2 or higher, then X is 1 and the result of the
'g' formatting is "100". The second result is clearly
wrong, since it ends up giving 3 significant digits
rather than 2.

Now consider g-style formatting of 99.75 with P = 3.
If we use precision 2 for the 'would give' format, we
get X = 1 and the result of the 'g' formatting is "99.8".
But if we use precision 1 for the 'would give' format
then X = 2 and the formatted result is "100". Again,
the second result is wrong: the result of rounding
99.75 to 3 significant figures should be 99.8, however
one chooses to express it.

So the value of X *does* depend on the precision used,
and the precision used should thus be specified. A
'would have' precision of P-1 is the correct one to use,
since this has the effect of rounding to a decimal number
with exactly P significant digits, which is the intent of
'g' formatting in the first place.

I'm 72.3% sure that I'm not misunderstanding Vincent's
point here; I hope he'll step in and correct me if I am.

Mark

Vincent Lefevre

unread,
Jun 20, 2009, 7:24:05 AM6/20/09
to
In article <kfnbpoj...@alumnus.caltech.edu>,
Tim Rentsch <t...@alumnus.caltech.edu> wrote:

> Vincent Lefevre <vincen...@vinc17.org> writes:

> > The standard says for fprintf (7.19.6.1):
> >
> > g,G A double argument representing a floating-point number is
> > converted in style f or e (or in style F or E in the case of
> > a G conversion specifier), depending on the value converted
> > and the precision. Let P equal the precision if nonzero, 6
> > if the precision is omitted, or 1 if the precision is zero.
> > Then, if a conversion with style E would have an exponent
> > of X:
> > - if P > X >= -4, the conversion is with style f (or F) and
> > precision P-(X+1).
> > - otherwise, the conversion is with style e (or E) and
> > precision P-1.
> >
> > but it doesn't explicitly say what precision to choose for "if
> > a conversion with style E would have an exponent of X" (this is
> > important since X depends on the precision). It seems logical
> > to choose P-1 (as in the "otherwise" case), but shouldn't the
> > standard make this clear?

> I found this question very confusing. First, the quoted clause
> serves to define X.

Yes.

> There is no question about whether "a conversion with style E would
> have an exponent of X". It would, because that's how X is defined.
> Second, X does not depend on the precision.

No, the main point is that in some cases (near a power of 10),
X does depend on the precision (there was a bug a MPFR because
of a different interpretation made by one of the developers).

> Can you give an example format and value to be output that
> illustrates the ambiguity you think is there?

0.99 with the precision P = 1.

With the correction interpretation (%.0e), X = 0. But with the
incorrect one (%.1e), X = -1.

Tim Rentsch

unread,
Jun 20, 2009, 12:35:52 PM6/20/09
to
Vincent Lefevre <vincen...@vinc17.org> writes:

Okay, I see your point now. My statement about X not depending
on the precision was wrong; it's good to get that clarified.

Responding to your original question, it seems clear (even if not
as obvious as it could/should be) that the clause in question ("a
conversion with style E would have an exponent of X") is meant in
the context of describing a g/G format conversion, partly because
of 7.19.6.1 p 4, which says (in part):

An optional precision that gives [...] the maximum number of
significant digits for the g and G conversions

So I don't think an additional clarifying condition is necessary
here, strictly speaking. I would, however, both second and vote
for a motion to add a footnote clarifying the subtlety.

Tim Rentsch

unread,
Jun 20, 2009, 12:39:36 PM6/20/09
to
Mark Dickinson <dick...@gmail.com> writes:

Quite. I see the point now; thank you for the clarification.
(I also see that Vincent has responded, and am posting my
more complete response there.)

0 new messages