problem to print large double number. Help!

A

ai lian

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.
 
E

Eric Sosman

ai said:
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? ;-)
 
C

Chris Dollin

ai said:
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?
 
T

Thomas Bonner

ai lian said:
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
 
G

Glen Herrmannsfeldt

ai lian said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top