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和std::abs时,我必须使用std::fabs还是fabs?或者这不是定义吗?
4 个解决方案
#1
110
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库),但是不需要使用它们。
#2
22
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。我喜欢这样,因为它确保了如果我不小心将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。我假设使用名称空间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来做浮点输入是一种很好的方式来清楚地记录你的意图。
#3
7
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.
如果您忘记包含
#4
1
"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
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,以获得比以前更多更好的模板使用。通常相同的模板参数可以是实际的任何标准浮动或复杂类型或类类型。为什么长时间的重复行为比其他类型更明智。一切都很顺利,我以前也包括了迈克普尔。然后,我将默认的浮点类型设置为mpreal,结果出现了大量语法错误。这就产生了成千上万的不明确的过载,例如abs和sqrt,迫切需要不同的解决方案。有些需要重载的帮助函数,但在模板之外。必须使用0或1或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.
到五月为止,我发现隐式转换的存在非常好。但更简单的是,它不包含任何类型,并且将带安全显式type_cast的类型常量转换为任何其他标准常量类型。
#1
110
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库),但是不需要使用它们。
#2
22
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。我喜欢这样,因为它确保了如果我不小心将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。我假设使用名称空间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来做浮点输入是一种很好的方式来清楚地记录你的意图。
#3
7
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.
如果您忘记包含
#4
1
"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
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,以获得比以前更多更好的模板使用。通常相同的模板参数可以是实际的任何标准浮动或复杂类型或类类型。为什么长时间的重复行为比其他类型更明智。一切都很顺利,我以前也包括了迈克普尔。然后,我将默认的浮点类型设置为mpreal,结果出现了大量语法错误。这就产生了成千上万的不明确的过载,例如abs和sqrt,迫切需要不同的解决方案。有些需要重载的帮助函数,但在模板之外。必须使用0或1或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.
到五月为止,我发现隐式转换的存在非常好。但更简单的是,它不包含任何类型,并且将带安全显式type_cast的类型常量转换为任何其他标准常量类型。