strange results with /fp:fast

Posted by martinus on Stack Overflow See other posts from Stack Overflow or by martinus
Published on 2010-06-15T06:16:05Z Indexed on 2010/06/15 6:22 UTC
Read the original article Hit count: 248

We have some code that looks like this:

inline int calc_something(double x) {
  if (x > 0.0) {
    // do something
    return 1;
  } else {
    // do something else
    return 0;
  }
}

Unfortunately, when using the flag /fp:fast, we get calc_something(0)==1 so we are clearly taking the wrong code path. This only happens when we use the method at multiple points in our code with different parameters, so I think there is some fishy optimization going on here from the compiler (Microsoft Visual Studio 2008, SP1).

Also, the above problem goes away when we change the interface to

inline int calc_something(const double& x) {

But I have no idea why this fixes the strange behaviour. Can anyone explane this behaviour? If I cannot understand what's going on we will have to remove the /fp:fastswitch, but this would make our application quite a bit slower.

© Stack Overflow or respective owner

Related posts about c++

Related posts about visual-studio-2008