Surely rounding is (one of) the mostly asked and resolved questions. With any pre-defined precision after decimal point e.g. 10 ^ N, one could easily do
double round(double input) {
static constexpr double DIVISOR = std::pow(10, N);
return std::round(value * DIVISOR) / DIVISOR;
}
However, this would potentially give values like e.g. 15.1499999999999 when the value I want is 15.15; As the nearest double number to the desired value it makes sense. But my application needs to make sure the result is always strictly larger than 15.15.
To achieve this, the easiest way is to change the std::round()
to std::ceil()
as above; but a N must be chosen. Given the IEEE standard for double, is there any elegant way to round up and make sure the number is always e.g. 15.1500000000000001 and achieve the highest accuracy?
I read up gtest ASSERT_DOUBLE_EQ which goes:
Verifies that the two double values val1 and val2 are approximately equal, to within 4 ULPs from each other.
But am not sure if within 4 ULPs I would surely have the above result.
Thank you in advance.