Support explicit cast for float types in mode Delphi

Summary

Since Delphi 10.2, DCC supports explicit cast for floating point numeric types, e.g. to reduce the precision or to determine the variant type if the target type is Variant.

System Information

  • Operating system: Windows
  • Processor architecture: x86
  • Compiler version: trunk
  • Device: Computer

Steps to reproduce

Run the example project in Delphi 10.2 or newer and in FPC (Delphi mode).

Example Project

program FloatCastTest;
{$mode Delphi}
uses Math;
var
  C: Currency;
  D, D2: Double;
  S: Single;
  Cp: Comp;
  V: Variant;
begin
  C := 5.5556;
  D := Double(C);
  S := Single(D);
  D2 := Single(D);
  Cp := Comp(D);
  V := Double(C);

  Writeln('C: ', C);
  Writeln('D: ', D);
  if not SameValue(C, D) then // C and D must be the same
    Halt(1);

  Writeln('D2:', D2);
  if SameValue(D, D2) then // D and D2 must be different -> precision reduced with Single() conversion
    Halt(2);

  Writeln('S: ', S);
  if not SameValue(D2, S) then // D2 and S must be the same
    Halt(3);

  Writeln('Cp:', Cp);
  if not SameValue(Cp, 6) then // Cp is 6
    Halt(4);

  Writeln('V:  ', V);
  if not SameValue(D, V) then // D and V must be the same
    Halt(5);
end.

What is the current bug behavior?

The program doesn't compile with this error:

FloatCastTest.lpr(13,8) Error: Illegal type conversion: "Double" to "Single"

When the unsupported explicit casts are removed then the program fails at runtime with ErrorCode=1 (C and D are different).

What is the expected (correct) behavior?

The program should compile and the output should be the same as in Delphi 13 (and in FPC ObjFPC mode).

Delphi 13 output:

C:  5.55560000000000E+0000
D:  5.55560000000000E+0000
D2: 5.55560016632080E+0000
S:  5.55560016632080E+0000
Cp: 6.00000000000000E+0000
V:  5,5556
Assignee Loading
Time tracking Loading