Q: I am migrating from VB. Where is the exponent function?
A: There isn't one, but it is simple to make one using Ln(). Ln() gives us the natural logarithm of a number. Using that, we can get the exponentiation easily. E.g. If we want X^Y (or X to the Y power) we would write the code like this:
ExpXY := Exp(Ln(X) * Y);
To use a generic function, declare the formal parameters and function result as Extended, and the conversions from the actual parameters and back to your result variable will be done automatically. (Ln is the natural logarithm of a number).
{ Reproduced from post by Dr. Bob }
function SwartPower(X: LongInt; N: Word): LongInt;
var R: LongInt;
begin
if N = 0 then SwartPower := 1
else
begin
if N = 1 then SwartPower := X
else
begin
if Odd(N) then
begin
R := X;
Dec(N)
end
else R := 1;
while N > 1 do { inner loop
O(log N) }
begin
if Odd(N) then
R := R * X;
X := X * X;
N := N div 2
end;
if R > 1 then
SwartPower :=
R * X
else { save last multiplication
with 1 }
SwartPower :=
X
end
end
end {SwartPower};