max和fmax之间的差异(跨平台编译) [英] The difference between max and fmax (Cross platform compiling)

查看:566
本文介绍了max和fmax之间的差异(跨平台编译)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xcode中,可以很好地编译:

In Xcode, this compiles fine:

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

但是在Visual Studio 2010中会出错,我必须使用max而不是fmax。
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.

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

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

有什么想法吗?

推荐答案

函数 std :: fmax std :: fmin < cmath> 标头(或<$ c来自 的$ c> fmax fmin )是C ++ 11功能以及 Visual Studio 2010 尚不支持的许多其他新数学函数( 2012 也不支持)。因此,出于可移植性考虑,建议用<$ c $ st>:min 和 std :: max < algorithm>

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

实际的区别是 fmin fmax 是处理浮点数的数学函数,起源于 C99 (并且可能 min max 是通用算法,可在支持< (并且可能只是一个简单的(b< a)?b:a 而不是浮点指令,尽管实现甚至可以使用 min max 的专业化来做到这一点,但我对此表示怀疑。

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

对于特殊的浮点参数,它们的行为也略有不同。尽管未完全指定 min max 如何响应 NaN s,我认为(尽管从上面的定义来看,它们应始终返回第一个操作数), fmin fmax ,如果其中一个实现为 NaN ,则总是返回另一个(非- NaN )参数。符合IEEE 754(任何现代PC实现通常都是如此)。

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

这篇关于max和fmax之间的差异(跨平台编译)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆