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

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

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)

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

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

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