Witscale Test Center

7.3 Literals > 7.3.4 Floating-point literals


7.3.4 Floating-point literals

Floating point values are decimal values with a decimal point (optional) and an optional exponent. A floating-point literal represents a floating-point value.  In order to be interpreted as a floating point literal, a numeric value must have either:

         A decimal point to indicate it is a floating-point value. for instance 124.67

         A letter E or e (denoting the exponent), for instance 12467e-2

         The suffix f or F to indicate that the type of this literal is float. For instance, 124.67f

         The suffix d or D to indicate that the type of this literal is double. For instance, 124.67d

 

The floating-point literal can be of two types, float or double. By default, it is of type double. If you want to assign a floating-point literal to a variable of type float, you must attach the suffix F or f to the number.

 

float f = 12.34;       //invalid

float f = 12.34f;      //valid

 

The literal 12.34 is of double type, it cannot be directly assigned to a variable of type float because float (32 bits) is narrower datatype than double (64 bits).

You can redundantly apply the suffix d to the floating-point literals. Following are some valid declarations using  floating-point literals.

 

 double d = 12.34;       //valid

 double d = 12.34D;      //valid

 double d1 = 1.23E+20;  //valid