Hello everyone.
I found an inconsistency in results when dividing a float number by an integer in exponential notation and literal notation.
Ex, the following code used to round and crop a float number to six decimal places, using the division first by 1E6 (exponential notation) and last for 1000000 (literal notation as I mean):
program NumberTest;
const
NUM_CONST = 2.123456789;
var
NumVar: Double;
Res1, Res2, Res3, Res4: Double;
begin
NumVar := NUM_CONST;
Res1 := Round(NUM_CONST * 1E6) / 1E6;
Res2 := Round(NumVar * 1E6) / 1E6;
Res3 := Round(NUM_CONST * 1E6) / 1000000;
Res4 := Round(NumVar * 1E6) / 1000000;
WriteLn('Res1 = ', Res1);
WriteLn('Res2 = ', Res2);
WriteLn('Res3 = ', Res3);
WriteLn('Res4 = ', Res4);
end.
I expected the same result in both, but it is not the case.
The result output on Windows 10 (64 bit) is:
Res1 = 2.1234570000000002E+000
Res2 = 2.1234569549560547E+000
Res3 = 2.1234570000000002E+000
Res4 = 2.1234570000000002E+000
Note that the operation using the const number results as expected (2.123457), but when using a variable it results in (2.1234569549560547).
The same code on Linux (Ubuntu 20.04) results in:
Res1 = 2.1234569549560547E+000
Res2 = 2.1234569549560547E+000
Res3 = 2.1234570000000001E+000
Res4 = 2.1234570000000001E+000
Note that on Linux, the operations using const and var result the same (2.1234569549560547).
Why does an operation using the expenential notation differ from using the literal one?
Edit: I am using Free Pascal 3.2.0, but the same occurs in 3.0.4