Why does assigning a single to a variant result in a varDouble variant?

穿精又带淫゛_ 提交于 2019-12-08 16:31:49

问题


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:


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.



来源:https://stackoverflow.com/questions/15133496/why-does-assigning-a-single-to-a-variant-result-in-a-vardouble-variant

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!