xcode max和fmax的区别(跨平台编译)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16584558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:23:20  来源:igfitidea点击:

The difference between max and fmax (Cross platform compiling)

c++xcodevisual-studio-2010

提问by Christian Rau

In Xcode, this compiles fine:

在 Xcode 中,这编译得很好:

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

However in Visual Studio 2010 it errors, and I have to use max instead of fmax. distanceSqrd and cRadius are also both floats.

但是在 Visual Studio 2010 中它出错了,我必须使用 max 而不是 fmax。distanceSqrd 和 cRadius 也是浮点数。

It's the only part of my code that doesn't seamlessly cross compile...

这是我的代码中唯一不能无缝交叉编译的部分......

Any ideas why?

任何想法为什么?

回答by Christian Rau

The functions std::fmaxand std::fminfrom the <cmath>header (or fmaxand fminfrom <math.h>) are a C++11 feature and along with many other new mathematical functions one that Visual Studio 2010doesn't support yet (neither does 2012). So for portability it is advisable to replace them by std::minand std::maxfrom <algorithm>.

函数std::fmaxstd::fmin来自<cmath>标头(或fmaxfmin来自<math.h>)是 C++11 特性,以及许多其他新的数学函数,Visual Studio 2010尚不支持(2012也不支持)。因此,为了可移植性,建议将它们替换为std::minstd::maxfrom <algorithm>

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

实际的区别在于,fminfmax是处理浮点数的数学函数并源自C99(并且可能可能的情况下由实际的专用 CPU 指令内部实现),而minmax是可用于任何类型支持的通用算法<(并且可能只是一个简单的(b<a) ? b : a而不是浮点指令,尽管实现甚至可以通过专门化minand 来做到这一点max,但我对此表示怀疑)。

They also behave slightlydifferent for special floating point arguments. While it is not entirely specified how minand maxwill respond to NaNs, I think (though from the above definition they should always return the 1st operand), fminand fmaxare 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).

对于特殊的浮点参数,它们的行为也略有不同。虽然没有完全指定如何min以及max将如何响应NaNs,但我认为(尽管根据上面的定义,它们应该始终返回第一个操作数),fmin并且fmax明确指定始终返回另一个(非NaN)参数NaN,如果实现符合 IEEE 754(任何现代 PC 实现通常都是如此)。