Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Problem with printf formats (http://www.velocityreviews.com/forums/t440604-problem-with-printf-formats.html)

Guenther Sohler 12-20-2005 11:11 AM

Problem with printf formats
 
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 ?







Chuck F. 12-20-2005 12:06 PM

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 ?


You are asking the wrong question. The trailing zero is needed to
represent the number. Otherwise how could you distinguish it from:

printf("%.2f\n", -2158471 * 0.001)

(always assuming that -2158471 does not create an overflow)

Your question, I believe, is "how to suppress trailing zeroes". A
little thought and an intermediary buffer should give you a
suitable answer for that.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

usenet@zevv.nl 12-20-2005 12:08 PM

Re: Problem with printf formats
 

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
google for 'floating point' for some details, or read this wikipedia page:
http://en.wikipedia.org/wiki/Floating_point.

It is up to you to make the choice how many digits are significant in a
number, so that is why you should tell printf() about this.


--
:wq
^X^Cy^K^X^C^C^C^C

Jirka Klaue 12-20-2005 12:56 PM

Re: Problem with printf formats
 
Guenther Sohler:

> 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 ?


The l in %lf is superfluous, just use %f.
To avoid the trailing zeros try %g.

Jirka

Guenther Sohler 12-20-2005 01:34 PM

Re: Problem with printf formats
 
On Tue, 20 Dec 2005 13:56:44 +0100, Jirka Klaue wrote:

> Guenther Sohler:
>
>> 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 ?

>
> The l in %lf is superfluous, just use %f.

Thank you for the info.
> To avoid the trailing zeros try %g.


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!

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



Guenther Sohler 12-20-2005 01:35 PM

Re: Problem with printf formats
 
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
> google for 'floating point' for some details, or read this wikipedia page:
> http://en.wikipedia.org/wiki/Floating_point.
>
> It is up to you to make the choice how many digits are significant in a
> number, so that is why you should tell printf() about this.


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



Guenther Sohler 12-20-2005 01:37 PM

Re: Problem with printf formats
 
On Tue, 20 Dec 2005 07:06:42 -0500, Chuck F. wrote:

> Guenther Sohler wrote:
> >
>> I have following code:
>>
>> int main(void)
>> {
>> printf("%.3lf\n",-2158470*0.001);
>> }
>>
>> it prints

>
> Your question, I believe, is "how to suppress trailing zeroes". A
> little thought and an intermediary buffer should give you a
> suitable answer for that.


Thank you for the information. Yes, I also keep this in mind,
but i use %f very often in my code ( > 100) and to suitable
handle all them, I would have to write my own printf.
I dont like to do this unless I am really sure there is no other way



Jirka Klaue 12-20-2005 01:59 PM

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

Dik T. Winter 12-20-2005 03:07 PM

Re: Problem with printf formats
 
In article <pan.2005.12.20.13.34.19.270627@newlogic.com> Guenther Sohler <guenther.sohler@newlogic.com> writes:
....
> 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!
>
> how can i have the proper value displayed in all cases ?


But, what *is* the proper value? In binary it is:
-10000110110.01110011001100110011...
and that is what is stored on the computer, most decimal numbers are
not exactly representable in binary. So what will be stored is a
rounded value, and the value in memory is:
-2158.47499999999990905052982270717620849609375
so what should the system do?

--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

Old Wolf 12-20-2005 11:22 PM

Re: Problem with printf formats
 
Jirka Klaue wrote:

> Guenther Sohler:
>> Is there a possibility just to write %lf and just print as many digits
>> as needed to represent the number ?

>
> The l in %lf is superfluous, just use %f.


It is superfluous in C99, and is an error in C89. But most
C89 compilers allow it anyway.



All times are GMT. The time now is 12:25 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.