C++ 我什么时候使用 fabs,什么时候使用 std::abs 就足够了?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3118165/
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
When do I use fabs and when is it sufficient to use std::abs?
提问by math
I assume that abs
and fabs
are behaving different when using math.h
. But when I use just cmath
and std::abs
, do I have to use std::fabs
or fabs
? Or isn't this defined?
我认为abs
并且fabs
在使用math.h
. 但是当我只使用cmath
and 时std::abs
,我必须使用std::fabs
orfabs
吗?还是这个没有定义?
回答by Mike Seymour
In C++, it's always sufficient to use std::abs
; it's overloaded for all the numerical types.
在 C++ 中,使用std::abs
;总是足够的。它为所有数字类型重载。
In C, abs
only works on integers, and you need fabs
for floating point values. These are available in C++ (along with all of the C library), but there's no need to use them.
在 C 中,abs
仅适用于整数,并且您需要fabs
浮点值。这些在 C++ 中可用(以及所有 C 库),但没有必要使用它们。
回答by Alan Turing
It's still okay to use fabs
for double
and float
arguments. I prefer this because it ensures that if I accidentally strip the std::
off the abs
, that the behavior remains the same for floating point inputs.
仍然可以使用fabs
fordouble
和float
arguments。我喜欢这个,因为它确保了如果我意外地剥离std::
掉abs
,该行为仍然是浮点输入的相同。
I just spent 10 minutes debugging this very problem, due to my own mistake of using abs
instead of std::abs
. I assumed that the using namespace std;
would infer std::abs
but it did not, and instead was using the C version.
我只花了 10 分钟来调试这个问题,因为我自己错误地使用abs
了std::abs
. 我认为using namespace std;
会推断std::abs
但它没有,而是使用 C 版本。
Anyway, I believe it's good to use fabs
instead of abs
for floating-point inputs as a way of documenting your intention clearly.
无论如何,我相信使用浮点输入fabs
代替abs
浮点输入作为一种清楚地记录您的意图的方式是很好的。
回答by Kenichi Hidai
There is one more reason to recommend std::fabs
for floating-point inputs explicitly.
还有一个std::fabs
明确推荐浮点输入的理由。
If you forget to include <cmath>, your std::abs(my_float_num)
can be std::abs(int)
instead of std::abs(float)
. It's hard to notice.
如果您忘记包含 <cmath>,您std::abs(my_float_num)
可以用std::abs(int)
代替std::abs(float)
。很难注意到。
回答by BS3
"abs" and "fabs" are only identical for C++ float types, when they can be translated without ambiguous overload messages.
"abs" 和 "fabs" 仅对于 C++ 浮点类型是相同的,因为它们可以在没有歧义的重载消息的情况下进行转换。
I'm using g++ (g++-7).
Together with template usage and especially when using mpreal there are cases with hard "ambiguous overload" messages - abs(static_cast<T>(x))
isn't always solving that.
When abs is ambiguous, there are chances that fabs is working as expected. For sqrt I found no such simple escape.
我正在使用 g++ (g++-7)。连同模板使用,特别是在使用 mpreal 时,存在硬“模糊重载”消息的情况 -abs(static_cast<T>(x))
并不总是能解决这个问题。当 abs 不明确时,晶圆厂有可能按预期工作。对于 sqrt 我发现没有这么简单的转义。
Since weeks I'm hard struggling on C++ "not existing problems". I'm updating an old C++ program to C++14 for more and better template usage than possible before. Often the same template parameter may be actual any standard float or complex type or a class type. Why ever, long double acted somewhat more sensible than other types. All was working, and I had included mpreal before. Then I was setting my default float type to mpreal and got a deluge of syntax errors. That gave thousands of ambiguous overloads e.g. for abs and sqrt, crying for different solutions. Some were needing overloaded help functions, but outside of a template. Had to replace individually a thousand usages of 0.0L and 1.0L with the exact constant type using Zero or One or a type_cast - automatic conversion definition impossible because of ambiguities.
几周以来,我一直在努力解决 C++“不存在的问题”。我正在将旧的 C++ 程序更新为 C++14,以获得比以前更多更好的模板使用。通常,相同的模板参数可能是任何标准浮点数或复杂类型或类类型。为什么,long double 的行为比其他类型更明智一些。一切正常,我之前已经包含了 mpreal。然后我将我的默认浮点类型设置为 mpreal 并得到了大量的语法错误。这给了数以千计的模棱两可的重载,例如 abs 和 sqrt,为不同的解决方案而哭泣。有些需要重载的帮助功能,但在模板之外。必须使用零或一或 type_cast 将 0.0L 和 1.0L 的一千个用法单独替换为精确的常量类型 - 由于歧义,自动转换定义是不可能的。
Up to May I found the existing of implicit conversions very nice. But much simpler it would be without any, and to have typesave constants with safe explicit type_casts to any other standard constant type.
直到 5 月,我发现隐式转换的存在非常好。但是更简单的是没有任何类型,并且将类型保存常量与安全的显式 type_casts 转换为任何其他标准常量类型。