Re: Problem with printf formats



Guenther Sohler:

I also tried %g:
int main(void)
{
	printf("%g\n",-2158475*0.001);
}

which results in
-2158.47

one digit is missed! this is a killing issue for me!

Well, then it's time for you to read the appropriate part of the standard.

ISO/IEC 9899:TC2
7.19.6.1#8
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.
    Finally, unless the # flag is used, any trailing zeros are removed from
    the fractional portion of the result and the decimal-point character is
    removed if there is no fractional portion remaining.

So try %.9g or something like that.

how can i have the proper value displayed in all cases ?

This could be difficult depending on the meaning of proper.

Jirka
.



Relevant Pages

  • Re: IBM2435I on ROUND(x,-3)
    ... fraction. ... If an intermediate result is necessary, as in evaluation of an operational expression, the attributes of the intermediate result are the same as if a decimal fixed-point value of precision had appeared in place of the string. ... In other words, FIXED DECIMALwould be converted to FLOAT DECIMAL, and in the actual conversion process, any fractional part of the constant expressed in character form would influence the exponent of the converted result and the fractional digits would not actually be lost. ... ROUNDreturns the value of the real expression x rounded at a digit specified by n. ...
    (comp.lang.pl1)
  • Re: IBM2435I on ROUND(x,-3)
    ... fraction. ... If an intermediate result is necessary, as in evaluation of an operational expression, the attributes of the intermediate result are the same as if a decimal fixed-point value of precision had appeared in place of the string. ... In other words, FIXED DECIMALwould be converted to FLOAT DECIMAL, and in the actual conversion process, any fractional part of the constant expressed in character form would influence the exponent of the converted result and the fractional digits would not actually be lost. ... ROUNDreturns the value of the real expression x rounded at a digit specified by n. ...
    (comp.lang.pl1)
  • Re: double to int conversion yields strange results
    ... giving me two different values for the int. ... the question raised is not about the precision of the ... is done in extended precision. ... It is the conversion to 'double' before the conversion to 'int' that ...
    (comp.lang.c)
  • Re: printf and style g
    ... a G conversion specifier), ... Let P equal the precision if nonzero, ... if a conversion with style E would have an exponent ... formatting and a precision of 2. ...
    (comp.std.c)
  • Re: double money
    ... precision in decimal due to this "wobbling precision". ... -- way faster conversion between external decimal and internal form ... -- exact representation of decimal fractions ... People seldom notice the loss of a bit or two of precision -- ...
    (comp.lang.c)