对,我查Stack Overflow上说,这个std::abs和abs是两套东西。c的abs,只支持int
cppreference的std::abs页面上Defect reports说,
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR LWG 2192
Applied to C++98
Behavior as published overloads of std::abs were inconsistently declared in two headers -- 这是说定义不致 <cmath> <cstdlib>
Correct behavior declared these overloads in both headers
另一个页面,std::fabs,说
DR LWG 2735
Applied to C++11
Behavior as published overloads of std::abs for integer types returning double was erroneously required -- 这是说 double fabs(int),进int出double,易错。
Correct behavior removed the requirement
https://en.cppreference.com/w/cpp/numeric/math/abs
https://en.cppreference.com/w/cpp/numeric/math/fabs
琢磨来琢磨去,我觉得 abs , std::abs留给整型, fabs()留给float/double最佳实践。
至于abs支持float/double,应该是第一个Defect Report引入的bug。
【 在 origin008 的大作中提到: 】
: 有的编译器,abs就自动int了
: 很久以前遇到过这个问题
: 不过那个时候还没用std::这么高级的货
: ...................
--
FROM 61.185.187.*