The difference between max and fmax (Cross platform compiling)

前端 未结 1 861
孤城傲影
孤城傲影 2020-12-31 12:44

In Xcode, this compiles fine:

float falloff = fmin(1.0, fmax(0.0, distanceSqrd/cRadius));

However in Visual Studio 2010 it errors, and I ha

相关标签:
1条回答
  • The functions std::fmax and std::fmin from the <cmath> header (or fmax and fmin from <math.h>) are a C++11 feature and along with many other new mathematical functions one that Visual Studio 2010 doesn't support yet (neither does 2012). So for portability it is advisable to replace them by std::min and std::max from <algorithm>.

    The actual difference is, that fmin and fmax are mathematical functions working on floating point numbers and originating from C99 (and might be implemented intrisically by actual specialized CPU instructions where possible), while min and max are general algorithms usable on any type supporting < (and are probably just a simple (b<a) ? b : a instead of a floating point instruction, though an implementation could even do that with a specialization of min and max, but I doubt this).

    They also behave slightly different for special floating point arguments. While it is not entirely specified how min and max will respond to NaNs, I think (though from the above definition they should always return the 1st operand), fmin and fmax are clearly specified to always return the other (non-NaN) argument when one is NaN, if the implementation is IEEE 754 conformant (which any modern PC implementation usually is).

    0 讨论(0)
提交回复
热议问题