Re: Problem with printf formats
- From: Tatu Portin <axel86@xxxxxxxx>
- Date: Wed, 21 Dec 2005 01:12:17 GMT
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
.
- References:
- Problem with printf formats
- From: Guenther Sohler
- Problem with printf formats
- Prev by Date: Re: Pseudorandom algorithms
- Next by Date: Re: Problem with printf formats
- Previous by thread: Re: Problem with printf formats
- Next by thread: Re: Problem with printf formats
- Index(es):
Relevant Pages
|