# problem to print large double number. Help!

Discussion in 'C Programming' started by ai lian, Jul 23, 2003.

1. ### ai lianGuest

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.

ai lian, Jul 23, 2003

2. ### Eric SosmanGuest

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

--

Eric Sosman, Jul 23, 2003

3. ### Chris DollinGuest

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-newsgroup/comp/comp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html

Chris Dollin, Jul 24, 2003
4. ### Thomas BonnerGuest

"ai lian" <> schrieb im Newsbeitrag
news:...
> 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

Thomas Bonner, Jul 24, 2003
5. ### Glen HerrmannsfeldtGuest

"ai lian" <> wrote in message
news:...
> 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

Glen Herrmannsfeldt, Jul 25, 2003