precision floating point numbers:
floating point describes a method of representing real numbers in a way that can support a wide range of values. Numbers are, in general, represented approximately to a fixed number of significant digits and scaled using an exponent.
Significant digits × baseexponent
Consider the fraction 1/3. The decimal representation of this number
is 0.33333333333333… with 3′s going out to infinity. An infinite
length number would require infinite memory, and we typically only have 4
or 8 bytes. Floating point numbers can only store a certain number of
digits, and the rest are lost. The precision of a floating point number is how many digits it can represent without information loss.
When outputting floating point numbers, cout has a default precision
of 6 — that is, it assumes all variables are only significant to 6
digits, and hence it will truncate anything after that.
The following program shows cout truncating to 6 digits:
Example
No comments:
Post a Comment