Re: Unexpected truncation behavior

Discussion in 'C Programming' started by mark.bluemel@gmail.com, Jun 28, 2013.

  1. Guest

    On Friday, 28 June 2013 10:23:53 UTC+1, Giuseppe wrote:
    > Dear comp.lang.c,
    >
    > 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*.


    The platform (and the compiler) doesn't really matter...

    >
    > What I get is:
    >
    > "r1 is 10, r2 is 9"
    >
    > instead. Any other compiler and gcc on Linux display the expected message.


    Like I said, platform and compiler don't really matter.

    > Wouldn't the C language "require" the result to be the one I expect?


    No - and if you think it does, you don't understand what floating point means.

    > #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;
    > }


    Section 14 of the C FAQ is probably what you need to read...

    <http://c-faq.com/fp/index.html>

    For more detail, something like <http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html> is probably worthwhile.
    , Jun 28, 2013
    #1
    1. Advertising

  2. wrote:
    > On Friday, 28 June 2013 10:23:53 UTC+1, Giuseppe wrote:


    (snip)
    >> I tried to understand why I can't get the message:
    >> "r1 is 10, r2 is 10"


    > The platform (and the compiler) doesn't really matter...


    Well, except that it sometimes does.

    Two values that don't have exact binary fractions will either
    round up or down. In this case, if they both round down, then the
    sum will be less than the exact decimal sum.

    Not counting all the non-IEEE floating point forms (which C
    allows for, and could also cause 9 instead of 10), some processors
    use extra precision for intermediate values. While extra precision
    most often gives better results, it can also give surprising
    results in some cases.

    (snip)
    > Like I said, platform and compiler don't really matter.


    >> Wouldn't the C language "require" the result to be the
    >> one I expect?


    > No - and if you think it does, you don't understand what
    > floating point means.


    Yes. If you want exact results you integer arithmetic.

    There has been a claim that a conforming Fortran compiler could
    return 42. for all floating point expressions. I don't believe
    that C will allow for that, but it does allow for binary floating
    point rounding.

    -- glen
    glen herrmannsfeldt, Jun 28, 2013
    #2
    1. Advertising

  3. James Kuyper Guest

    On 06/28/2013 02:53 PM, glen herrmannsfeldt wrote:
    ....
    > There has been a claim that a conforming Fortran compiler could
    > return 42. for all floating point expressions. I don't believe
    > that C will allow for that,


    You're right - the C standard only allows arbitrarily inaccurate results
    from floating point expressions that involve one of the following:
    * +, -, *, or /
    * functions from <math.h> or <complex.h> that return floating point values
    * conversions between strings and floating point values by functions in
    <stdio.h>, <stdlib.h>, or <wchar.h>

    If you can figure out something useful to do with floating point math
    that doesn't involve any of those things (good luck with that!), then
    the accuracy requirements of the C standard are actually fairly strict.
    --
    James Kuyper
    James Kuyper, Jun 28, 2013
    #3
  4. James Kuyper <> writes:
    > On 06/28/2013 02:53 PM, glen herrmannsfeldt wrote:
    > ...
    >> There has been a claim that a conforming Fortran compiler could
    >> return 42. for all floating point expressions. I don't believe
    >> that C will allow for that,

    >
    > You're right - the C standard only allows arbitrarily inaccurate results
    > from floating point expressions that involve one of the following:
    > * +, -, *, or /
    > * functions from <math.h> or <complex.h> that return floating point values
    > * conversions between strings and floating point values by functions in
    > <stdio.h>, <stdlib.h>, or <wchar.h>
    >
    > If you can figure out something useful to do with floating point math
    > that doesn't involve any of those things (good luck with that!), then
    > the accuracy requirements of the C standard are actually fairly strict.


    I was thinking you might be able to play some ugly games constructing
    floating-point constants with the preprocessor, but even that could give
    you arbitrarily inaccurate results.

    N1570 6.4.4.2p7:

    The translation-time conversion of floating constants should
    match the execution-time conversion of character strings
    by library functions, such as strtod, given matching inputs
    suitable for both conversions, the same result format, and
    default execution-time rounding.

    And even that's only "Recommended practice". (Accuracy for strtod is
    also "Recommended practice".)

    --
    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 29, 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:
    503
    Mark Fitzpatrick
    Jul 1, 2005
  2. Marcelo

    MySql Data Truncation

    Marcelo, Dec 19, 2005, in forum: Java
    Replies:
    3
    Views:
    17,123
    Roedy Green
    Dec 21, 2005
  3. Replies:
    3
    Views:
    1,243
    Mike Beaty
    Oct 11, 2006
  4. glen herrmannsfeldt

    Re: Unexpected truncation behavior

    glen herrmannsfeldt, Jun 28, 2013, in forum: C Programming
    Replies:
    23
    Views:
    369
    glen herrmannsfeldt
    Jun 30, 2013
  5. Keith Thompson

    Re: Unexpected truncation behavior

    Keith Thompson, Jun 28, 2013, in forum: C Programming
    Replies:
    3
    Views:
    200
    Keith Thompson
    Jun 28, 2013
Loading...

Share This Page