RTL and Data value refactoring and defered values

This commit is contained in:
Michael Schimmel
2025-09-18 13:35:25 +02:00
parent 4d380c8f98
commit 2c55a120f1
11 changed files with 783 additions and 365 deletions
+28 -9
View File
@@ -25,8 +25,8 @@ type
class operator LessThan(const A, B: TDecimal): Boolean; inline;
class operator LessThanOrEqual(const A, B: TDecimal): Boolean; inline;
class operator Negative(const A: TDecimal): TDecimal; inline;
class operator Round(const A: TDecimal): TDecimal;
class operator Trunc(const A: TDecimal): TDecimal;
class operator Round(const A: TDecimal): Int64;
class operator Trunc(const A: TDecimal): Int64;
class operator Implicit(const A: Int64): TDecimal; inline;
class operator Explicit(const A: TDecimal): Double;
@@ -34,6 +34,9 @@ type
class function MinValue(AScale: TScale): TDecimal; static;
class function MaxValue(AScale: TScale): TDecimal; static;
function Sign: Integer; inline;
function Abs: TDecimal; inline;
function GetValue: Int64;
function GetScale: TScale;
function GetRawValue: Int64; inline;
@@ -119,6 +122,14 @@ begin
FValue := (Int64(AScale) shl VALUE_BITS) or (intValue and RAW_VALUE_MASK);
end;
function TDecimal.Abs: TDecimal;
begin
if GetValue < 0 then
Result := -Self
else
Result := Self;
end;
class operator TDecimal.Add(const A, B: TDecimal): TDecimal;
begin
var aScale := A.GetScale;
@@ -302,24 +313,22 @@ begin
Result := TDecimal.Create(-A.GetValue, A.GetScale);
end;
class operator TDecimal.Round(const A: TDecimal): TDecimal;
class operator TDecimal.Round(const A: TDecimal): Int64;
var
value: Int64;
divisor: Int64;
begin
// Rounds to the nearest whole number (scale 0) using arithmetic rounding.
value := A.GetValue;
divisor := PowersOf10[A.GetScale];
if value >= 0 then
Result := TDecimal.Create((value + divisor div 2) div divisor, 0)
Result := (value + divisor div 2) div divisor
else
Result := TDecimal.Create((value - divisor div 2) div divisor, 0);
Result := (value - divisor div 2) div divisor;
end;
class operator TDecimal.Trunc(const A: TDecimal): TDecimal;
class operator TDecimal.Trunc(const A: TDecimal): Int64;
begin
// Truncates the fractional part, resulting in a decimal with scale 0
Result := TDecimal.Create(A.GetValue div PowersOf10[A.GetScale], 0);
Result := A.GetValue div PowersOf10[A.GetScale];
end;
class operator TDecimal.Implicit(const A: Int64): TDecimal;
@@ -365,4 +374,14 @@ begin
Result := FValue;
end;
function TDecimal.Sign: Integer;
begin
var val := GetValue;
if val < 0 then
exit(-1);
if val > 0 then
exit(-1);
exit(0);
end;
end.