- Data Types and Other Tokens
- Working with Variables
- The boolean Primitive
- The Flavors of Integer
- Operators
- Character Variables
- Floating-Point Variables
- Literals: Assigning Values
- Integer Literals
- Character Literals
- Floating-Point Literals
- String Literals
- Arrays
- Non-Token Input Elements
- Troubleshooting
Floating-Point Literals
Floating-point numbers can be represented in a number of ways. The following are all legitimate floating-point numbers:
1003.45 |
.00100345e6 |
100.345E+1100345e2 |
1.00345e3 |
0.00100345e+6 |
|
Floating-point literals have several parts, which appear in the following order as shown in Table 3.9.
Table 3.9 Floating-Point Literal Requirements
Part |
Is It Required? |
Examples |
Whole Number |
Not if fractional part is present. |
0, 1, 100, 1003 |
Decimal Point |
Not if exponent is present; must be there if there is a fractional part. |
|
Fractional |
Cannot be present if there is no decimal point; must be there if there is no whole number part. |
0, 1, 1415927 |
Exponent |
Only if there is no decimal point. |
e23, E19, E6, e+307, e1 |
Type Suffix |
No. The number is assumed to be double precision in the absence of a type suffix. |
f, F, d, D |
The following representations illustrate the ways you can specify a floating-point literal that is consistent with the requirements in Table 3.9:
1.234 |
(Whole Number) . (Fractional) |
1E2 |
(Whole Number)(Exponent) |
1.234F |
(Whole Number) . (Fractional)(Type Suffix) |
1E2D |
(Whole Number)(Exponent)(Type Suffix) |
The following restrictions apply to floating-point literals:
-
Single precision floating-point literals produce compile-time errors if their values are non-zero and have an absolute value outside the range 1.40239846e45f through 3.40282347e+38f.
-
Double precision floating-point literals produce compile-time errors if their values are non-zero and have an absolute value outside the range 4.94065645841246544e324 through 1.7976931348623157e+308.