Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Problem with printf formats

Reply
Thread Tools

Problem with printf formats

 
 
Guenther Sohler
Guest
Posts: n/a
 
      12-20-2005
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 ?






 
Reply With Quote
 
 
 
 
Chuck F.
Guest
Posts: n/a
 
      12-20-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 ?


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?
 
Reply With Quote
 
 
 
 
usenet@zevv.nl
Guest
Posts: n/a
 
      12-20-2005

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
 
Reply With Quote
 
Jirka Klaue
Guest
Posts: n/a
 
      12-20-2005
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
 
Reply With Quote
 
Guenther Sohler
Guest
Posts: n/a
 
      12-20-2005
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


 
Reply With Quote
 
Guenther Sohler
Guest
Posts: n/a
 
      12-20-2005
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!


 
Reply With Quote
 
Guenther Sohler
Guest
Posts: n/a
 
      12-20-2005
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


 
Reply With Quote
 
Jirka Klaue
Guest
Posts: n/a
 
      12-20-2005
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
 
Reply With Quote
 
Dik T. Winter
Guest
Posts: n/a
 
      12-20-2005
In article <(E-Mail Removed)> Guenther Sohler <(E-Mail Removed)> 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/
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      12-20-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is MKV formats better than other video formats? helena68 Software 3 11-26-2008 07:57 AM
Detailed Analysis of the file formats for M$ Office 2007 -"Microsoft Office XML Formats? Defective by design" Jonathan Walker NZ Computing 1 08-26-2007 03:43 AM
Trouble with formats in printf Sanjay Kulkarni C Programming 5 03-21-2007 10:36 PM
CyberLink Supports UDF 2.5/2.6 Formats For Blu-ray and HD DVD next generation of disc formats. Allan DVD Video 0 07-15-2005 07:43 PM
printf formats for size_t? David Mathog C Programming 6 05-13-2005 07:07 PM



Advertisments