In Delphi libraries, it is proposed to implement all floating point assignments for options using the _VarFromReal call. And this 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 the varDouble type is used for this. And includes an implicit conversion to Real , which is an alias for Double . I'm not sure why the designers chose this particular route, but the consequence of this choice is the behavior that you observe.
An easy way to make a varSingle variant that you can use:
VariantSingle := VarAsType(SingleTest, varSingle);
Although this converts SingleTest to Double and then back to Single .
To avoid unnecessary 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 as follows:
VariantSingle := VarFromSingle(SingleTest);
This last approach is the right solution, in my opinion.
David heffernan
source share