I known the common way is to multiply 10^n and then divide 10^n. round a float with one digit number But due to the double precision accuracy problem, I found the solution above doesn't fully work for my case:
0.965 * 0.9 = 0.8685
std::round(0.8685 * 1000) / 1000 = 0.869
// expects it to be 0.869
std::round(0.965 * 0.9 * 1000) / 1000 = 0.868
If I want to get 0.869
from std::round(0.965 * 0.9 * 1000) / 1000
directly, I have to change the statement to
std::round((0.965 * 0.9 + std::numeric_limits<double>::epsilon()) * 1000) / 1000 = 0.869
Is there a simpler way to do the rounding without add an epsilon
for every calculation?
Edit: The problem is that, intuitively the value of std::round(0.965 * 0.9 * 1000) / 1000
shall be 0.869
(because 0.965 * 0.9 = 0.8685
), but it actually give 0.868
. I want to find common way get the accurate math value with 3 decimal precision.