Re: Unexpected truncation behavior

Discussion in 'C Programming' started by Keith Thompson, Jun 28, 2013.

  1. Giuseppe <> writes:
    > I tried to understand why I can't get the message:
    >
    > "r1 is 10, r2 is 10"
    >
    > as result from the code below when I compile it with gcc4.8.1 *on Windows*.
    > What I get is:
    >
    > "r1 is 10, r2 is 9"
    >
    > instead. Any other compiler and gcc on Linux display the expected message.
    > Wouldn't the C language "require" the result to be the one I expect?
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > float a = 3.3f, b = 3.3f, c = 3.4f;
    > double d = 3.3, e = 3.3, f = 3.4;
    > int r1, r2;
    > r1 = a + b + c;
    > r2 = d + e + f;
    > printf("r1 is %d, r2 is %d\n", r1, r2);
    > return 0;
    > }


    Here's a variant of your program that shows more precision. The
    behavior of printf with extra precision is likely to vary from one
    implementation to another; GNU libc shows the exact value in this case.

    #include <stdio.h>
    int main(void)
    {
    float a = 3.3f, b = 3.3f, c = 3.4f;
    double d = 3.3, e = 3.3, f = 3.4;
    int r1, r2;
    r1 = a + b + c;
    r2 = d + e + f;
    printf("a = %.24f\nb = %.24f\nc = %.24f\n", a, b, c);
    printf("d = %.50f\ne = %.50f\nf = %.50f\n", a, b, c);
    printf("a + b + c = %.24f\n", a + b + c);
    printf("d + e + f = %.24f\n", d + e + f);
    printf("r1 is %d, r2 is %d\n", r1, r2);
    return 0;
    }

    There's the output I get on Linux, x86_64, gcc 4.7.2:

    a = 3.299999952316284179687500
    b = 3.299999952316284179687500
    c = 3.400000095367431640625000
    d = 3.29999995231628417968750000000000000000000000000000
    e = 3.29999995231628417968750000000000000000000000000000
    f = 3.40000009536743164062500000000000000000000000000000
    a + b + c = 10.000000000000000000000000
    d + e + f = 10.000000000000000000000000
    r1 is 10, r2 is 10

    On Windows 7, Cygwin, gcc 4.5.3 (Windows is 64 bits, but Cygwin uses 32
    bits):

    a = 3.299999952316284179687500
    b = 3.299999952316284179687500
    c = 3.400000095367431640625000
    d = 3.29999995231628417968750000000000000000000000000000
    e = 3.29999995231628417968750000000000000000000000000000
    f = 3.40000009536743164062500000000000000000000000000000
    a + b + c = 10.000000000000000000000000
    d + e + f = 10.000000000000000000000000
    r1 is 10, r2 is 9

    On Windws/Cygwin, the output varies with optimization level.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 28, 2013
    #1
    1. Advertising

  2. Keith Thompson

    Lew Pitcher Guest

    On Friday 28 June 2013 11:51, in comp.lang.c, wrote:

    > Giuseppe <> writes:
    >> I tried to understand why I can't get the message:
    >>
    >> "r1 is 10, r2 is 10"
    >>
    >> as result from the code below when I compile it with gcc4.8.1 *on
    >> Windows*. What I get is:
    >>
    >> "r1 is 10, r2 is 9"
    >>
    >> instead. Any other compiler and gcc on Linux display the expected
    >> message. Wouldn't the C language "require" the result to be the one I
    >> expect?
    >>
    >> #include <stdio.h>
    >> int main(void)
    >> {
    >> float a = 3.3f, b = 3.3f, c = 3.4f;
    >> double d = 3.3, e = 3.3, f = 3.4;
    >> int r1, r2;
    >> r1 = a + b + c;
    >> r2 = d + e + f;
    >> printf("r1 is %d, r2 is %d\n", r1, r2);
    >> return 0;
    >> }

    >
    > Here's a variant of your program that shows more precision. The
    > behavior of printf with extra precision is likely to vary from one
    > implementation to another; GNU libc shows the exact value in this case.
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > float a = 3.3f, b = 3.3f, c = 3.4f;
    > double d = 3.3, e = 3.3, f = 3.4;
    > int r1, r2;
    > r1 = a + b + c;
    > r2 = d + e + f;
    > printf("a = %.24f\nb = %.24f\nc = %.24f\n", a, b, c);
    > printf("d = %.50f\ne = %.50f\nf = %.50f\n", a, b, c);


    Shouldn't the above line be
    printf("d = %.50f\ne = %.50f\nf = %.50f\n", d, e, f);
    = = =
    ?


    > printf("a + b + c = %.24f\n", a + b + c);
    > printf("d + e + f = %.24f\n", d + e + f);
    > printf("r1 is %d, r2 is %d\n", r1, r2);
    > return 0;
    > }
    >
    > There's the output I get on Linux, x86_64, gcc 4.7.2:
    >
    > a = 3.299999952316284179687500
    > b = 3.299999952316284179687500
    > c = 3.400000095367431640625000
    > d = 3.29999995231628417968750000000000000000000000000000
    > e = 3.29999995231628417968750000000000000000000000000000
    > f = 3.40000009536743164062500000000000000000000000000000
    > a + b + c = 10.000000000000000000000000
    > d + e + f = 10.000000000000000000000000
    > r1 is 10, r2 is 10
    >
    > On Windows 7, Cygwin, gcc 4.5.3 (Windows is 64 bits, but Cygwin uses 32
    > bits):
    >
    > a = 3.299999952316284179687500
    > b = 3.299999952316284179687500
    > c = 3.400000095367431640625000
    > d = 3.29999995231628417968750000000000000000000000000000
    > e = 3.29999995231628417968750000000000000000000000000000
    > f = 3.40000009536743164062500000000000000000000000000000
    > a + b + c = 10.000000000000000000000000
    > d + e + f = 10.000000000000000000000000
    > r1 is 10, r2 is 9
    >
    > On Windws/Cygwin, the output varies with optimization level.
    >
    > --
    > Keith Thompson (The_Other_Keith)
    > <http://www.ghoti.net/~kst> Working, but not speaking, for JetHead
    > Development, Inc.
    > "We must do something. This is something. Therefore, we must do this."
    > -- Antony Jay and Jonathan Lynn, "Yes Minister"


    --
    Lew Pitcher
    "In Skills, We Trust"
    Lew Pitcher, Jun 28, 2013
    #2
    1. Advertising

  3. Keith Thompson

    Lew Pitcher Guest

    On Friday 28 June 2013 11:55, in comp.lang.c,
    wrote:

    > On Friday 28 June 2013 11:51, in comp.lang.c, wrote:

    [snip]
    >> Here's a variant of your program that shows more precision. The
    >> behavior of printf with extra precision is likely to vary from one
    >> implementation to another; GNU libc shows the exact value in this case.
    >>
    >> #include <stdio.h>
    >> int main(void)
    >> {
    >> float a = 3.3f, b = 3.3f, c = 3.4f;
    >> double d = 3.3, e = 3.3, f = 3.4;
    >> int r1, r2;
    >> r1 = a + b + c;
    >> r2 = d + e + f;
    >> printf("a = %.24f\nb = %.24f\nc = %.24f\n", a, b, c);
    >> printf("d = %.50f\ne = %.50f\nf = %.50f\n", a, b, c);

    >
    > Shouldn't the above line be
    > printf("d = %.50f\ne = %.50f\nf = %.50f\n", d, e, f);
    > = = =
    > ?
    >
    >
    >> printf("a + b + c = %.24f\n", a + b + c);
    >> printf("d + e + f = %.24f\n", d + e + f);
    >> printf("r1 is %d, r2 is %d\n", r1, r2);
    >> return 0;
    >> }
    >>
    >> There's the output I get on Linux, x86_64, gcc 4.7.2:
    >>
    >> a = 3.299999952316284179687500
    >> b = 3.299999952316284179687500
    >> c = 3.400000095367431640625000
    >> d = 3.29999995231628417968750000000000000000000000000000
    >> e = 3.29999995231628417968750000000000000000000000000000
    >> f = 3.40000009536743164062500000000000000000000000000000
    >> a + b + c = 10.000000000000000000000000
    >> d + e + f = 10.000000000000000000000000
    >> r1 is 10, r2 is 10



    Here's the output I get with your original code on Linux, x86_32, gcc 4.2.4

    a = 3.299999952316284179687500
    b = 3.299999952316284179687500
    c = 3.400000095367431640625000
    d = 3.29999995231628417968750000000000000000000000000000
    e = 3.29999995231628417968750000000000000000000000000000
    f = 3.40000009536743164062500000000000000000000000000000
    a + b + c = 10.000000000000000000000000
    d + e + f = 10.000000000000000000000000
    r1 is 10, r2 is 9

    Here's the output I get with the corrected code

    a = 3.299999952316284179687500
    b = 3.299999952316284179687500
    c = 3.400000095367431640625000
    d = 3.29999999999999982236431605997495353221893310546875
    e = 3.29999999999999982236431605997495353221893310546875
    f = 3.39999999999999991118215802998747676610946655273438
    a + b + c = 10.000000000000000000000000
    d + e + f = 10.000000000000000000000000
    r1 is 10, r2 is 9


    Here is the corrected code...

    #include <stdio.h>
    int main(void)
    {
    float a = 3.3f, b = 3.3f, c = 3.4f;
    double d = 3.3, e = 3.3, f = 3.4;
    int r1, r2;
    r1 = a + b + c;
    r2 = d + e + f;
    printf("a = %.24f\nb = %.24f\nc = %.24f\n", a, b, c);
    printf("d = %.50f\ne = %.50f\nf = %.50f\n", d, e, f);
    printf("a + b + c = %.24f\n", a + b + c);
    printf("d + e + f = %.24f\n", d + e + f);
    printf("r1 is %d, r2 is %d\n", r1, r2);
    return 0;
    }

    HTH
    --
    Lew Pitcher
    "In Skills, We Trust"
    Lew Pitcher, Jun 28, 2013
    #3
  4. Lew Pitcher <> writes:
    > On Friday 28 June 2013 11:51, in comp.lang.c, wrote:

    [...]
    >> #include <stdio.h>
    >> int main(void)
    >> {
    >> float a = 3.3f, b = 3.3f, c = 3.4f;
    >> double d = 3.3, e = 3.3, f = 3.4;
    >> int r1, r2;
    >> r1 = a + b + c;
    >> r2 = d + e + f;
    >> printf("a = %.24f\nb = %.24f\nc = %.24f\n", a, b, c);
    >> printf("d = %.50f\ne = %.50f\nf = %.50f\n", a, b, c);

    >
    > Shouldn't the above line be
    > printf("d = %.50f\ne = %.50f\nf = %.50f\n", d, e, f);
    > = = =
    > ?


    Yes, absolutely, thanks for noticing that. (I was wondering why I
    wasn't getting many more digits for double; I should have investigated
    that).

    With that fix, I get this output on Linux x86_64:

    a = 3.299999952316284179687500
    b = 3.299999952316284179687500
    c = 3.400000095367431640625000
    d = 3.29999999999999982236431605997495353221893310546875
    e = 3.29999999999999982236431605997495353221893310546875
    f = 3.39999999999999991118215802998747676610946655273438
    a + b + c = 10.000000000000000000000000
    d + e + f = 10.000000000000000000000000
    r1 is 10, r2 is 10

    I don't have access to my Windows machine at the moment; I'll check that
    later.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 28, 2013
    #4
    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. manas

    Text Truncation

    manas, Jul 1, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    512
    Mark Fitzpatrick
    Jul 1, 2005
  2. Marcelo

    MySql Data Truncation

    Marcelo, Dec 19, 2005, in forum: Java
    Replies:
    3
    Views:
    17,149
    Roedy Green
    Dec 21, 2005
  3. Replies:
    3
    Views:
    1,266
    Mike Beaty
    Oct 11, 2006
  4. Replies:
    3
    Views:
    189
    Keith Thompson
    Jun 29, 2013
  5. glen herrmannsfeldt

    Re: Unexpected truncation behavior

    glen herrmannsfeldt, Jun 28, 2013, in forum: C Programming
    Replies:
    23
    Views:
    394
    glen herrmannsfeldt
    Jun 30, 2013
Loading...

Share This Page