9
votes
uses
  SysUtils, Variants;

var
  VariantSingle: Variant;
  VTSingle: TVarType;
  SingleTest: Single;
  VariantDouble: Variant;
  DoubleTest: Double;
  VTDouble: TVarType;

begin
  SingleTest := 1.234;
  VariantSingle := SingleTest;
  VTSingle := VarType(VariantSingle) and varTypeMask;

  DoubleTest := 1.23456;
  VariantDouble := DoubleTest;
  VTDouble := VarType(VariantDouble) and varTypeMask;

  WriteLn(Format('VarType: Single: %d, Double %d', [VTSingle, VTDouble]));
end.

The code above will output:

VarType: Single: 5, Double 5

From System.pas

varSingle   = $0004; { vt_r4           4 }
varDouble   = $0005; { vt_r8           5 }

Thus, I'd expect VTSingle to be 4 - not 5
What am I missing?

1

1 Answers

7
votes

The Delphi libraries choose to implement all floating point assignments to variants by means of a call to _VarFromReal. And that function looks like this:

procedure _VarFromReal(var V: TVarData; const Value: Real);
begin
  if (V.VType and varDeepData) <> 0 then
    VarClearDeep(V);
  V.VType := varDouble;
  V.VDouble := Value;
end;

Note that this uses a type of varDouble. And includes an implicit conversion to Real which is an alias for Double. I'm not sure why the designers chose that particular route, but the consequence of that choice is the behaviour that you observe.

A simple way to make a varSingle variant you can use:

VariantSingle := VarAsType(SingleTest, varSingle);

Although this will convert SingleTest to Double, and then back again to Single.

To avoid that needless conversion, write your own helper:

function VarFromSingle(const Value: Single): Variant;
begin
  VarClear(Result);
  TVarData(Result).VSingle := Value;
  TVarData(Result).VType := varSingle;
end;

which you can call like this:

VariantSingle := VarFromSingle(SingleTest);

This latter approach is the correct solution in my opinion.