In this particular instance I am rounding 1.49999 using std::round with float, double, and long double. Here is the code:
#include <iostream>
#include <iomanip>
#include <cmath>
int main() {
float f_val = 1.499999f;
double d_val = 1.499999999999;
long double ld_val = 1.4999999999999999L;
std::cout << std::fixed << std::setprecision(15);
std::cout << "Float value: " << f_val << " rounded: " << std::round(f_val) << std::endl;
std::cout << "Double value: " << d_val << " rounded: " << std::round(d_val) << std::endl;
std::cout << "Long double value: " << ld_val << " rounded: " << std::round(ld_val) << std::endl;
return 0;
}
And here is the output:
Float value: 1.499999046325684 rounded: 1.000000000000000
Double value: 1.499999999999000 rounded: 1.000000000000000
Long double value: 1.500000000000000 rounded: 2.000000000000000
Why is std::round rounding to 1 for a float and double? Why is a long double rounding to 2? Why are they different and not the same?