You could try answer the question "How do I define double
and float
literals?"
double
s are usually defined as:
double a = 12d;
//or
double b = 12.1;
//or
double c = 12.1d;
//or
double d = 12;
while float
s could be defined as:
float m = 10;
//or
float n = 10.4f;
//but this is invalid:
float p = 10.4;
Note that by default a literal like 10.123
is considered to be double
, so if you really want to assign it to a float
variable, you need to append a f
character in the end. However, f
is optional when the literal is 10
.
Let's also list the properties of both types:
float
: 32 bits (4 bytes) where 23 bits are used for the mantissa (up to 9 decimal digits). 8 bits are used for the exponent, so a float can move the decimal point to the right or to the left using those 8 bits. There is 1 bit used as the sign bit. The max long
value that double can hold is $10_18$
double
: 64 bits (8 bytes) where 52 bits are used for the mantissa (up to 19 decimal digits). 11 bits are used for the exponent and 1 bit is the sign bit.
So, double
and float
could be defined like:
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
double = 19 * digit, [ [, | .] 18 * digit]["d" | "D"];
float = 19 * digit, [ [, | .] 18 * digit, "f" | "F"];