When doubles are printed, why do only 6 digits show on the output?
just curious since I thought doubles could hold large values, but show only 6 by default eg. double number = 1.231231; cout << number; [output:] 1.23123 //missing the last digit
6 digits of accuracy is standard for stream formatting in C++. Standard library includes many manipulators to control formatting, including setprecision that changes number of decimals printed: http://www.cplusplus.com/reference/library/manipulators/
But doesn't set precision only set values after the decimal point? What if I just wanted the default formatting to be 10 digits and not 10 decimal spaces?
You may also use: cout.setf(ios::fixed); //To ensure that numbers are printed only in fixed notation, not as 10^ something... cout.precision(10); //Sets precision after decimal point... cout<<number; //outputs 1.2312310000 //Changes default precision...
Thanks for the info! Is there a way to change the default formatting of digits of accuracy if I wanted to set it to 10 digits instead of 6?
Yes, as I mentioned use setprecision: cout << setprecision(10) << number;
that is interesting, Yes a double should be able to hold that value. It just might be something to do with the sololearn compiler. Try it on code::blocks and see if it does the same.
Question is write a c program to print prime numbers up to n and print the largest number in array.
Error in Program