Problem with printf formats

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guenther Sohler

    Problem with printf formats

    I have following code:

    int main(void)
    {
    printf("%.3lf\n ",-2158470*0.001);
    }

    it prints

    -2158.470

    How can i disable '0' at the end, if they are not needed ?

    Is there a possibility just to write %lf and just print as many digits
    as needed to represent the number ?






  • usenet@zevv.nl

    #2
    Re: Problem with printf formats


    Guenther Sohle said:[color=blue]
    >[/color]
    (...)[color=blue]
    > printf("%.3lf\n ",-2158470*0.001);[/color]
    (...)[color=blue]
    > it prints
    >
    > -2158.470
    >
    > How can i disable '0' at the end, if they are not needed ? Is there a
    > possibility just to write %lf and just print as many digits as needed to
    > represent the number ?[/color]

    The problem is not in the printf() function, but in the way floating point
    numbers are represented by your computer. Due to rounding errors, floating
    point numbers hardly ever have the 'exact' value you would expect. Search
    google for 'floating point' for some details, or read this wikipedia page:
    http://en.wikipedia.org/wiki/Floating_point.

    It is up to you to make the choice how many digits are significant in a
    number, so that is why you should tell printf() about this.


    --
    :wq
    ^X^Cy^K^X^C^C^C ^C

    Comment

    • Chuck F.

      #3
      Re: Problem with printf formats

      Guenther Sohler wrote:[color=blue]
      >
      > I have following code:
      >
      > int main(void)
      > {
      > printf("%.3lf\n ",-2158470*0.001);
      > }
      >
      > it prints
      >
      > -2158.470
      >
      > How can i disable '0' at the end, if they are not needed ?
      >
      > Is there a possibility just to write %lf and just print as many
      > digits as needed to represent the number ?[/color]

      You are asking the wrong question. The trailing zero is needed to
      represent the number. Otherwise how could you distinguish it from:

      printf("%.2f\n" , -2158471 * 0.001)

      (always assuming that -2158471 does not create an overflow)

      Your question, I believe, is "how to suppress trailing zeroes". A
      little thought and an intermediary buffer should give you a
      suitable answer for that.

      --
      A: Because it fouls the order in which people normally read text.
      Q: Why is top-posting such a bad thing?
      A: Top-posting.
      Q: What is the most annoying thing on usenet and in e-mail?

      Comment

      • Jirka Klaue

        #4
        Re: Problem with printf formats

        Guenther Sohler:
        [color=blue]
        > int main(void)
        > {
        > printf("%.3lf\n ",-2158470*0.001);
        > }
        >
        > it prints
        >
        > -2158.470
        >
        > How can i disable '0' at the end, if they are not needed ?
        >
        > Is there a possibility just to write %lf and just print as many digits
        > as needed to represent the number ?[/color]

        The l in %lf is superfluous, just use %f.
        To avoid the trailing zeros try %g.

        Jirka

        Comment

        • Guenther Sohler

          #5
          Re: Problem with printf formats

          On Tue, 20 Dec 2005 13:56:44 +0100, Jirka Klaue wrote:
          [color=blue]
          > Guenther Sohler:
          >[color=green]
          >> int main(void)
          >> {
          >> printf("%.3lf\n ",-2158470*0.001);
          >> }
          >>
          >> it prints
          >>
          >> -2158.470
          >>
          >> How can i disable '0' at the end, if they are not needed ?
          >>
          >> Is there a possibility just to write %lf and just print as many digits
          >> as needed to represent the number ?[/color]
          >
          > The l in %lf is superfluous, just use %f.[/color]
          Thank you for the info.[color=blue]
          > To avoid the trailing zeros try %g.[/color]

          I also tried %g:
          int main(void)
          {
          printf("%g\n",-2158475*0.001);
          }

          which results in
          -2158.47

          one digit is missed! this is a killing issue for me!

          how can i have the proper value displayed in all cases ?[color=blue]
          >
          > Jirka[/color]

          Comment

          • Guenther Sohler

            #6
            Re: Problem with printf formats

            On Tue, 20 Dec 2005 12:08:23 +0000, usenet wrote:
            [color=blue]
            >
            > Guenther Sohle said:[color=green]
            >>[/color]
            > (...)[color=green]
            >> printf("%.3lf\n ",-2158470*0.001);[/color]
            > (...)[color=green]
            >> it prints
            >>
            >> -2158.470
            >>
            >> How can i disable '0' at the end, if they are not needed ? Is there a
            >> possibility just to write %lf and just print as many digits as needed to
            >> represent the number ?[/color]
            >
            > The problem is not in the printf() function, but in the way floating point
            > numbers are represented by your computer. Due to rounding errors, floating
            > point numbers hardly ever have the 'exact' value you would expect. Search
            > google for 'floating point' for some details, or read this wikipedia page:
            > http://en.wikipedia.org/wiki/Floating_point.
            >
            > It is up to you to make the choice how many digits are significant in a
            > number, so that is why you should tell printf() about this.[/color]

            I know, but the accuracy of double should still good enough to fulfill my
            purpose!


            Comment

            • Guenther Sohler

              #7
              Re: Problem with printf formats

              On Tue, 20 Dec 2005 07:06:42 -0500, Chuck F. wrote:
              [color=blue]
              > Guenther Sohler wrote:[color=green]
              > >
              >> I have following code:
              >>
              >> int main(void)
              >> {
              >> printf("%.3lf\n ",-2158470*0.001);
              >> }
              >>
              >> it prints[/color]
              >
              > Your question, I believe, is "how to suppress trailing zeroes". A
              > little thought and an intermediary buffer should give you a
              > suitable answer for that.[/color]

              Thank you for the information. Yes, I also keep this in mind,
              but i use %f very often in my code ( > 100) and to suitable
              handle all them, I would have to write my own printf.
              I dont like to do this unless I am really sure there is no other way


              Comment

              • Jirka Klaue

                #8
                Re: Problem with printf formats

                Guenther Sohler:
                [color=blue]
                > I also tried %g:
                > int main(void)
                > {
                > printf("%g\n",-2158475*0.001);
                > }
                >
                > which results in
                > -2158.47
                >
                > one digit is missed! this is a killing issue for me![/color]

                Well, then it's time for you to read the appropriate part
                of the standard.

                ISO/IEC 9899:TC2
                7.19.6.1#8
                g,G A double argument representing a floating-point number is converted
                in style f or e (or in style F or E in the case of a G conversion
                specifier), depending on the value converted and the precision.
                Let P equal the precision if nonzero, 6 if the precision is omitted,
                or 1 if the precision is zero. Then, if a conversion with style E
                would have an exponent of X:
                - if P > X ≥ -4, the conversion is with style f (or F) and
                precision P - (X + 1).
                - otherwise, the conversion is with style e (or E) and precision P - 1.
                Finally, unless the # flag is used, any trailing zeros are removed from
                the fractional portion of the result and the decimal-point character is
                removed if there is no fractional portion remaining.

                So try %.9g or something like that.
                [color=blue]
                > how can i have the proper value displayed in all cases ?[/color]

                This could be difficult depending on the meaning of proper.

                Jirka

                Comment

                • Dik T. Winter

                  #9
                  Re: Problem with printf formats

                  In article <pan.2005.12.20 .13.34.19.27062 7@newlogic.com> Guenther Sohler <guenther.sohle r@newlogic.com> writes:
                  ....[color=blue]
                  > int main(void)
                  > {
                  > printf("%g\n",-2158475*0.001);
                  > }
                  >
                  > which results in
                  > -2158.47
                  >
                  > one digit is missed! this is a killing issue for me!
                  >
                  > how can i have the proper value displayed in all cases ?[/color]

                  But, what *is* the proper value? In binary it is:
                  -10000110110.011 100110011001100 11...
                  and that is what is stored on the computer, most decimal numbers are
                  not exactly representable in binary. So what will be stored is a
                  rounded value, and the value in memory is:
                  -2158.4749999999 999090505298227 071762084960937 5
                  so what should the system do?

                  --
                  dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
                  home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

                  Comment

                  • Old Wolf

                    #10
                    Re: Problem with printf formats

                    Jirka Klaue wrote:
                    [color=blue]
                    > Guenther Sohler:[color=green]
                    >> Is there a possibility just to write %lf and just print as many digits
                    >> as needed to represent the number ?[/color]
                    >
                    > The l in %lf is superfluous, just use %f.[/color]

                    It is superfluous in C99, and is an error in C89. But most
                    C89 compilers allow it anyway.

                    Comment

                    • Tatu Portin

                      #11
                      Re: Problem with printf formats

                      Guenther Sohler wrote:
                      [color=blue]
                      > I have following code:
                      >
                      > int main(void)
                      > {
                      > printf("%.3lf\n ",-2158470*0.001);
                      > }
                      >
                      > it prints
                      >
                      > -2158.470
                      >
                      > How can i disable '0' at the end, if they are not needed ?
                      >
                      > Is there a possibility just to write %lf and just print as many
                      > digits as needed to represent the number ?[/color]


                      Question "How can I disable '0' at the end" indicates somekind of
                      misconception of floating point numbers.

                      Count of digits in a number indicates the accuracy of the number. So,
                      if you just remove zero from a number, you decrease accuracy of the
                      number.

                      For example:
                      -2158.5 could be result of rounding -2158.470, but it would be wrong
                      to add zeros to -2158.5 because the accurate amount was -2158.470.

                      The significance of zeros in indicating accuracy can be only perceived
                      when handling numbers on right side of decimal point.
                      For example:
                      -2000 could be result of rounding -2158 or it could be the exact
                      amount. You cannot tell if -2000 has one, two, three or four
                      significant digits, i.e. if -2000 is accurate to first, second, third
                      or fourth digit.
                      This is why scientific notation is used to represent quantities.
                      E.g. -2.000e3 is different from -2e3 in accuracy.


                      --
                      It's bit too late to be clear, but I hope I made my point through.

                      C faq: http://www.eskimo.com/~scs/C-faq/top.html
                      Reference: http://www.acm.uiuc.edu/webmonkeys/book/c_guide/
                      Coding standards: http://www.psgd.org/paul/docs/cstyle/cstyle.htm

                      Comment

                      • Andrey Tarasevich

                        #12
                        Re: Problem with printf formats

                        Guenther Sohler wrote:[color=blue]
                        > ...
                        > I know, but the accuracy of double should still good enough to fulfill my
                        > purpose!
                        > ...[/color]

                        What exactly do you mean by "good enough" and "your purpose" in this case? Most
                        of the time floating-point numbers that have innocent-looking decimal
                        representations have infinitely long [periodic] representations in traditional
                        binary positional notation. For example, both '0.001' and '2158.47' are such
                        numbers. It is impossible to represent them precisely in 'double', which
                        probably means that 'double's precision (or precision of any type of same
                        structure, no matter how large) is definitely not "good enough" (assuming that I
                        understood your "good enough" correctly).

                        --
                        Best regards,
                        Andrey Tarasevich

                        Comment

                        • Gordon Burditt

                          #13
                          Re: Problem with printf formats

                          >> I know, but the accuracy of double should still good enough to fulfill my[color=blue][color=green]
                          >> purpose!
                          >> ...[/color]
                          >
                          >What exactly do you mean by "good enough" and "your purpose" in this case?[/color]
                          (I'm not the original poster).

                          It is difficult and very expensive to measure most any physical
                          quantity to more significant digits than are present in a double.
                          (15 in a typical IEEE float implementation) . Exceptions may include
                          currency and time.

                          Gordon L. Burditt

                          Comment

                          • Simon Biber

                            #14
                            Re: Problem with printf formats

                            Guenther Sohler wrote:[color=blue]
                            > I have following code:
                            >
                            > int main(void)
                            > {
                            > printf("%.3lf\n ",-2158470*0.001);
                            > }
                            >
                            > it prints
                            >
                            > -2158.470
                            >
                            > How can i disable '0' at the end, if they are not needed ?
                            >
                            > Is there a possibility just to write %lf and just print as many digits
                            > as needed to represent the number ?[/color]

                            I can write a function to do it.

                            #include <stdio.h>
                            #include <string.h>

                            char *sprintd(char *buf, size_t buf_size, double val, int precision)
                            {
                            /* write value into string */
                            snprintf(buf, buf_size, "%.*f", precision, val);

                            /* find decimal point */
                            char *p = strchr(buf, '.'), *q;

                            /* remove trailing zeros */
                            if(p) for(q = strchr(p, 0) - 1; *q == '0'; --q) *q = 0;

                            /* remove trailing decimal point */
                            if(p && p[1] == 0) p[0] = 0;

                            return buf;
                            }

                            int main(void)
                            {
                            char buf[100];
                            printf("%s\n", sprintd(buf, sizeof buf, 355.0 / 113.0, 15));
                            return 0;
                            }

                            --
                            Simon.

                            Comment

                            • vishnu

                              #15
                              Re: Problem with printf formats

                              write %.2lf inn place of %.3lf

                              Comment

                              Working...