most accurate printf("%f")

Discussion in 'C++' started by Gernot Frisch, Oct 11, 2004.

  1. Hi,


    I want to represent a double in char* as accurate as possible, but as
    short as possible:

    double char*
    10000 1E5
    1000.00123 1000.00123
    ....

    How can I archive this?
    %.10f prints too much, and %.10g is too inaccurate.


    --
    -Gernot
    int main(int argc, char** argv) {printf
    ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

    ________________________________________
    Looking for a good game? Do it yourself!
    GLBasic - you can do
    www.GLBasic.com
    Gernot Frisch, Oct 11, 2004
    #1
    1. Advertising

  2. "Gernot Frisch" <> wrote in message
    news:...
    > I want to represent a double in char* as accurate as possible, but as
    > short as possible:

    This is the intent of the %g formatter in printf.

    > double char*
    > 10000 1E5
    > 1000.00123 1000.00123
    > ...
    >
    > How can I archive this?
    > %.10f prints too much, and %.10g is too inaccurate.

    How is %g "too inaccurate", could you provide an example ?
    The accuracy of a double, in IEEE format, is limited to about 15 decimal
    digits. So a %.15g should do what you asked for.


    Regards,
    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Ivan Vecerina, Oct 11, 2004
    #2
    1. Advertising

  3. "Ivan Vecerina" <>
    schrieb im Newsbeitrag news:ckdsrj$ukg$...
    > "Gernot Frisch" <> wrote in message
    > news:...
    >> I want to represent a double in char* as accurate as possible, but
    >> as short as possible:

    > This is the intent of the %g formatter in printf.
    >
    >> double char*
    >> 10000 1E5
    >> 1000.00123 1000.00123
    >> ...
    >>
    >> How can I archive this?
    >> %.10f prints too much, and %.10g is too inaccurate.

    > How is %g "too inaccurate", could you provide an example ?
    > The accuracy of a double, in IEEE format, is limited to about 15
    > decimal
    > digits. So a %.15g should do what you asked for.


    double f = 10000.0000000123567;
    printf("%20.15f - %.15g\n", f, f);
    // 10000.000000012356000 - 10000.0000000124
    Gernot Frisch, Oct 11, 2004
    #3
  4. Gernot Frisch wrote:
    >
    > "Ivan Vecerina" <>
    > schrieb im Newsbeitrag news:ckdsrj$ukg$...
    > > "Gernot Frisch" <> wrote in message
    > > news:...
    > >> I want to represent a double in char* as accurate as possible, but
    > >> as short as possible:

    > > This is the intent of the %g formatter in printf.
    > >
    > >> double char*
    > >> 10000 1E5
    > >> 1000.00123 1000.00123
    > >> ...
    > >>
    > >> How can I archive this?
    > >> %.10f prints too much, and %.10g is too inaccurate.

    > > How is %g "too inaccurate", could you provide an example ?
    > > The accuracy of a double, in IEEE format, is limited to about 15
    > > decimal
    > > digits. So a %.15g should do what you asked for.

    >
    > double f = 10000.0000000123567;


    cout the digits

    10000.0000000123567

    00000 0000111111111 (Note: 1 reads: 14
    12345 6789012345678 4

    So everything to the right of

    ......123

    is most likely noise anyway (assuming a standard
    precission of 15 digits)

    Note: 15 digits does *not* mean: 15 digits after comma.
    It means 15 digits total!

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Oct 11, 2004
    #4
  5. Gernot Frisch wrote:
    >
    > "Ivan Vecerina" <>
    > schrieb im Newsbeitrag news:ckdsrj$ukg$...
    > > "Gernot Frisch" <> wrote in message
    > > news:...
    > >> I want to represent a double in char* as accurate as possible, but
    > >> as short as possible:

    > > This is the intent of the %g formatter in printf.
    > >
    > >> double char*
    > >> 10000 1E5
    > >> 1000.00123 1000.00123
    > >> ...
    > >>
    > >> How can I archive this?
    > >> %.10f prints too much, and %.10g is too inaccurate.

    > > How is %g "too inaccurate", could you provide an example ?
    > > The accuracy of a double, in IEEE format, is limited to about 15
    > > decimal
    > > digits. So a %.15g should do what you asked for.

    >
    > double f = 10000.0000000123567;


    cout the digits

    10000.0000000123567

    00000 0000111111111 (Note: 1 reads: 14
    12345 6789012345678 4

    So everything to the right of

    .......000123

    is most likely noise anyway (assuming a standard
    precission of 15 digits)

    Note: 15 digits does *not* mean: 15 digits after comma.
    It means 15 digits total!

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Oct 11, 2004
    #5
  6. > cout the digits
    >
    > 10000.0000000123567
    >
    > 00000 0000111111111 (Note: 1 reads: 14
    > 12345 6789012345678 4
    >
    > So everything to the right of
    >
    > .......000123
    >
    > is most likely noise anyway (assuming a standard
    > precission of 15 digits)
    >
    > Note: 15 digits does *not* mean: 15 digits after comma.
    > It means 15 digits total!


    So, why is %.15f printing it correctly? Coincidence?
    Gernot Frisch, Oct 11, 2004
    #6
  7. Gernot Frisch wrote:
    >
    > > cout the digits
    > >
    > > 10000.0000000123567
    > >
    > > 00000 0000111111111 (Note: 1 reads: 14
    > > 12345 6789012345678 4
    > >
    > > So everything to the right of
    > >
    > > .......000123
    > >
    > > is most likely noise anyway (assuming a standard
    > > precission of 15 digits)
    > >
    > > Note: 15 digits does *not* mean: 15 digits after comma.
    > > It means 15 digits total!

    >
    > So, why is %.15f printing it correctly? Coincidence?


    15 is average. It depends on the number itself how many digits you get
    exactly. BTW by assining 10000.0000000123567 and getting
    10000.000000012356000 one cannot talk from correctly. The last
    digit you assigned was 7, printf brought back a 0 (and didn't round
    the previous 6 to 7).

    Might be interesting for you:
    http://www.petebecker.com/js200006.html
    http://docs.sun.com/source/806-3568/ncg_goldberg.html

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Oct 11, 2004
    #7
  8. Gernot Frisch, Oct 11, 2004
    #8
  9. Gernot Frisch wrote:
    >>cout the digits
    >>
    >>10000.0000000123567
    >>
    >>00000 0000111111111 (Note: 1 reads: 14
    >>12345 6789012345678 4
    >>
    >>So everything to the right of
    >>
    >> .......000123
    >>
    >>is most likely noise anyway (assuming a standard
    >>precission of 15 digits)
    >>
    >>Note: 15 digits does *not* mean: 15 digits after comma.
    >>It means 15 digits total!

    >
    >
    > So, why is %.15f printing it correctly? Coincidence?


    Absolutely.

    Change the number (to be, e.g., 10000.0000000123333) and see if
    you get the same "precise" output. I got two different results
    on Linux (g++ v 3.2.2) and IRIX (MIPSpro 7.3). Both had garbage
    after the first 16 digits. sizeof(double) is 8 on both systems.

    Also, change the leading 1 to, say, leading 9, and you will get
    even "less" accuracy -- the garbage will begin to appear in the
    16th digit.

    V
    Victor Bazarov, Oct 11, 2004
    #9
  10. Gernot Frisch

    Jack Klein Guest

    On Mon, 11 Oct 2004 11:40:15 +0200, "Gernot Frisch" <>
    wrote in comp.lang.c++:

    > Hi,
    >
    >
    > I want to represent a double in char* as accurate as possible, but as
    > short as possible:
    >
    > double char*
    > 10000 1E5
    > 1000.00123 1000.00123
    > ...
    >
    > How can I archive this?
    > %.10f prints too much, and %.10g is too inaccurate.


    sprintf() with "%.20f" into a large enough character array buffer.
    Trim trailing zeros. Do what you need to do with the result.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Oct 12, 2004
    #10
  11. "Jack Klein" <> wrote...
    > On Mon, 11 Oct 2004 11:40:15 +0200, "Gernot Frisch" <>
    > wrote in comp.lang.c++:
    >
    >> Hi,
    >>
    >>
    >> I want to represent a double in char* as accurate as possible, but as
    >> short as possible:
    >>
    >> double char*
    >> 10000 1E5
    >> 1000.00123 1000.00123
    >> ...
    >>
    >> How can I archive this?
    >> %.10f prints too much, and %.10g is too inaccurate.

    >
    > sprintf() with "%.20f" into a large enough character array buffer.
    > Trim trailing zeros. Do what you need to do with the result.


    So, how well is it going to represent 1.000000000009e-22? Just curious,
    I guess...

    Victor
    Victor Bazarov, Oct 12, 2004
    #11

  12. > So, how well is it going to represent 1.000000000009e-22? Just
    > curious,
    > I guess...


    1.000000000009e-22
    Gernot Frisch, Oct 12, 2004
    #12
  13. Gernot Frisch wrote:

    >>So, how well is it going to represent 1.000000000009e-22? Just
    >>curious,
    >>I guess...

    >
    >
    > 1.000000000009e-22



    Is that so? You get that output using the suggested %.20f format?
    REALLY??? What compiler/library are you using?
    Victor Bazarov, Oct 12, 2004
    #13
  14. "Victor Bazarov" <> schrieb im Newsbeitrag
    news:IhRad.4655$09.us.to.verio.net...
    > Gernot Frisch wrote:
    >
    >>>So, how well is it going to represent 1.000000000009e-22? Just
    >>>curious,
    >>>I guess...

    >>
    >>
    >> 1.000000000009e-22

    >
    >
    > Is that so? You get that output using the suggested %.20f format?
    > REALLY??? What compiler/library are you using?




    int main(int, char**)
    {
    double f = 1.000000000009e-22;
    printf("%20.15f %.15g\n", f, f);
    }

    // output:
    // 0.000000000000000 1.000000000009e-022

    gcc 2.95.3-6
    Gernot Frisch, Oct 12, 2004
    #14
  15. Gernot Frisch wrote:
    > "Victor Bazarov" <> schrieb im Newsbeitrag
    > news:IhRad.4655$09.us.to.verio.net...
    >
    >>Gernot Frisch wrote:
    >>
    >>
    >>>>So, how well is it going to represent 1.000000000009e-22? Just
    >>>>curious,
    >>>>I guess...
    >>>
    >>>
    >>>1.000000000009e-22

    >>
    >>
    >>Is that so? You get that output using the suggested %.20f format?
    >>REALLY??? What compiler/library are you using?

    >
    >
    >
    >
    > int main(int, char**)
    > {
    > double f = 1.000000000009e-22;
    > printf("%20.15f %.15g\n", f, f);
    > }
    >
    > // output:
    > // 0.000000000000000 1.000000000009e-022
    >
    > gcc 2.95.3-6


    Well, duh. The 'g' format definitely works. That's not what Jack
    suggested. He suggested the 'f' format and I pointed out that it's
    not going to work for small numbers, as you confirmed.

    V
    Victor Bazarov, Oct 12, 2004
    #15
    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. H.MuthuKumaraRajan
    Replies:
    3
    Views:
    438
    H.MuthuKumaraRajan
    Feb 4, 2004
  2. Whitecrest

    Re: Most accurate browser for CSS?

    Whitecrest, Apr 1, 2004, in forum: HTML
    Replies:
    0
    Views:
    422
    Whitecrest
    Apr 1, 2004
  3. kchayka
    Replies:
    0
    Views:
    417
    kchayka
    Apr 2, 2004
  4. Replies:
    2
    Views:
    560
    Chris Torek
    Jun 17, 2007
  5. xkenneth
    Replies:
    8
    Views:
    336
    Bruno Desthuilliers
    Feb 6, 2008
Loading...

Share This Page