Float values have between 6 and 9 digits of precision, with most float values having at least 7 significant digits. The number of digits of precision a floating point variable has depends on both the size (floats have less precision than doubles) and the particular value being stored (some values have more precision than others). The minimum number of exponent digits displayed is compiler-specific (Visual Studio uses 3, some others use 2 as per the C99 standard). Depending on the compiler, the exponent will typically be padded to a minimum number of digits. Note that each of these only have 6 significant digits.Īlso note that std::cout will switch to outputting numbers in scientific notation in some cases. The following program shows std::cout truncating to 6 digits: #include When outputting floating point numbers, std::cout has a default precision of 6 - that is, it assumes all floating point variables are only significant to 6 digits (the minimum precision of a float), and hence it will truncate anything after that. The precision of a floating point number defines how many significant digits it can represent without information loss. The number that is actually stored will be close to the desired number, but not exact. This limited memory means floating point numbers can only store a certain number of significant digits - and that any additional significant digits are lost. On a computer, an infinite length number would require infinite memory to store, and typically we only have 4 or 8 bytes. (with 3’s going out to infinity) but not exactly. And the number you were left with would be close to 0.3333333333…. If you were writing this number on a piece of paper, your arm would get tired at some point, and you’d eventually stop writing. The decimal representation of this number is 0.33333333333333… with 3’s going out to infinity. This is because they have the same number of bits dedicated to the exponent - however, the 16-byte number can store more significant digits.Ĭonsider the fraction 1/3. It may seem a little odd that the 80-bit floating point type has the same range as the 16-byte floating point type. On modern processors, it is typically implemented using 12 or 16 bytes (which is a more natural size for processors to handle). The 80-bit floating point type is a bit of a historical anomaly.
0 Comments
Leave a Reply. |