Reputation:
I want to print out the full legitimate value of some large summation of floats, but when floats are large enough you get this notation such as "1.01383e+007". How can you get the legitimate value?
Upvotes: 4
Views: 4516
Reputation: 612794
I read your question to mean that you do not want to output scientific format. If so then you can control the stream formatting like this:
cout << setiosflags(ios::fixed) << thefloat << endl;
Upvotes: 7
Reputation: 64827
you'll need to use a decimal library, e.g. gmp; the "1.01383e+007" format are due to the way float are stored.
Upvotes: 0
Reputation: 57764
An important characteristic of floating point is that they do not have precision associated with all the significant figures back to the decimal point for large values. The "scientific" display reasonably reflects the inherent internal storage realities.
If you want 10138300 (for example) to appear, use a datatype which has more significant figures, such as double
in the C family languages for values up to 1015 or so. Or use an extended precision integer representation, such as long
or long long
depending on the CPU architecture and programming language or environment.
Upvotes: 3
Reputation: 36323
The float
data type does not store the precise value, and hence it is not possible to print out the exact value.
In a float
, 32 bits are divided between three distinct parts: The sign bit, the exponent and the mantissa like: S EEEEEEEE MMMMMMMMMMMMMMMMMMMMMMM
In simpler terms, instead of storing the precise "12345678", float
data type will only store "1.23*10^7" (and double
data type will store "1.2345*10^7"). Other digits are lost.
Maybe you can look into the "Big Integer" library in C++ if you are interested in the precise value of a large number.
Upvotes: 0