8

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.

Hans Tananda
  • 169
  • 8
  • 9
    You start from a wrong assumption, _"why these two variable of the same value_": they don't have the same value. – Jack Aug 04 '17 at 04:11
  • https://stackoverflow.com/questions/2386772/difference-between-float-and-double – Hariom Singh Aug 04 '17 at 04:14
  • Protip: don't use float unless you have to. Complete pain in the ass. – zzxyz Aug 04 '17 at 04:17
  • @zzxyz: float or double? Both are justified, except when calculating monetary units, which are integral. – Aki Suihkonen Aug 04 '17 at 04:32
  • @Nipun Em I think after I call the std::fixed, all of the commands I gave below follow the fixed rule? – Hans Tananda Aug 04 '17 at 04:38
  • @zzxyz Ok noted. – Hans Tananda Aug 04 '17 at 04:38
  • float - The loss of precision that occurs can be relatively large, in addition to being non-intuitive, particularly in terms of base-10 numbers and rounding. Certainly they are necessary for certain applications, but definitely to be avoided imo. – zzxyz Aug 04 '17 at 06:08
  • Also see [strange output in comparison of float with float literal](https://stackoverflow.com/questions/1839422/strange-output-in-comparison-of-float-with-float-literal) – Bo Persson Aug 04 '17 at 08:38

1 Answers1

9

Those variables don't have the same value. When you shoehorn the literal double of 98.765 into the float, it has to do a best fit, and some precision is lost.

You can see this quite easily if you change the precision to 50, you'll also see that not even the double can represent that value exactly:

98.76499938964843750000000000000000000000000000000000
98.76500000000000056843418860808014869689941406250000

However, the important thing is that the former float variable will round down, the latter double will round up.

See also the IEEE754 online converter.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953