problem with long double

hi
in c++ i do :
long double x=1.123456789;
cout << x << endl;

but this prints : x=1.12346
why?
i use long double and so i expect this commands print x=1.123456789

thanks
Last edited on
Output function for floating point numbers does rounding, because sometimes instead of 0.1 a floating point number will contain 0.099999 or etc.
Try using http://cplusplus.com/reference/iostream/manipulators/fixed/
Last edited on
well you can setprecision() but it rounds...

you can do
cout << setprecision9 << ...
cout << scientific << ....
Last edited on
thanks
but here
http://www.cplusplus.com/doc/tutorial/variables/
says that range of long double floating point numbers is 15 digits. but in practice i get a range equal 6 . i want to know that why in my pc long double does not have a range equal 15 digits
6 is the default precision of the output stream. You are free to change it:

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <limits>
#include <iomanip>
int main()
{
    long double x = 1.123456789L;
    std::cout << "default precision: "  << x << '\n'
              << "max precision: " << std::setprecision(std::numeric_limits<long double>::digits10) << x << '\n';
}
demo: http://ideone.com/gZo8r
thanks
Topic archived. No new replies allowed.