Data Type Conversion Delphi Var Parameter

I have a function with a parameter var Extended

. The compiler complains if I try to use a type argument Double

when calling a function. However, if I pass the value Extended

as a function Result

(and assign it to a variable Double

) then the compiler is happy.

Is this expected? If so, is there a way to trick the compiler to reduce the precision of the parameter to match the argument?

function foo1(var e: extended): boolean;
begin
  e := 0.0;
  Result := true;
end;

function foo2(): extended;
begin
  Result := 0.0;
end;

procedure CallFoo();
var
  d: double;
begin
  if foo1(d) then Exit; // compiler complains
  d := foo2; // compiler happy
end;

      

+3


source to share


1 answer


Parameters

var

require the actual argument to match exactly. You can only pass a variable Extended

.



One option for you is to introduce overloads for each of the floating point types you need. But my advice is to stop using Extended

and switch to Double

. The type Extended

only exists on 32 Intel platforms and very rarely offers any benefit over Double

. In contrast, its unusual size of 10 bytes often results in poor performance due to inconsistencies and inefficiencies in cache usage.

+3


source







All Articles