C++ float vs double cout setprecision oddities(newbie)
Asked Answered
M

1

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.

Moselle answered 4/8, 2017 at 4:8 Comment(8)
You start from a wrong assumption, "why these two variable of the same value": they don't have the same value.Morrissette
#2387272Wedding
Protip: don't use float unless you have to. Complete pain in the ass.Sholapur
@zzxyz: float or double? Both are justified, except when calculating monetary units, which are integral.Hypersensitize
@Nipun Em I think after I call the std::fixed, all of the commands I gave below follow the fixed rule?Moselle
@Sholapur Ok noted.Moselle
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.Sholapur
Also see strange output in comparison of float with float literalFragmental
E
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.

Escrow answered 4/8, 2017 at 4:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.