Sign in Join
Wiki copytemporary

IEEE 754 formats: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11`single`, `double` and `extended` are [[FPC]]'s [[Data type|data types]] implementing [[Pascal]]’s [[Real|real]]. All of them are implemented according IEEE standard 754, where `single` is “single-precision”, `double` is “double-precision”, and `extended` has 80 bits.
22
33## `single`
44
55| | |
66|----|----|
77| value range | 1.5E-45 .. 3.4E38 |
88| accuracy | 6-9 significant decimal digits precision |
99| memory requirement | 4 bytes or [[32 bit|32 bits]] |
1010| property | The single- data-type data field can hold floating-point values ​​and signed and unsigned integer values. Assigning other values ​​will result in error messages from the compiler when the program is compiled, and the compile will be aborted. That is, the executable program is not created. |
1111
1212Definition of a data field of data type Single:
1313
1414``` pascal
1515var
1616 s: single;
1717```
1818
1919Examples of assigning valid values:
2020
2121``` pascal
2222s := -123.45678;
2323// Note: '0' is an integer literal. 0.0 is an real literal.
2424// Here, FPC will make an implicit typecast from integer to single:
2525s := 0;
2626// a positive sign is optional
2727s := 123.45678;
2828```
2929
3030Examples of assigning invalid values:
3131
3232```text
3333s := '-123.45678';
3434s := '0';
3535s := '123.45678';
3636```
3737
3838The difference between the two examples is that the upper example is the assignment of Integer and Floating literals, while the assignment of the lower example is literals of the String type.
3939
4040### Binary floating-point format
4141
4242Any value stored as a single requires 32 bits, formatted as shown in the table below:
4343
4444| Bits | Usage |
4545|----------|-----------------------------------|
4646| 31 | Sign (0 = positive, 1 = negative) |
4747| 30 to 23 | Exponent, biased by 127 |
4848| 22 to 0 | Fraction f of the number 1.f |
4949
5050## `double`
5151
5252Any value stored as a double requires 64 bits, formatted as shown in the table below:
5353
5454| Bits | Usage |
5555|----------|-----------------------------------|
5656| 63 | Sign (0 = positive, 1 = negative) |
5757| 62 to 52 | Exponent, biased by 1023 |
5858| 51 to 0 | Fraction f of the number 1.f |
5959
6060Example of converting from raw data to *double* (Data is `array [0..7] of byte`):
6161
6262``` pascal
6363function ToDouble(const Data; IntelEndianness: Boolean = False):Double;inline;
6464var
6565 ADouble: Double absolute Data;
6666 AQWord: QWord absolute Result;
6767begin
6868 Result := ADouble;
6969 if not IntelEndianness then
7070 AQWord := SwapEndian(AQWord);
7171end;
7272```
7373
7474## `extended`
7575
76-`extended` is a 80-bit wide floating-point data type. There are <abbr title="floating-point unit">FPU</abbr>s that internally use 80 bits for increased precision. FPC allows to use this gain in precision.
76+`extended` is a 80-bit wide floating-point data type. There are FPUs that internally use 80 bits for increased precision. FPC allows to use this gain in precision.
7777
7878> **Note:** If some platform does not support the `extended` data type, it will be mapped to largest available floating-point number data available, i. e. usually `double`.
7979
8080## Using the maximum precision for constants
8181
8282In this example:
8383
8484``` pascal
8585var
8686 f: double;
8787 n: integer = 1758;
8888 m: integer = 0;
8989begin
9090 f := n * 1.2E6 + (2*m+1) * 50E3; // 2109650048
9191```
9292
9393FPC will interpret the constant `1.2E6` as `Single` type, because it fits into the range of the `Single` type. This leads to some precision loss in the calculation: the result is 2109650048 instead of 2109650000.
9494
9595You can use the FPC command-line flag `-CF64` to force floating point constants to have at least 64 bits precision. Or you can use `{$MINFPCONSTPREC <n>}` to force the compiler to evaluate all floating point constants always with the precision given by \<n\> = `32`, `64` or `DEFAULT` (`80` is not supported for implementation reasons). In FPC v3.3.1+, you can also use the directive `{$excessprecision on}` which exists also in Delphi.
9696
9797---
9898*From the Free Pascal wiki, page "IEEE 754 formats", as it stood on 2023-04-05 after a change by Wp, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history