I need help on keeping the precision of a double
. If I assign a literal to a double, the actual value was truncated.
int main() {
double x =
Doubles don't have decimal places. They have binary places. And binary places and decimal places are incommensurable (because log2(10)
isn't an integer).
What you are asking for doesn't exist.
Let s make an analogous request: after initialising an integer with 001, you would want to print it with the leading zeroes. That formatting info was simply never stored.
For further understanding the double precision floating point storage, look at the IEEE 754 standard.
You must use setiosflags(ios::fixed)
and setprecision(x)
.
For example, cout << setiosflags(ios::fixed) << setprecision(4) << myNumber << endl;
Also, don't forget to #include <iomanip.h>
.