Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
结果std::abs(0.5f)是0因为浮点数没有过载。为什么?我正在使用 G++。
std::abs(0.5f)
0
使用 的重载时必须非常小心,std::abs因为一些标准库实现会在许多文件中乱扔重载,其中一些文件被隐式包含到其他文件中,例如<iostream>.
std::abs
<iostream>
如果您#include <cmath>或#include <cstdlib>(来自 C++17 的第二个)在您之前,std::abs(0.5f)则float重载将可用。如果不是这种情况,那么您的编译器/标准库实现中存在错误(在 g++ 的情况下不太可能)。
#include <cmath>
#include <cstdlib>
float
参考:https ://en.cppreference.com/w/cpp/numeric/math/fabs