Data POD
This commit is contained in:
@@ -7,27 +7,27 @@ interface
|
||||
type
|
||||
TScale = 0..7;
|
||||
|
||||
TDecimal64 = record
|
||||
TDecimal = record
|
||||
strict private
|
||||
FValue: Int64;
|
||||
public
|
||||
constructor Create(AValue: Int64; AScale: TScale); overload;
|
||||
constructor Create(const AValue: TDecimal64; ANewScale: TScale); overload;
|
||||
constructor Create(const AValue: TDecimal; ANewScale: TScale); overload;
|
||||
|
||||
class operator Add(const A, B: TDecimal64): TDecimal64;
|
||||
class operator Subtract(const A, B: TDecimal64): TDecimal64;
|
||||
class operator Multiply(const A, B: TDecimal64): TDecimal64;
|
||||
class operator Divide(const A, B: TDecimal64): TDecimal64;
|
||||
class operator Add(const A, B: TDecimal): TDecimal;
|
||||
class operator Subtract(const A, B: TDecimal): TDecimal;
|
||||
class operator Multiply(const A, B: TDecimal): TDecimal;
|
||||
class operator Divide(const A, B: TDecimal): TDecimal;
|
||||
|
||||
class operator Equal(const A, B: TDecimal64): Boolean;
|
||||
class operator NotEqual(const A, B: TDecimal64): Boolean;
|
||||
class operator Equal(const A, B: TDecimal): Boolean;
|
||||
class operator NotEqual(const A, B: TDecimal): Boolean;
|
||||
|
||||
class operator Implicit(const A: Int64): TDecimal64;
|
||||
class operator Explicit(const A: TDecimal64): Int64;
|
||||
class operator Explicit(const A: TDecimal64): Double;
|
||||
class operator Implicit(const A: Int64): TDecimal;
|
||||
class operator Explicit(const A: TDecimal): Int64;
|
||||
class operator Explicit(const A: TDecimal): Double;
|
||||
|
||||
class function MinValue(AScale: TScale): TDecimal64; static;
|
||||
class function MaxValue(AScale: TScale): TDecimal64; static;
|
||||
class function MinValue(AScale: TScale): TDecimal; static;
|
||||
class function MaxValue(AScale: TScale): TDecimal; static;
|
||||
|
||||
function GetValue: Int64;
|
||||
function GetScale: TScale;
|
||||
@@ -52,14 +52,14 @@ const
|
||||
// Use a lookup table for powers of 10 for performance
|
||||
PowersOf10: array[TScale] of Int64 = (1, 10, 100, 1000, 10000, 100000, 1000000, 10000000);
|
||||
|
||||
{ TDecimal64 }
|
||||
{ TDecimal }
|
||||
|
||||
constructor TDecimal64.Create(AValue: Int64; AScale: TScale);
|
||||
constructor TDecimal.Create(AValue: Int64; AScale: TScale);
|
||||
begin
|
||||
FValue := (Int64(AScale) shl VALUE_BITS) or (AValue and ((1 shl VALUE_BITS) - 1));
|
||||
end;
|
||||
|
||||
constructor TDecimal64.Create(const AValue: TDecimal64; ANewScale: TScale);
|
||||
constructor TDecimal.Create(const AValue: TDecimal; ANewScale: TScale);
|
||||
var
|
||||
oldScale: TScale;
|
||||
newValue: Int64;
|
||||
@@ -99,7 +99,7 @@ begin
|
||||
FValue := (Int64(ANewScale) shl VALUE_BITS) or (newValue and ((1 shl VALUE_BITS) - 1));
|
||||
end;
|
||||
|
||||
class operator TDecimal64.Add(const A, B: TDecimal64): TDecimal64;
|
||||
class operator TDecimal.Add(const A, B: TDecimal): TDecimal;
|
||||
var
|
||||
scale: TScale;
|
||||
resultValue: Int64;
|
||||
@@ -109,10 +109,10 @@ begin
|
||||
raise EArgumentException.Create('Operands must have the same scale.');
|
||||
|
||||
resultValue := A.GetValue + B.GetValue;
|
||||
Result := TDecimal64.Create(resultValue, A.GetScale);
|
||||
Result := TDecimal.Create(resultValue, A.GetScale);
|
||||
end;
|
||||
|
||||
class operator TDecimal64.Subtract(const A, B: TDecimal64): TDecimal64;
|
||||
class operator TDecimal.Subtract(const A, B: TDecimal): TDecimal;
|
||||
var
|
||||
scale: TScale;
|
||||
resultValue: Int64;
|
||||
@@ -122,10 +122,10 @@ begin
|
||||
raise EArgumentException.Create('Operands must have the same scale.');
|
||||
|
||||
resultValue := A.GetValue - B.GetValue;
|
||||
Result := TDecimal64.Create(resultValue, scale);
|
||||
Result := TDecimal.Create(resultValue, scale);
|
||||
end;
|
||||
|
||||
class operator TDecimal64.Multiply(const A, B: TDecimal64): TDecimal64;
|
||||
class operator TDecimal.Multiply(const A, B: TDecimal): TDecimal;
|
||||
var
|
||||
scale: TScale;
|
||||
resultValue: Int64;
|
||||
@@ -135,10 +135,10 @@ begin
|
||||
raise EArgumentException.Create('Operands must have the same scale.');
|
||||
|
||||
resultValue := Trunc(A.GetValue * B.GetValue / PowersOf10[scale]);
|
||||
Result := TDecimal64.Create(resultValue, scale);
|
||||
Result := TDecimal.Create(resultValue, scale);
|
||||
end;
|
||||
|
||||
class operator TDecimal64.Divide(const A, B: TDecimal64): TDecimal64;
|
||||
class operator TDecimal.Divide(const A, B: TDecimal): TDecimal;
|
||||
var
|
||||
scale: TScale;
|
||||
resultValue: Int64;
|
||||
@@ -152,10 +152,10 @@ begin
|
||||
|
||||
// Correctly scale the dividend to preserve precision
|
||||
resultValue := Trunc(A.GetValue * PowersOf10[scale] / B.GetValue);
|
||||
Result := TDecimal64.Create(resultValue, scale);
|
||||
Result := TDecimal.Create(resultValue, scale);
|
||||
end;
|
||||
|
||||
class operator TDecimal64.Equal(const A, B: TDecimal64): Boolean;
|
||||
class operator TDecimal.Equal(const A, B: TDecimal): Boolean;
|
||||
begin
|
||||
if A.GetScale <> B.GetScale then
|
||||
Result := False
|
||||
@@ -163,38 +163,38 @@ begin
|
||||
Result := (A.GetValue = B.GetValue);
|
||||
end;
|
||||
|
||||
class operator TDecimal64.NotEqual(const A, B: TDecimal64): Boolean;
|
||||
class operator TDecimal.NotEqual(const A, B: TDecimal): Boolean;
|
||||
begin
|
||||
Result := not (A = B);
|
||||
end;
|
||||
|
||||
class operator TDecimal64.Implicit(const A: Int64): TDecimal64;
|
||||
class operator TDecimal.Implicit(const A: Int64): TDecimal;
|
||||
begin
|
||||
// Correctly create a TDecimal64 with scale 0
|
||||
Result := TDecimal64.Create(A, 0);
|
||||
// Correctly create a TDecimal with scale 0
|
||||
Result := TDecimal.Create(A, 0);
|
||||
end;
|
||||
|
||||
class operator TDecimal64.Explicit(const A: TDecimal64): Int64;
|
||||
class operator TDecimal.Explicit(const A: TDecimal): Int64;
|
||||
begin
|
||||
Result := A.GetValue;
|
||||
end;
|
||||
|
||||
class operator TDecimal64.Explicit(const A: TDecimal64): Double;
|
||||
class operator TDecimal.Explicit(const A: TDecimal): Double;
|
||||
begin
|
||||
Result := A.GetValue / PowersOf10[A.GetScale];
|
||||
end;
|
||||
|
||||
class function TDecimal64.MaxValue(AScale: TScale): TDecimal64;
|
||||
class function TDecimal.MaxValue(AScale: TScale): TDecimal;
|
||||
begin
|
||||
Result := TDecimal64.Create(MAX_VALUE_61BIT, AScale);
|
||||
Result := TDecimal.Create(MAX_VALUE_61BIT, AScale);
|
||||
end;
|
||||
|
||||
class function TDecimal64.MinValue(AScale: TScale): TDecimal64;
|
||||
class function TDecimal.MinValue(AScale: TScale): TDecimal;
|
||||
begin
|
||||
Result := TDecimal64.Create(MIN_VALUE_61BIT, AScale);
|
||||
Result := TDecimal.Create(MIN_VALUE_61BIT, AScale);
|
||||
end;
|
||||
|
||||
function TDecimal64.GetValue: Int64;
|
||||
function TDecimal.GetValue: Int64;
|
||||
var
|
||||
value: Int64;
|
||||
begin
|
||||
@@ -206,12 +206,12 @@ begin
|
||||
Result := value;
|
||||
end;
|
||||
|
||||
function TDecimal64.GetScale: TScale;
|
||||
function TDecimal.GetScale: TScale;
|
||||
begin
|
||||
Result := TScale((FValue shr VALUE_BITS) and SCALE_MASK);
|
||||
end;
|
||||
|
||||
function TDecimal64.GetRawValue: Int64;
|
||||
function TDecimal.GetRawValue: Int64;
|
||||
begin
|
||||
Result := FValue;
|
||||
end;
|
||||
|
||||
Reference in New Issue
Block a user