Skip to content

Commit 1af91b3

Browse files
committed
Update tests re revised PowNZN routine.
Regenerated UMathsCatSnippets using CodeSnip to include the revised version of PowNZN. Added a test for the EOverflow exception that can now be raised by PowNZN.
1 parent 17fced2 commit 1af91b3

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

tests/Cat-Maths/TestUMathsCatSnippets.pas

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ TestMathsCatSnippets = class(TTestCase)
2020
procedure TestWeightedArithMean_Double_Except4;
2121
procedure TestDigitSumBase_Except;
2222
procedure TestDigitsOf_ArgExcept;
23+
procedure TestPowNZN_EOverflow;
2324
function EqualArrays(const Left, Right: TBytes): Boolean;
2425
function ReverseArray(const A: TBytes): TBytes;
2526
published
@@ -930,6 +931,12 @@ procedure TestMathsCatSnippets.TestPowNZN;
930931
CheckEquals(10000, PowNZN(10, 4), 'PowNZN(10,2)');
931932
CheckEquals(-1000, PowNZN(-10, 3), 'PowNZN(-10,3)');
932933
CheckEquals(10000, PowNZN(-10, 4), 'PowNZN(-10,4)');
934+
CheckException(TestPowNZN_EOverflow, EOverflow, 'EOverflow');
935+
end;
936+
937+
procedure TestMathsCatSnippets.TestPowNZN_EOverflow;
938+
begin
939+
PowNZN(2, 63);
933940
end;
934941

935942
procedure TestMathsCatSnippets.TestPowNZZ;

tests/Cat-Maths/UMathsCatSnippets.pas

+14-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* The unit is copyright © 2005-2024 by Peter Johnson & Contributors and is
77
* licensed under the MIT License (https://opensource.org/licenses/MIT).
88
*
9-
* Generated on : Tue, 07 Jan 2025 13:13:01 GMT.
9+
* Generated on : Wed, 08 Jan 2025 17:47:45 GMT.
1010
* Generated by : DelphiDabbler CodeSnip Release 4.24.0.
1111
*
1212
* The latest version of CodeSnip is available from the CodeSnip GitHub project
@@ -1614,14 +1614,24 @@ function PowN(const X: Extended; const N: Integer): Extended;
16141614
function PowNZN(const X: Integer; const N: Cardinal): Int64;
16151615
var
16161616
I: Integer;
1617+
OverflowGuard: Int64;
16171618
begin
1618-
if (X = 0) and (N > 0) then
1619-
Exit(0);
1620-
if X = 1 then
1619+
if N = 0 then
1620+
// IEEE: pown(x, 0) is 1, even when X is zero
16211621
Exit(1);
1622+
if X = 0 then
1623+
// pown(0, n) = 0, for all positive n
1624+
Exit(0);
1625+
OverflowGuard := High(Int64) div Abs(X);
16221626
Result := 1;
16231627
for I := 1 to N do
1628+
begin
1629+
if OverflowGuard < Abs(Result) then
1630+
raise SysUtils.EOverflow.CreateFmt(
1631+
'Overflow calculating %d to the power %d', [X, N]
1632+
);
16241633
Result := Result * X;
1634+
end;
16251635
end;
16261636

16271637
{

0 commit comments

Comments
 (0)