Delphi: define const double from binary representation
This can be done directly thru unions:
type // In a union all variables share the same address
TAll= packed record
case Integer of
1:( d: Double );
2:( i: Int64 );
3:( f: TFileTime );
end;
const // Define the constant just like you would define an array
ALL: TAll= ( i: 43 );
The rest is obvious: either you now access ALL.d
to see how 43
as Int64
is interpreted as Double
. Or do it the other way around. Likewise you can even check how the TFileTime
interpretation looks by accessing ALL.f
.
When dealing with or checking binary keep in mind that the byte order is also important (LE versus BE) - especially when reading from different storage types (FS versus RAM).
To answer with my own initial solution, the syntax is a bit more extensive than I like, but this seems to do the trick using a variant record and implicit type casting.
Nice feature/side effect is that you can quickly see binary representation and double representation together in the inspector/watchwindow.
type
RDoubleHelperRec=record
class operator Implicit(aRec:RDoubleHelperRec):double;
case Bin:boolean of
True:
(BinData:UINT64);
False:
(DoubleData:double);
end;
...
class operator RDoubleHelperRec.Implicit(aRec: RDoubleHelperRec): double;
begin
Result:=aRec.DoubleData;
end;
then when using it, declare as const:
procedure TestIEEEDouble.TestCompareValueBoundary;
const
cSmallestPositiveDouble:RDoubleHelperRec=(BinData:$0000000000000001);
begin
CheckEquals(0,CompareValue(FSmallestPositiveDouble,cSmallestPositiveDouble),'CompareValue(FSmallestPositiveDouble,cSmallestPositiveDouble)');
end;