Real numbers (“floats”, “doubles” or “real numbers”) can be specified using any of the following syntaxes:
a = 1.234; a = 1.2e3; a = 7E-10;
The size of a real depends and thus its memory needs depend on the declaration keyword:
- real: The variable gets stored in 80 bit FPU format (internal coprocessor format), with 15 bit exponent, 64 bit mantissa and 1 bit sign of mantissa. This leads to a range from -1.1*10^4932 upto +1.1*10^4932, having a precision of round about 19 decimal digits (64 bit divided by ld(10)).
Variables declared with the “real” qualifier need 16 bytes in memory (12 bytes would be sufficient, but due to alignment it must be rounded to 16 bytes).
- double: The variable gets stored in 64 bit IEEE double precision format, with 10 bit exponent, 52 bit mantissa and 1 bit sign of mantissa. This leads to a range from -10^307 upto 10^308, having a precision of round about 15 decimal digits (52 bit divided by ld(10)).
Variables declared with the “double” qualifier only need 8 bytes in memory.
- float: The variable gets stored in 32 bit IEEE single precision format, with 8 bit exponent, 23 bit mantissa and sign. This leads to a range from -10^18 upto +10^19, having a precision of round about 7 decimal digits (23 bit divided by ld(10)).
Variables declared with the “float” qualifier need 4 bytes in memory, the same amount of memory as integer values.
Floating point precision
It is (quite) commonly known that simple decimal fractions like 0.1 or 0.7 cannot be converted into their internal binary counterparts without a little loss of precision: The reason is that these values are using the number base 10 (which we humans use for thousands of years), but the computer is using number base 2:
This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8 as the result of the internal representation really being something like 7.9999999999….
This is related to the fact that it is impossible to exactly express some fractions in decimal notation with a finite number of digits. For instance, 1/3 in decimal form becomes 0.3333333.
So never trust floating point number results downto the last digit and never compare floating point numbers for equality.
Converting to/from reals
Floating point numbers (real/double/float) are converted automatically to other datatypes if necessary and allowed.
- bool: 0 gets converted to false, any other value gets converted to true.
- int: real values are rounded towards zero.
- complex: real value gets converted to a complex number with real part=value, imaginary part=0
- quaternion: real value gets converted to a quaternion number with real part=value, all other parts are set to 0
- bool: false gets converted to 0, true gets converted to 1.
- int: real value gets this value directly.
- complex: the resulting real value is the real component of the complex number.
- quaternion: the resulting real value is the real component of the quaternion number.