Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > problem to print large double number. Help!

Reply
Thread Tools

problem to print large double number. Help!

 
 
ai lian
Guest
Posts: n/a
 
      07-23-2003
When I use printf to print a large double number, the result is not
the same as the original input number. For example:

double num=899999999999.894400;
printf("%lf\n",num);

The output is: 899999999999.894409

Could anyone tell me what the problem is? How can I get the right
output?

Thanks.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      07-23-2003
ai lian wrote:
>
> When I use printf to print a large double number, the result is not
> the same as the original input number. For example:
>
> double num=899999999999.894400;
> printf("%lf\n",num);
>
> The output is: 899999999999.894409
>
> Could anyone tell me what the problem is? How can I get the right
> output?


Question 14.1 in the comp.lang.c Frequently Asked
Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

covers part of your problem. The rest is due to unrealistic
expectations: Why do you think you're going to get accuracy
to eighteen (or more!) significant digits? The C language
Standard only promises ten and you're actually getting
fifteen, so what are you complaining about?

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      07-24-2003
ai lian wrote:

> When I use printf to print a large double number, the result is not
> the same as the original input number. For example:
>
> double num=899999999999.894400;
> printf("%lf\n",num);
>
> The output is: 899999999999.894409
>
> Could anyone tell me what the problem is?


Yes. Usual implementations of floating-point numbers don't have that
much accuracy; furthermore there are numbers they cannot express
accurately at all, unless you happen to have a machine with unbounded
memory.

You're complaining about an error of 9 parts in 10e18 (or 17 or 19,
I can't count very well, 9 in a billion billion. Have some perspective.
(Also, see the FAQ.)

> How can I get the right output?


Use strings to hold decimal numbers?

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
 
Reply With Quote
 
Thomas Bonner
Guest
Posts: n/a
 
      07-24-2003

"ai lian" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) om...
> When I use printf to print a large double number, the result is not
> the same as the original input number. For example:
>
> double num=899999999999.894400;
> printf("%lf\n",num);
>
> The output is: 899999999999.894409
>
> Could anyone tell me what the problem is? How can I get the right
> output?

Maybe you could try changing the original input a little bit hoping to
obtain a result positioned nearer to the desired value. Or use two
numbers (one before and one after the digit)?
Thomas Bonner



 
Reply With Quote
 
Glen Herrmannsfeldt
Guest
Posts: n/a
 
      07-25-2003

"ai lian" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> When I use printf to print a large double number, the result is not
> the same as the original input number. For example:
>
> double num=899999999999.894400;
> printf("%lf\n",num);
>
> The output is: 899999999999.894409
>
> Could anyone tell me what the problem is? How can I get the right
> output?


Normally %f is used for output, and not %lf.

In most implementations, and for most values, %f prints a reasonably number
of digits to round to the expected value. You seem to have found a case
where it doesn't.

Note that Java, for example, always prints enough digits so that when
converted back to internal format the same binary value is obtained. This
often means that the printed output disagrees with the user expectation, but
C doesn't normally do that.

Anyway, you got more digits than is normal for a double.

-- glen


 
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
print a vs print '%s' % a vs print '%f' a David Cournapeau Python 0 12-30-2008 03:19 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Double double display display problem problem Tom Accuosti Firefox 3 09-27-2004 10:02 PM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM



Advertisments