Re: Problem with printf formats



Guenther Sohler wrote:

> I have following code:
>
> int main(void)
> {
> printf("%.3lf\n",-2158470*0.001);
> }
>
> it prints
>
> -2158.470
>
> How can i disable '0' at the end, if they are not needed ?
>
> Is there a possibility just to write %lf and just print as many
> digits as needed to represent the number ?


Question "How can I disable '0' at the end" indicates somekind of
misconception of floating point numbers.

Count of digits in a number indicates the accuracy of the number. So,
if you just remove zero from a number, you decrease accuracy of the
number.

For example:
-2158.5 could be result of rounding -2158.470, but it would be wrong
to add zeros to -2158.5 because the accurate amount was -2158.470.

The significance of zeros in indicating accuracy can be only perceived
when handling numbers on right side of decimal point.
For example:
-2000 could be result of rounding -2158 or it could be the exact
amount. You cannot tell if -2000 has one, two, three or four
significant digits, i.e. if -2000 is accurate to first, second, third
or fourth digit.
This is why scientific notation is used to represent quantities.
E.g. -2.000e3 is different from -2e3 in accuracy.


--
It's bit too late to be clear, but I hope I made my point through.

C faq: http://www.eskimo.com/~scs/C-faq/top.html
Reference: http://www.acm.uiuc.edu/webmonkeys/book/c_guide/
Coding standards: http://www.psgd.org/paul/docs/cstyle/cstyle.htm
.



Relevant Pages

  • Re: Problem Importing from an Excel spread sheet to an Access 2003 table.
    ... display 5 digits with a single command? ... displayed as 5 digits and 5 digits only. ... >1) Modify the Excel sheet before import by prefixing an ... leading zeros. ...
    (microsoft.public.access.externaldata)
  • Re: How to Force Field Size
    ... will not *store* leading zeros, ... Public Function LeftPadToLength(strVal As String, intLength As Integer, ... I have decided to change my numbers into the 10 digits ... >>End Sub ...
    (microsoft.public.access.modulesdaovba)
  • Re: I think the standard will be on my side (BOZ)
    ... The 'leading' and 'trailing' zero counts should probably ... language is written right-to-left write their numbers with the ... digits on the left. ... And this discussion of leading and trailing zeros is the ...
    (comp.lang.fortran)
  • Re: How do I display number field in Access?
    ... "Jeff Boyce" wrote: ... If your input number is preceeded by any number of leading zeros, ... text characters that happen to be digits. ... I am attempting to get the field to display when I go into the Table view ...
    (microsoft.public.access.externaldata)
  • Re: Leading Zero in channel changing. (resolution)
    ... MCE will output leading zeros when you specify "I don't use ... to change channels, it keeps using leading zeros as it has no code for the ... If I use 3 digits, then I can't tune in a four digit ...
    (microsoft.public.windows.mediacenter)