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 = 7.40200133400;
std::cout << x << "\n";
}
For the above code snippet, the output was 7.402
Is there a way to prevent this type of truncation? Or is there a way to calculate exactly how many floating points for a double
? For example, number_of_decimal(x)
would give 11, since the input is unknown at run-time so I can't use setprecision()
.
I think I should change my question to: How to convert a double to a string without truncating the floating points. i.e.
#include <iostream>
#include <string>
#include <sstream>
template<typename T>
std::string type_to_string( T data ) {
std::ostringstream o;
o << data;
return o.str();
}
int main() {
double x = 7.40200;
std::cout << type_to_string( x ) << "\n";
}
The expected output should be 7.40200 but the actual result was 7.402. So how can I work around this problem? Any idea?
The only answer to this that I've come up with is that there is no way to do this (as in calculate the decimal places) correctly! THE primary reason for this being that the representation of a number may not be what you expect, for example, 128.82, seems innocuous enough, however it's actual representation is 128.8199999999... how do you calculate the number of decimal places there??
The second part of the question, about how to preserve trailing zeroes in a floating point value from value specification to output result, has no solution. A floating point value doesn't retain the original value specification. It seems this nonsensical part was added by an SO moderator.
Regarding the first and original part of the question, which I interpret as how to present all significant digits of
7.40200133400
, i.e. with output like7.402001334
, you can just remove trailing zeroes from an output result that includes only trustworthy digits in thedouble
value:Output:
You might consider adding logic for rounding to avoid long sequences of 9's, like in the last result.
You must use
setiosflags(ios::fixed)
andsetprecision(x)
.For example,
cout << setiosflags(ios::fixed) << setprecision(4) << myNumber << endl;
Also, don't forget to
#include <iomanip.h>
.Responding to your answer-edit: There is no way to do that. As soon as you assign a value to a
double
, any trailing zeroes are lost (to the compiler/computer, 0.402, 0.4020, and 0.40200 are the SAME NUMBER). The only way to retain trailing zeroes as you indicated is to store the values as strings (or do trickery where you keep track of the number of digits you care about and format it to exactly that length).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.