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
The difference between max and fmax (Cross platform compiling)
提问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::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 2010doesn't support yet (neither does 2012). So for portability it is advisable to replace them by std::min
and std::max
from <algorithm>
.
函数std::fmax
和std::fmin
来自<cmath>
标头(或fmax
和fmin
来自<math.h>
)是 C++11 特性,以及许多其他新的数学函数,Visual Studio 2010尚不支持(2012也不支持)。因此,为了可移植性,建议将它们替换为std::min
和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 mightbe 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).
实际的区别在于,fmin
和fmax
是处理浮点数的数学函数并源自C99
(并且可能在可能的情况下由实际的专用 CPU 指令内部实现),而min
和max
是可用于任何类型支持的通用算法<
(并且可能只是一个简单的(b<a) ? b : a
而不是浮点指令,尽管实现甚至可以通过专门化min
and 来做到这一点max
,但我对此表示怀疑)。
They also behave slightlydifferent for special floating point arguments. While it is not entirely specified how min
and max
will respond to NaN
s, 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).
对于特殊的浮点参数,它们的行为也略有不同。虽然没有完全指定如何min
以及max
将如何响应NaN
s,但我认为(尽管根据上面的定义,它们应该始终返回第一个操作数),fmin
并且fmax
明确指定始终返回另一个(非NaN
)参数NaN
,如果实现符合 IEEE 754(任何现代 PC 实现通常都是如此)。