The difference between max and fmax (Cross platform compiling) The difference between max and fmax (Cross platform compiling) xcode xcode

The difference between max and fmax (Cross platform compiling)


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).