problem to print large double number. Help!

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

  1. ai lian

    ai lian Guest

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

  2. ai lian

    Eric Sosman Guest

    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
    #2
    1. Advertising

  3. ai lian

    Chris Dollin Guest

    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
    #3
  4. "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
    #4
  5. "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
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,473
    Victor Bazarov
    Feb 17, 2005
  2. sweetone
    Replies:
    1
    Views:
    419
    Andrew Thompson
    Jan 20, 2007
  3. keto
    Replies:
    0
    Views:
    924
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    345
    David Cournapeau
    Dec 30, 2008
  5. KB
    Replies:
    6
    Views:
    263
Loading...

Share This Page