TDecimal fix
This commit is contained in:
+153
-70
@@ -2,8 +2,6 @@ unit Myc.Data.Decimal;
|
||||
|
||||
interface
|
||||
|
||||
{$H+}
|
||||
|
||||
type
|
||||
TScale = 0..7;
|
||||
|
||||
@@ -12,18 +10,18 @@ type
|
||||
FValue: Int64;
|
||||
public
|
||||
constructor Create(AValue: Int64; AScale: TScale); overload;
|
||||
constructor Create(AValue: Double; AScale: TScale); overload;
|
||||
constructor Create(const AValue: TDecimal; ANewScale: TScale); overload;
|
||||
|
||||
class operator Add(const A, B: TDecimal): TDecimal;
|
||||
class operator Subtract(const A, B: TDecimal): TDecimal;
|
||||
class operator Add(const A, B: TDecimal): TDecimal; inline;
|
||||
class operator Subtract(const A, B: TDecimal): TDecimal; inline;
|
||||
class operator Multiply(const A, B: TDecimal): TDecimal;
|
||||
class operator Divide(const A, B: TDecimal): TDecimal;
|
||||
|
||||
class operator Equal(const A, B: TDecimal): Boolean;
|
||||
class operator NotEqual(const A, B: TDecimal): Boolean;
|
||||
class operator Equal(const A, B: TDecimal): Boolean; inline;
|
||||
class operator NotEqual(const A, B: TDecimal): Boolean; inline;
|
||||
|
||||
class operator Implicit(const A: Int64): TDecimal;
|
||||
class operator Explicit(const A: TDecimal): Int64;
|
||||
class operator Implicit(const A: Int64): TDecimal; inline;
|
||||
class operator Explicit(const A: TDecimal): Double;
|
||||
|
||||
class function MinValue(AScale: TScale): TDecimal; static;
|
||||
@@ -45,9 +43,11 @@ const
|
||||
VALUE_BITS = 61;
|
||||
SCALE_MASK = $07;
|
||||
|
||||
// Range of the 61-bit signed value part
|
||||
MIN_VALUE_61BIT = -(1 shl 60);
|
||||
MAX_VALUE_61BIT = (1 shl 60) - 1;
|
||||
// CRITICAL FIX: Cast '1' to Int64 to force 64-bit constant evaluation.
|
||||
MIN_VALUE_61BIT = -(Int64(1) shl 60);
|
||||
MAX_VALUE_61BIT = (Int64(1) shl 60) - 1;
|
||||
SIGN_BIT_61 = Int64(1) shl (VALUE_BITS - 1); // Bit 60 for sign check
|
||||
RAW_VALUE_MASK = (Int64(1) shl VALUE_BITS) - 1; // Mask for bits 0..60
|
||||
|
||||
// Use a lookup table for powers of 10 for performance
|
||||
PowersOf10: array[TScale] of Int64 = (1, 10, 100, 1000, 10000, 100000, 1000000, 10000000);
|
||||
@@ -56,31 +56,30 @@ const
|
||||
|
||||
constructor TDecimal.Create(AValue: Int64; AScale: TScale);
|
||||
begin
|
||||
FValue := (Int64(AScale) shl VALUE_BITS) or (AValue and ((1 shl VALUE_BITS) - 1));
|
||||
// Validate that the incoming value fits within the 61-bit storage.
|
||||
if (AValue < MIN_VALUE_61BIT) or (AValue > MAX_VALUE_61BIT) then
|
||||
raise EOverflow.Create('Value is out of range for a 61-bit TDecimal.');
|
||||
|
||||
FValue := (Int64(AScale) shl VALUE_BITS) or (AValue and RAW_VALUE_MASK);
|
||||
end;
|
||||
|
||||
constructor TDecimal.Create(const AValue: TDecimal; ANewScale: TScale);
|
||||
var
|
||||
oldScale: TScale;
|
||||
newValue: Int64;
|
||||
scaleDiff: Integer;
|
||||
i: Integer;
|
||||
begin
|
||||
// Create a new decimal by changing the scale of an existing one.
|
||||
oldScale := AValue.GetScale;
|
||||
newValue := AValue.GetValue;
|
||||
|
||||
var oldScale := AValue.GetScale;
|
||||
if oldScale = ANewScale then
|
||||
begin
|
||||
FValue := AValue.FValue;
|
||||
exit;
|
||||
end;
|
||||
|
||||
scaleDiff := ANewScale - oldScale;
|
||||
var newValue := AValue.GetValue;
|
||||
var scaleDiff := ANewScale - oldScale;
|
||||
|
||||
if scaleDiff > 0 then
|
||||
begin
|
||||
// Increasing scale: multiply by 10^scaleDiff, checking for overflow at each step.
|
||||
for i := 1 to scaleDiff do
|
||||
for var i := 1 to scaleDiff do
|
||||
begin
|
||||
if newValue > (MAX_VALUE_61BIT div 10) then
|
||||
raise EOverflow.Create('Decimal scale up resulted in an overflow.');
|
||||
@@ -96,71 +95,158 @@ begin
|
||||
end;
|
||||
|
||||
// Pack the new value and scale
|
||||
FValue := (Int64(ANewScale) shl VALUE_BITS) or (newValue and ((1 shl VALUE_BITS) - 1));
|
||||
FValue := (Int64(ANewScale) shl VALUE_BITS) or (newValue and RAW_VALUE_MASK);
|
||||
end;
|
||||
|
||||
constructor TDecimal.Create(AValue: Double; AScale: TScale);
|
||||
begin
|
||||
// Scale the double by the specified scale and round it to the nearest integer.
|
||||
// Round() will raise an exception if the value exceeds the Int64 range.
|
||||
var intValue := Round(AValue * PowersOf10[AScale]);
|
||||
|
||||
// Check if the resulting value fits within the 61-bit storage.
|
||||
if (intValue < MIN_VALUE_61BIT) or (intValue > MAX_VALUE_61BIT) then
|
||||
raise EOverflow.Create('Double to TDecimal conversion resulted in an overflow.');
|
||||
|
||||
// Pack the value and scale into FValue.
|
||||
FValue := (Int64(AScale) shl VALUE_BITS) or (intValue and RAW_VALUE_MASK);
|
||||
end;
|
||||
|
||||
class operator TDecimal.Add(const A, B: TDecimal): TDecimal;
|
||||
var
|
||||
scale: TScale;
|
||||
resultValue: Int64;
|
||||
begin
|
||||
scale := A.GetScale;
|
||||
if scale <> B.GetScale then
|
||||
raise EArgumentException.Create('Operands must have the same scale.');
|
||||
var aScale := A.GetScale;
|
||||
var bScale := B.GetScale;
|
||||
|
||||
resultValue := A.GetValue + B.GetValue;
|
||||
Result := TDecimal.Create(resultValue, A.GetScale);
|
||||
// Fast path for performance when scales are identical
|
||||
if aScale = bScale then
|
||||
begin
|
||||
Result := TDecimal.Create(A.GetValue + B.GetValue, aScale);
|
||||
exit;
|
||||
end;
|
||||
|
||||
// Slower path for different scales: align scales before operation
|
||||
if aScale > bScale then
|
||||
begin
|
||||
var scaledB := TDecimal.Create(B, aScale);
|
||||
Result := TDecimal.Create(A.GetValue + scaledB.GetValue, aScale);
|
||||
end
|
||||
else
|
||||
begin
|
||||
var scaledA := TDecimal.Create(A, bScale);
|
||||
Result := TDecimal.Create(scaledA.GetValue + B.GetValue, bScale);
|
||||
end;
|
||||
end;
|
||||
|
||||
class operator TDecimal.Subtract(const A, B: TDecimal): TDecimal;
|
||||
var
|
||||
scale: TScale;
|
||||
resultValue: Int64;
|
||||
begin
|
||||
scale := A.GetScale;
|
||||
if scale <> B.GetScale then
|
||||
raise EArgumentException.Create('Operands must have the same scale.');
|
||||
var aScale := A.GetScale;
|
||||
var bScale := B.GetScale;
|
||||
|
||||
resultValue := A.GetValue - B.GetValue;
|
||||
Result := TDecimal.Create(resultValue, scale);
|
||||
// Fast path for performance when scales are identical
|
||||
if aScale = bScale then
|
||||
begin
|
||||
Result := TDecimal.Create(A.GetValue - B.GetValue, aScale);
|
||||
exit;
|
||||
end;
|
||||
|
||||
// Slower path for different scales: align scales before operation
|
||||
if aScale > bScale then
|
||||
begin
|
||||
var scaledB := TDecimal.Create(B, aScale);
|
||||
Result := TDecimal.Create(A.GetValue - scaledB.GetValue, aScale);
|
||||
end
|
||||
else
|
||||
begin
|
||||
var scaledA := TDecimal.Create(A, bScale);
|
||||
Result := TDecimal.Create(scaledA.GetValue - B.GetValue, bScale);
|
||||
end;
|
||||
end;
|
||||
|
||||
class operator TDecimal.Multiply(const A, B: TDecimal): TDecimal;
|
||||
var
|
||||
scale: TScale;
|
||||
resultValue: Int64;
|
||||
begin
|
||||
scale := A.GetScale;
|
||||
if scale <> B.GetScale then
|
||||
raise EArgumentException.Create('Operands must have the same scale.');
|
||||
var scaleA := A.GetScale;
|
||||
var scaleB := B.GetScale;
|
||||
var valA := A.GetValue;
|
||||
var valB := B.GetValue;
|
||||
|
||||
resultValue := Trunc(A.GetValue * B.GetValue / PowersOf10[scale]);
|
||||
Result := TDecimal.Create(resultValue, scale);
|
||||
if scaleA = scaleB then
|
||||
begin
|
||||
// Fast path for performance when scales are identical
|
||||
var resultValue := MulDivInt64(valA, valB, PowersOf10[scaleA]);
|
||||
Result := TDecimal.Create(resultValue, scaleA);
|
||||
end
|
||||
else
|
||||
begin
|
||||
// Slower path for different scales
|
||||
var targetScale := Max(scaleA, scaleB);
|
||||
// Effective power of 10 to divide by is Min(scaleA, scaleB)
|
||||
var scaleIndex := Min(scaleA, scaleB);
|
||||
var resultValue := MulDivInt64(valA, valB, PowersOf10[scaleIndex]);
|
||||
Result := TDecimal.Create(resultValue, targetScale);
|
||||
end;
|
||||
end;
|
||||
|
||||
class operator TDecimal.Divide(const A, B: TDecimal): TDecimal;
|
||||
var
|
||||
scale: TScale;
|
||||
resultValue: Int64;
|
||||
begin
|
||||
scale := A.GetScale;
|
||||
if scale <> B.GetScale then
|
||||
raise EArgumentException.Create('Operands must have the same scale.');
|
||||
|
||||
if B.GetValue = 0 then
|
||||
var bVal := B.GetValue;
|
||||
if bVal = 0 then
|
||||
raise EDivByZero.Create('Division by zero');
|
||||
|
||||
// Correctly scale the dividend to preserve precision
|
||||
resultValue := Trunc(A.GetValue * PowersOf10[scale] / B.GetValue);
|
||||
Result := TDecimal.Create(resultValue, scale);
|
||||
var aVal := A.GetValue;
|
||||
var aScale := A.GetScale;
|
||||
var bScale := B.GetScale;
|
||||
|
||||
// Fast path for performance when scales are identical
|
||||
if aScale = bScale then
|
||||
begin
|
||||
var multiplier := PowersOf10[aScale];
|
||||
var resultValue := MulDivInt64(aVal, multiplier, bVal);
|
||||
Result := TDecimal.Create(resultValue, aScale);
|
||||
exit;
|
||||
end;
|
||||
|
||||
// Slower path for different scales: align scales before operation
|
||||
if aScale > bScale then
|
||||
begin
|
||||
var targetScale := aScale;
|
||||
var tempB := TDecimal.Create(B, targetScale);
|
||||
var multiplier := PowersOf10[targetScale];
|
||||
var resultValue := MulDivInt64(aVal, multiplier, tempB.GetValue);
|
||||
Result := TDecimal.Create(resultValue, targetScale);
|
||||
end
|
||||
else // bScale > aScale
|
||||
begin
|
||||
var targetScale := bScale;
|
||||
var tempA := TDecimal.Create(A, targetScale);
|
||||
var multiplier := PowersOf10[targetScale];
|
||||
var resultValue := MulDivInt64(tempA.GetValue, multiplier, bVal);
|
||||
Result := TDecimal.Create(resultValue, targetScale);
|
||||
end;
|
||||
end;
|
||||
|
||||
class operator TDecimal.Equal(const A, B: TDecimal): Boolean;
|
||||
begin
|
||||
if A.GetScale <> B.GetScale then
|
||||
Result := False
|
||||
else
|
||||
var aScale := A.GetScale;
|
||||
var bScale := B.GetScale;
|
||||
|
||||
// Fast path for identical scales
|
||||
if aScale = bScale then
|
||||
begin
|
||||
Result := (A.GetValue = B.GetValue);
|
||||
exit;
|
||||
end;
|
||||
|
||||
// Slower path: align scales before comparing values
|
||||
if aScale > bScale then
|
||||
begin
|
||||
var scaledB := TDecimal.Create(B, aScale);
|
||||
Result := (A.GetValue = scaledB.GetValue);
|
||||
end
|
||||
else // bScale > aScale
|
||||
begin
|
||||
var scaledA := TDecimal.Create(A, bScale);
|
||||
Result := (scaledA.GetValue = B.GetValue);
|
||||
end;
|
||||
end;
|
||||
|
||||
class operator TDecimal.NotEqual(const A, B: TDecimal): Boolean;
|
||||
@@ -174,14 +260,11 @@ begin
|
||||
Result := TDecimal.Create(A, 0);
|
||||
end;
|
||||
|
||||
class operator TDecimal.Explicit(const A: TDecimal): Int64;
|
||||
begin
|
||||
Result := A.GetValue;
|
||||
end;
|
||||
|
||||
class operator TDecimal.Explicit(const A: TDecimal): Double;
|
||||
begin
|
||||
Result := A.GetValue / PowersOf10[A.GetScale];
|
||||
var scale := A.GetScale;
|
||||
var value := A.GetValue;
|
||||
Result := value / PowersOf10[scale];
|
||||
end;
|
||||
|
||||
class function TDecimal.MaxValue(AScale: TScale): TDecimal;
|
||||
@@ -198,10 +281,10 @@ function TDecimal.GetValue: Int64;
|
||||
var
|
||||
value: Int64;
|
||||
begin
|
||||
value := FValue and ((1 shl VALUE_BITS) - 1);
|
||||
value := FValue and RAW_VALUE_MASK;
|
||||
// Perform sign extension from 61 to 64 bits
|
||||
if (value and (1 shl (VALUE_BITS - 1))) <> 0 then
|
||||
Result := value or (not ((1 shl VALUE_BITS) - 1))
|
||||
if (value and SIGN_BIT_61) <> 0 then
|
||||
Result := value or (not RAW_VALUE_MASK)
|
||||
else
|
||||
Result := value;
|
||||
end;
|
||||
|
||||
@@ -41,13 +41,13 @@ type
|
||||
class function FromDateTime(AValue: TDateTime): TScalarValue; static; inline;
|
||||
class function FromTimestamp(AValue: TTimestamp): TScalarValue; static; inline;
|
||||
class function FromBoolean(AValue: Boolean): TScalarValue; static; inline;
|
||||
class function FromDecimal(AValue: TDecimal): TScalarValue; static; inline;
|
||||
class function FromChar(AValue: Char): TScalarValue; static; inline;
|
||||
class function FromPChar(const AValue: String): TScalarValue; overload; static; inline;
|
||||
class function FromPChar(const AValue: TScalarPChar): TScalarValue; overload; static; inline;
|
||||
class function FromString(const AValue: String): TScalarValue; overload; static; inline;
|
||||
class function FromString(const AValue: TScalarString): TScalarValue; overload; static; inline;
|
||||
class function FromBytes(const AValue: TScalarBytes): TScalarValue; static; inline;
|
||||
class function FromDecimal(AValue: TDecimal): TScalarValue; static; inline;
|
||||
|
||||
// Direct field access for performance.
|
||||
case TScalarKind of
|
||||
|
||||
@@ -5,8 +5,7 @@ interface
|
||||
uses
|
||||
System.SysUtils,
|
||||
Myc.Data.Scalar,
|
||||
Myc.Data.Series,
|
||||
Myc.Data.Decimal;
|
||||
Myc.Data.Series;
|
||||
|
||||
type
|
||||
TDataValueKind = (vkVoid, vkScalar, vkInterface, vkText, vkSeries, vkRecordSeries, vkRecord, vkMemberSeries, vkGeneric);
|
||||
@@ -94,7 +93,7 @@ end;
|
||||
function TDataValue.AsVal<T>: TVal<T>;
|
||||
begin
|
||||
if (FKind <> vkGeneric) then
|
||||
raise EInvalidCast.Create('Cannot read value as ' + PTypeInfo(TypeInfo(T)).Name + '.');
|
||||
raise EInvalidCast.Create('Cannot read value as ' + String(PTypeInfo(TypeInfo(T)).Name) + '.');
|
||||
Result := TVal<T>(FInterface);
|
||||
end;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user