|
|
|
|
|
|
|
Below is a table that gives sample ranges of values for the three floating point types, float, double, and long double. In this table we show, for each type, the maximum positive value and the minimum positive value (a tiny fraction that is very close to zero). Negative numbers have the same range but the opposite sign. Ranges of values are expressed in exponential (scientific) notation, where 3.4E+38 means 3.4 × 1038. |
|
|
|
|
| Type | | Minimum Positive Value* | Maximum Positive Value* | | float | | 3.4E-38 | 3.4E+38 | | double | | 1.7E-308 | 1.7E+308 | | long double | | 3.4E-4932 | 1.1E+4932 | | * These values are for one particular machine. Your machine's values may be different. |
|
|
|
|
|
|
The standard header file float.h defines the constants FLT_MAX and FLT_MIN, DBL_MAX and DBL_MIN, and LDBL_MAX and LDBL_MIN. To determine the ranges of values for your machine, you could write a short program that prints out these constants. |
|
|
|
|
|
|
|
|
When you use a floating point constant like 5.8 in a C++ program, its type is assumed to be double (double precision). If you store the value into a float variable, the computer coerces its type from double to float (single precision). If you insist on a constant being of type float rather than double, you can append an F or an f at the end of the constant. Similarly, a suffix of L or l signifies a long double constant. Here are some examples of floating point constants in C++: |
|
|
|
|
| Constant | Type | Remarks | | 6.83 | double | By default, floating point constants are of type double | | 6.83F | float | Explicit float constants end in F or f. | | 6.83L | long double | Explicit long double constants end in L or l. | | 4.35E-9 | double | Exponential notation, meaning 4.35 × 10-9 |
|
|
|
|
|
|
Here's the syntax template for a floating point constant in C++: |
|
|
|
|
|