mardi 25 août 2015

c++ sqrt guaranteed precision, upper/lower bound

I have to check an inequality containing square roots. To avoid incorrect results due to floating point inaccuracy and rounding, I use std::nextafter() to get an upper/lower bound:

#include <cfloat> // DBL_MAX
#include <cmath> // std::nextafter, std::sqrt

double x = 42.0;
double y = std::nextafter(std::sqrt(x), DBL_MAX);

a) Is y*y >= x guaranteed?

b) Will this work for other operations like + - * / or even std::cos() and std::acos()?

c) Are there better ways to get upper/lower bounds?

Aucun commentaire:

Enregistrer un commentaire