小编Lux*_*Lux的帖子

c ++ sqrt保证精度,上限/下限

我必须检查包含平方根的不等式.为了避免由于浮点不准确和舍入导致的错误结果,我使用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编译器吗?

c++ floating-point floating-accuracy sqrt c++11

16
推荐指数
1
解决办法
603
查看次数

标签 统计

c++ ×1

c++11 ×1

floating-accuracy ×1

floating-point ×1

sqrt ×1