Lux*_*Lux 16 c++ floating-point floating-accuracy sqrt c++11
我必须检查包含平方根的不等式.为了避免由于浮点不准确和舍入导致的错误结果,我使用std::nextafter()
上限/下限:
#include <cfloat> // DBL_MAX
#include <cmath> // std::nextafter, std::sqrt
double x = 42.0; //just an example number
double y = std::nextafter(std::sqrt(x), DBL_MAX);
Run Code Online (Sandbox Code Playgroud)
a)y*y >= x
使用GCC编译器保证吗?
b)这是否适用于其他操作+ - * /
,甚至std::cos()
和std::acos()
?
c)是否有更好的方法来获得上限/下限?
更新:我读过这不是C++标准保证的,但应该按照IEEE-754工作.这适用于GCC编译器吗?