Velocity Reviews > Problem with printf formats

# Problem with printf formats

Tatu Portin
Guest
Posts: n/a

 12-21-2005
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

Andrey Tarasevich
Guest
Posts: n/a

 12-21-2005
Guenther Sohler wrote:
> ...
> I know, but the accuracy of double should still good enough to fulfill my
> purpose!
> ...

What exactly do you mean by "good enough" and "your purpose" in this case? Most
of the time floating-point numbers that have innocent-looking decimal
representations have infinitely long [periodic] representations in traditional
binary positional notation. For example, both '0.001' and '2158.47' are such
numbers. It is impossible to represent them precisely in 'double', which
probably means that 'double's precision (or precision of any type of same
structure, no matter how large) is definitely not "good enough" (assuming that I

--
Best regards,
Andrey Tarasevich

Gordon Burditt
Guest
Posts: n/a

 12-21-2005
>> I know, but the accuracy of double should still good enough to fulfill my
>> purpose!
>> ...

>
>What exactly do you mean by "good enough" and "your purpose" in this case?

(I'm not the original poster).

It is difficult and very expensive to measure most any physical
quantity to more significant digits than are present in a double.
(15 in a typical IEEE float implementation). Exceptions may include
currency and time.

Gordon L. Burditt

Simon Biber
Guest
Posts: n/a

 12-21-2005
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 ?

I can write a function to do it.

#include <stdio.h>
#include <string.h>

char *sprintd(char *buf, size_t buf_size, double val, int precision)
{
/* write value into string */
snprintf(buf, buf_size, "%.*f", precision, val);

/* find decimal point */
char *p = strchr(buf, '.'), *q;

/* remove trailing zeros */
if(p) for(q = strchr(p, 0) - 1; *q == '0'; --q) *q = 0;

/* remove trailing decimal point */
if(p && p[1] == 0) p[0] = 0;

return buf;
}

int main(void)
{
char buf[100];
printf("%s\n", sprintd(buf, sizeof buf, 355.0 / 113.0, 15));
return 0;
}

--
Simon.

vishnu
Guest
Posts: n/a

 12-21-2005
write %.2lf inn place of %.3lf

Keith Thompson
Guest
Posts: n/a

 12-21-2005
"vishnu" <(E-Mail Removed)> writes:
> write %.2lf inn place of %.3lf

What?

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Joe Wright
Guest
Posts: n/a

 12-21-2005
Guenther Sohler wrote:
> On Tue, 20 Dec 2005 12:08:23 +0000, usenet wrote:
>
>
>>Guenther Sohle said:
>>
>>(...)
>>
>>> 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 ?

>>
>>The problem is not in the printf() function, but in the way floating point
>>numbers are represented by your computer. Due to rounding errors, floating
>>point numbers hardly ever have the 'exact' value you would expect. Search
>>http://en.wikipedia.org/wiki/Floating_point.
>>
>>It is up to you to make the choice how many digits are significant in a

>
>
> I know, but the accuracy of double should still good enough to fulfill my
> purpose!
>
>

You are perhaps confusing accuracy and precision. A double will always
be precise to 53 bits or 16 digits or whatever. Its value is often less
accurate than that, depending on how it was contrived.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---