setprecision output when the last digit beyond decimal is 5

Here is the below code which I was testing from edit&run option in the cplusplus website itself. I am getting different output than I expect:-
// setprecision example
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision

int main () {
double f =107.705;
double g =13.605;
double h =45.905;
std::cout << std::fixed;
std::cout << std::setprecision(2) << f << '\n';
std::cout << std::setprecision(2) << g << '\n';
std::cout << std::setprecision(2) << h << '\n';
return 0;
}
OUTPUT:-
107.70
13.61
45.91

For 107.705 as well I expect it to be 107.71. But what is the reason that it is giving 107.70 as output.

Can someone help me understand.
A double can't store 107.705 exactly so the value that actually gets stored is 107.7049999999999982946974341757595539093017578125 which with two decimal places gets rounded to 107.70.
Topic archived. No new replies allowed.