Float can hold 6 significant digits and double 10 (this is how the book tells me).

Consider this code i wrote to test :

int main()
{
    double a=355979.567; //9 significant digits i think
    cout<<a;
    return 0;

}

The o/p is 3555980. i thought since 'a' is double it should show me what i assigned(355979.567).

in fact the o/p is same for float and long double as well?

Any1 can explain this?

how do float double and long double really differ other than their sizes ?

Recommended Answers

All 5 Replies

Here is a thread that talks about the floating point types here. And if you want the decimals to show you can use the <iomanip> header cout << setprecision(5) << fixed; You can check out this to get more manipulators and see what the ones I posted do.

I agree with sfuo, this is a matter of output formatting, not of the actual precision of the stored floating-point value. By default, the output format is somewhat automatic in the sense that if you have a number that is reasonably close to 0, it will be outputted in normal format (not scientific) and with some default number of digits (probably 5 or 6, from what you posted). Just set the precision to a really high number (like 25 or something) and you should see a difference between the float, double, and long double.

cout << setprecision(5) << fixed;

error: 'setprecision' was not declared in this scope

What header should i inlcude to use that ?

the header is <iomanip>, see top right corner of this page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.