RTL: Sqrt & Pow
This commit is contained in:
@@ -153,6 +153,16 @@ type
|
||||
[AstSignature('(number) -> number')]
|
||||
class function Sign(const Arg: TScalar): TScalar; static;
|
||||
|
||||
[TRtlExport('Sqrt', Pure)]
|
||||
[AstDoc('Returns the square root of a number.')]
|
||||
[AstSignature('(number) -> number')]
|
||||
class function Sqrt(const Arg: TScalar): TScalar; static;
|
||||
|
||||
[TRtlExport('Pow', Pure)]
|
||||
[AstDoc('Returns Base raised to the power of Exponent.')]
|
||||
[AstSignature('(number, number) -> number')]
|
||||
class function Pow(const Args: TArray<TDataValue>): TDataValue; static;
|
||||
|
||||
// --- DateTime ---
|
||||
|
||||
[TRtlExport('Now', Impure)]
|
||||
@@ -326,6 +336,20 @@ type
|
||||
class function Trunc_Ordinal_Ordinal(A: Int64): Int64; static;
|
||||
[TRtlExport('Trunc', Pure)]
|
||||
class function Trunc_Float_Ordinal(A: Double): Int64; static;
|
||||
|
||||
[TRtlExport('Sqrt', Pure)]
|
||||
class function Sqrt_Ordinal_Float(A: Int64): Double; static;
|
||||
[TRtlExport('Sqrt', Pure)]
|
||||
class function Sqrt_Float_Float(A: Double): Double; static;
|
||||
|
||||
[TRtlExport('Pow', Pure)]
|
||||
class function Pow_Ordinal_Ordinal_Float(A, B: Int64): Double; static;
|
||||
[TRtlExport('Pow', Pure)]
|
||||
class function Pow_Float_Float_Float(A, B: Double): Double; static;
|
||||
[TRtlExport('Pow', Pure)]
|
||||
class function Pow_Ordinal_Float_Float(A: Int64; B: Double): Double; static;
|
||||
[TRtlExport('Pow', Pure)]
|
||||
class function Pow_Float_Ordinal_Float(A: Double; B: Int64): Double; static;
|
||||
end;
|
||||
|
||||
implementation
|
||||
@@ -536,6 +560,22 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
class function TRtlFunctions.Sqrt(const Arg: TScalar): TScalar;
|
||||
begin
|
||||
var val: Double := Arg; // Implicit cast to Double handles both Ordinal and Float
|
||||
Result := TScalar.FromDouble(System.Sqrt(val));
|
||||
end;
|
||||
|
||||
class function TRtlFunctions.Pow(const Args: TArray<TDataValue>): TDataValue;
|
||||
begin
|
||||
if Length(Args) <> 2 then
|
||||
raise EArgumentException.Create('Pow requires 2 arguments (Base, Exponent).');
|
||||
|
||||
var baseVal: Double := Args[0].AsScalar; // Implicit cast
|
||||
var expVal: Double := Args[1].AsScalar; // Implicit cast
|
||||
Result := TScalar.FromDouble(System.Math.Power(baseVal, expVal));
|
||||
end;
|
||||
|
||||
// --- Date Constructors ---
|
||||
|
||||
class function TRtlFunctions.Now(const Args: TArray<TDataValue>): TDataValue;
|
||||
@@ -1024,4 +1064,34 @@ begin
|
||||
Result := System.Trunc(A);
|
||||
end;
|
||||
|
||||
class function TRtlFunctions.Sqrt_Ordinal_Float(A: Int64): Double;
|
||||
begin
|
||||
Result := System.Sqrt(A);
|
||||
end;
|
||||
|
||||
class function TRtlFunctions.Sqrt_Float_Float(A: Double): Double;
|
||||
begin
|
||||
Result := System.Sqrt(A);
|
||||
end;
|
||||
|
||||
class function TRtlFunctions.Pow_Ordinal_Ordinal_Float(A, B: Int64): Double;
|
||||
begin
|
||||
Result := System.Math.Power(A, B);
|
||||
end;
|
||||
|
||||
class function TRtlFunctions.Pow_Float_Float_Float(A, B: Double): Double;
|
||||
begin
|
||||
Result := System.Math.Power(A, B);
|
||||
end;
|
||||
|
||||
class function TRtlFunctions.Pow_Ordinal_Float_Float(A: Int64; B: Double): Double;
|
||||
begin
|
||||
Result := System.Math.Power(A, B);
|
||||
end;
|
||||
|
||||
class function TRtlFunctions.Pow_Float_Ordinal_Float(A: Double; B: Int64): Double;
|
||||
begin
|
||||
Result := System.Math.Power(A, B);
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
+21
-1
@@ -78,6 +78,7 @@ type
|
||||
TNativeFunc_O_O = function(A: Int64): Int64;
|
||||
TNativeFunc_F_F = function(A: Double): Double;
|
||||
TNativeFunc_F_O = function(A: Double): Int64;
|
||||
TNativeFunc_O_F = function(A: Int64): Double;
|
||||
|
||||
TNativeFunc_OO_O = function(A, B: Int64): Int64;
|
||||
TNativeFunc_OO_F = function(A, B: Int64): Double;
|
||||
@@ -101,6 +102,7 @@ type
|
||||
class function CreateWrapper_O_O(CodeAddress: Pointer): TDataValue.TFunc; static;
|
||||
class function CreateWrapper_F_F(CodeAddress: Pointer): TDataValue.TFunc; static;
|
||||
class function CreateWrapper_F_O(CodeAddress: Pointer): TDataValue.TFunc; static;
|
||||
class function CreateWrapper_O_F(CodeAddress: Pointer): TDataValue.TFunc; static;
|
||||
class function CreateWrapper_OO_O(CodeAddress: Pointer): TDataValue.TFunc; static;
|
||||
class function CreateWrapper_OO_F(CodeAddress: Pointer): TDataValue.TFunc; static;
|
||||
class function CreateWrapper_FF_F(CodeAddress: Pointer): TDataValue.TFunc; static;
|
||||
@@ -546,6 +548,20 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
class function TRtlRegistry.CreateWrapper_O_F(CodeAddress: Pointer): TDataValue.TFunc;
|
||||
begin
|
||||
Result :=
|
||||
function(const Args: TArray<TDataValue>): TDataValue
|
||||
var
|
||||
A: Int64;
|
||||
Res: Double;
|
||||
begin
|
||||
A := Args[0].AsScalar.Value.AsInt64;
|
||||
Res := TNativeFunc_O_F(CodeAddress)(A);
|
||||
Result := TDataValue(TScalar.FromDouble(Res));
|
||||
end;
|
||||
end;
|
||||
|
||||
class function TRtlRegistry.CreateWrapper_OO_O(CodeAddress: Pointer): TDataValue.TFunc;
|
||||
begin
|
||||
Result :=
|
||||
@@ -703,6 +719,8 @@ begin
|
||||
Result := CreateWrapper_F_F(ptr)
|
||||
else if (argTypes[0].Kind = stFloat) and (retType.Kind = stOrdinal) then
|
||||
Result := CreateWrapper_F_O(ptr)
|
||||
else if (argTypes[0].Kind = stOrdinal) and (retType.Kind = stFloat) then
|
||||
Result := CreateWrapper_O_F(ptr)
|
||||
else if (argTypes[0].Kind = stUnknown) and (rttiParams[0].ParamType.Handle = TypeInfo(TScalar)) then
|
||||
begin
|
||||
// This is the wrapper for: class function(Arg: TScalar): TScalar;
|
||||
@@ -767,7 +785,9 @@ begin
|
||||
begin
|
||||
Result := CreateWrapper_TT_T(ptr); // e.g. String concat
|
||||
end
|
||||
end;
|
||||
end
|
||||
else
|
||||
Assert(false, 'fgdfdf');
|
||||
end;
|
||||
|
||||
procedure RegisterRtlFunctions(const AScope: IExecutionScope);
|
||||
|
||||
Reference in New Issue
Block a user