Calculations inline with conditionals

Discussion in 'C++' started by jdwyer3, Oct 28, 2005.

  1. jdwyer3

    jdwyer3 Guest

    I'm using Visual C++.Net 2003, and I have noticed a little quirkiness
    when using a conditional based on the comparison of two calculations
    that are performed inline with the conditional.

    For example, if I run this code:

    double angle1;
    double angle2;

    angle1 = 45;
    angle2 = 45;

    if( tan( angle1 ) < tan( angle2 ) )
    cout << "Angle 1";
    else
    cout << "Angle 2";

    I would expect the conditional to return false and the output to be
    "Angle 2". What actually happens, however, is that the output is
    "Angle 1".

    If, instead, I write the code so as to perform the calculation prior to
    the conditional like so:

    double angle1;
    double angle2;
    double temp1;
    double temp2;

    angle1 = 45;
    angle2 = 45;

    temp1 = tan( angle1 );
    temp2 = tan( angle2 );

    if( temp1 < temp2 )
    cout << "Angle 1";
    else
    cout << "Angle 2";

    the code runs as I would expect and the output is "Angle 2".

    Can somebody explain what I am missing? What is the difference in the
    way the two snippets work that would cause what I believe to be two
    functionally identical pieces of code to operate differently.
    jdwyer3, Oct 28, 2005
    #1
    1. Advertising

  2. jdwyer3 wrote:
    > I'm using Visual C++.Net 2003

    [snip]
    > Can somebody explain what I am missing?


    Look up the /Op (Improve Float consistency) option
    in your compilers documentation.


    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Oct 28, 2005
    #2
    1. Advertising

  3. jdwyer3

    John Ratliff Guest

    John Ratliff, Oct 28, 2005
    #3
  4. jdwyer3

    Kai-Uwe Bux Guest

    John Ratliff wrote:

    > jdwyer3 wrote:
    >> I'm using Visual C++.Net 2003, and I have noticed a little quirkiness
    >> when using a conditional based on the comparison of two calculations
    >> that are performed inline with the conditional.
    >>

    >
    > http://www.parashift.com/c -faq-lite/newbie.html#faq-29.17


    If I was running into the problem of the OP, reading that FAQ item would not
    give me piece of mind. Have a closer look at the code under consideration:

    double angle1;
    double angle2;

    angle1 = 45;
    angle2 = 45;

    if( tan( angle1 ) < tan( angle2 ) )
    cout << "Angle 1";
    else
    cout << "Angle 2";

    If the program prints Angle 1, it takes license to evaluate tan(45)
    inconsistently. Note that after initialization angle1 == angle2 is
    guaranteed by the standard. Yet the OP finds tan(angle1) != tan(angle2).

    Now, look at the code from the FAQ:

    double x = 1.0 / 10.0;
    double y = x * 10.0;
    if (y != 1.0)
    std::cout << "surprise: " << y << " != 1\n";

    Well, it is unreasonable to expect to different floating point calculations
    to yield identical results just because they happen to be mathematically
    equivalent -- unreasonable at least once one considers how real numbers are
    represented in the computer. However, it is reasonable to expect a function
    call to tan() to return identical values for identical arguments: this is
    comparing the results from performing the same calculation two times.

    I agree that the standard does no guarantee the natural behavior of math
    functions (in fact it does not guarantee anything meaningful). Also, there
    are good reasons why one might not want to make any stronger guarantees
    here: The compiler can inline the function call and do optimizations that
    take into account the callers context. For floating point arithmetic, we
    might want to allow for those optimizations to slightly change the results.
    However, that issue is not addressed in the FAQ. Maybe one should add this
    case to the list of warnings:

    Double-check your assumptions, including "obvious" things like how to
    compute averages, how to solve quadratic equations, etc., etc. Do not
    assume the formulas you learned in High School will work with floating
    point numbers!

    to include "obvious things like two calls to tan(45.0) will evaluate to
    equal results."


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 28, 2005
    #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. Abhi
    Replies:
    2
    Views:
    719
    E. Robert Tisdale
    Jul 3, 2003
  2. Alvin
    Replies:
    7
    Views:
    458
    E. Robert Tisdale
    May 6, 2005
  3. Joshua Ginsberg

    Inline Conditionals?

    Joshua Ginsberg, Aug 24, 2004, in forum: Python
    Replies:
    15
    Views:
    3,820
    Martin Maney
    Sep 6, 2004
  4. Robert Brewer

    RE: Inline Conditionals?

    Robert Brewer, Aug 26, 2004, in forum: Python
    Replies:
    1
    Views:
    316
    Alex Martelli
    Aug 26, 2004
  5. Nathan Sokalski

    Inline Calculations for Databinding

    Nathan Sokalski, Aug 20, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    451
    Alvin Bruney [MVP]
    Aug 26, 2007
Loading...

Share This Page