C++ float vs double cout setprecision oddities(newbie)

Viewed 414

Can anyone explain why these two variable of the same value can output different values when i use setprecision()?

#include <iostream>
#include <iomanip>
int main()
{
    float a=98.765;
    double b = 98.765;
    //std::cout<<std::setprecision(2)<<a<<std::endl;
    std::cout<<std::fixed;
    std::cout<<std::setprecision(2)<<a<<std::endl;
    std::cout<<std::setprecision(2)<<b<<std::endl;
}

The output for a will be 98.76 while the output for b will be 98.77.

1 Answers
Related