Skip to content

Commit d2f124f

Browse files
committed
Add 3 new routines to Maths category
The routines are: * PowN * PowNZN * PowNZZ Add compile result for all three for Delphi XE, 11 & 12. Add test info showing advanced / unit tests Also updated Pow function entry to cross reference these new routines. Add test info for PowNxx functions
1 parent b5026e9 commit d2f124f

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

collection/633.dat

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function PowN(const X: Extended; const N: Integer): Extended;
2+
var
3+
I: Integer;
4+
begin
5+
if N = 0 then
6+
// IEEE: pown(x, 0) is 1, even when X is zero
7+
Exit(1.0);
8+
if Math.SameValue(1.0, X) then
9+
Exit(1.0);
10+
if Math.IsZero(X) then
11+
begin
12+
if N < 0 then
13+
raise SysUtils.EDivByZero.Create('PowN: Negative exponent for X = 0');
14+
Exit(0.0);
15+
end;
16+
Result := 1.0;
17+
for I := 1 to System.Abs(N) do
18+
Result := Result * X;
19+
if N < 0 then
20+
Result := 1 / Result;
21+
end;

collection/634.dat

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function PowNZN(const X: Integer; const N: Cardinal): Int64;
2+
var
3+
I: Integer;
4+
begin
5+
if (X = 0) and (N > 0) then
6+
Exit(0);
7+
if X = 1 then
8+
Exit(1);
9+
Result := 1;
10+
for I := 1 to N do
11+
Result := Result * X;
12+
end;

collection/635.dat

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function PowNZZ(const X: Integer; const N: Integer): Extended;
2+
var
3+
I: Integer;
4+
ResultInt: Int64;
5+
begin
6+
if N = 0 then
7+
Exit(1.0);
8+
if X = 1 then
9+
Exit(1.0);
10+
if X = 0 then
11+
begin
12+
if N < 0 then
13+
raise SysUtils.EDivByZero.Create('PowNZZ: Negative exponent for X = 0');
14+
Exit(0.0);
15+
end;
16+
ResultInt := 1;
17+
for I := 1 to System.Abs(N) do
18+
ResultInt := ResultInt * X;
19+
if N > 0 then
20+
Result := ResultInt
21+
else // N < 0
22+
Result := 1 / ResultInt;
23+
end;

collection/maths.ini

+39
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,7 @@ FPC=Y
11781178

11791179
[Pow]
11801180
DescEx="<p>Raises integer value <var>Base</var> to non-negative integer power <var>Exponent</var> and returns the result.</p>"
1181+
SeeAlso=PowN,PowNZN,PowNZZ
11811182
Snip=561.dat
11821183
Delphi2=N
11831184
Delphi3=N
@@ -1197,6 +1198,44 @@ DelphiXE4=Y
11971198
Delphi10S=Y
11981199
FPC=Y
11991200

1201+
[PowN]
1202+
DescEx="<p>IEEE compliant function that raises real number <var>X</var> to the power <var>N</var>.</p>"
1203+
Extra="<p>See IEEE standard 754-2008 for Floating-Point Arithmetic, page 44.</p>"
1204+
Units=SysUtils,Math
1205+
SeeAlso=Pow,PowNZN,PowNZZ
1206+
TestInfo=advanced
1207+
AdvancedTest.Level=unit-tests
1208+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1209+
Snip=633.dat
1210+
DelphiXE=Y
1211+
Delphi11A=Y
1212+
Delphi12A=Y
1213+
1214+
[PowNZN]
1215+
DescEx="<p>Raises integer <var>X</var> to non-negative integer power <var>N</var>.</p>"
1216+
Extra="<p>Returns an <var>integer</var> value because the power <var>N</var> is non-negative which guarantees that the result is integral.</p><p>Based on IEEE standard 754-2008 for Floating-Point Arithmetic, page 44, but which restricts <var>X</var> to an integer and <var>N</var> to a non-negative integer.</p>"
1217+
SeeAlso=Pow,PowN,PowNZZ
1218+
TestInfo=advanced
1219+
AdvancedTest.Level=unit-tests
1220+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1221+
Snip=634.dat
1222+
DelphiXE=Y
1223+
Delphi11A=Y
1224+
Delphi12A=Y
1225+
1226+
[PowNZZ]
1227+
DescEx="<p>Raises integer <var>X</var> to integer power <var>N</var>.</p>"
1228+
Extra="<p>Returns an <var>Extended</var> value since the result is not an integer when power <var>N</var> is negative.</p><p>Based on IEEE standard 754-2008 for Floating-Point Arithmetic, page 44, but which restricts <var>X</var> to an integer.</p>"
1229+
Units=SysUtils,
1230+
SeeAlso=Pow,PowN,PowNZN
1231+
TestInfo=advanced
1232+
AdvancedTest.Level=unit-tests
1233+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1234+
Snip=635.dat
1235+
DelphiXE=Y
1236+
Delphi11A=Y
1237+
Delphi12A=Y
1238+
12001239
[ResizeRect_A]
12011240
DisplayName="ResizeRect (TSize overload)"
12021241
DescEx="<p>Resizes rectangle <var>R</var> to size <var>NewSize</var>, leaving the top-left position unchanged.</p><p>Returns the resized rectangle.</p>"

0 commit comments

Comments
 (0)