Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ac6f54d

Browse files
Add 3 new routines to Maths category
Added new DigitSumBase, DigitCountBase & DigitsOf routines to the Mathematics category. Updated maths.ini with meta data of new routines & updated SeeAlso fields of some other routines that now cross reference new routines.
1 parent a19dc9e commit ac6f54d

File tree

4 files changed

+102
-3
lines changed

4 files changed

+102
-3
lines changed

‎collection/657.dat‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function DigitSumBase(N: Int64; const Base: Byte): Integer;
2+
var
3+
SignOfN: Math.TValueSign;
4+
begin
5+
if Base < 2 then
6+
raise SysUtils.EArgumentException.Create(
7+
'Base must be in the range 2..255'
8+
);
9+
if N = 0 then
10+
Exit(0);
11+
SignOfN := Math.Sign(N);
12+
N := Abs(N);
13+
Result := 0;
14+
repeat
15+
Inc(Result, N mod Base);
16+
N := N div Base;
17+
until N = 0;
18+
if SignOfN = Math.NegativeValue then
19+
Result := -1 * Result;
20+
end;

‎collection/658.dat‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function DigitCountBase(N: Int64; const Base: Byte): Cardinal;
2+
begin
3+
if Base < 2 then
4+
raise SysUtils.EArgumentException.Create(
5+
'Base must be in the range 2..255'
6+
);
7+
if N = 0 then
8+
Exit(1);
9+
N := Abs(N);
10+
Result := 0;
11+
repeat
12+
Inc(Result);
13+
N := N div Base;
14+
until N = 0;
15+
end;

‎collection/659.dat‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function DigitsOf(N: Int64; const Base: Byte): SysUtils.TBytes;
2+
var
3+
Idx: Integer;
4+
begin
5+
if Base < 2 then
6+
raise SysUtils.EArgumentException.Create(
7+
'Base must be in the range 2..255'
8+
);
9+
N := Abs(N);
10+
SetLength(Result, DigitCountBase(N, Base));
11+
if N > 0 then
12+
begin
13+
Idx := 0;
14+
repeat
15+
Result[Idx] := N mod Base;
16+
Inc(Idx);
17+
N := N div Base;
18+
until N = 0;
19+
end
20+
else
21+
Result[0] := 0;
22+
end;

‎collection/maths.ini‎

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Delphi12A=Y
141141
[DigitCount]
142142
DescEx="<p>Counts the number of digits in the given integer.</p>"
143143
Extra="<p>Contributed by Bill Miller.</p>"
144-
SeeAlso=DigitCount2,DigitCountR
144+
SeeAlso=DigitCount2,DigitCountR,DigitCountBase
145145
TestInfo=advanced
146146
AdvancedTest.Level=unit-tests
147147
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
@@ -168,7 +168,7 @@ FPC=Y
168168
DescEx="<p>Counts the number of digits in the given integer.</p>"
169169
Extra="<p>Based on code suggested by Don Rowlett.</p>"
170170
Units=Math
171-
SeeAlso=DigitCount,DigitCountR
171+
SeeAlso=DigitCount,DigitCountR,DigitCountBase
172172
TestInfo=advanced
173173
AdvancedTest.Level=unit-tests
174174
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
@@ -194,7 +194,7 @@ FPC=Y
194194
[DigitCountR]
195195
DescEx="<p>Counts the number of digits in the given integer using recursion.</p>"
196196
Extra="<p>Contributed by Rubem Nascimento da Rocha.</p>"
197-
SeeAlso=DigitCount,DigitCount2
197+
SeeAlso=DigitCount,DigitCount2,DigitCountBase
198198
TestInfo=advanced
199199
AdvancedTest.Level=unit-tests
200200
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
@@ -620,6 +620,7 @@ Extra="<p>Sums of digits of negative numbers are negative, for example <mono>Dig
620620
TestInfo=advanced
621621
AdvancedTest.Level=unit-tests
622622
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
623+
SeeAlso=DigitSumBase
623624
Snip=418.dat
624625
Delphi2=N
625626
Delphi3=N
@@ -1755,3 +1756,44 @@ AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tes
17551756
Snip=655.dat
17561757
DelphiXE=Y
17571758
Delphi12A=Y
1759+
1760+
[DigitSumBase]
1761+
DisplayName="DigitSumBase"
1762+
DescEx="<p>Calculates the sum of all the digits of integer <var>N</var> when epxressed in base <var>Base</var>. The returned value has the same sign as <var>N</var>.</p><p>Bases up to 255 are supported. If <var>Base</var> &lt; 2 then an <var>EArgumentException</var> exception is raised.</p>"
1763+
Kind=routine
1764+
Units=SysUtils,Math
1765+
SeeAlso=DigitSum
1766+
TestInfo=advanced
1767+
AdvancedTest.Level=unit-tests
1768+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1769+
Snip=657.dat
1770+
DelphiXE=Y
1771+
Delphi12A=Y
1772+
1773+
[DigitCountBase]
1774+
DisplayName="DigitCountBase"
1775+
DescEx="<p>Returns the number of digits in integer <var>N</var> when expressed in base <var>Base</var>.</p><p>Bases up to 255 are supported. If <var>Base</var> &lt; 2 then an <var>EArgumentException</var> exception is raised.</p>"
1776+
Extra="<p>The number of digits of an integer <em>n</em> &gt; 0 in base <em>b</em> &gt;= 2 can be expressed mathematically as:</p><p><mono>floor(log<em>b</em>(<em>n</em>)) + 1</mono></p><p>Unfortunately testing a Pascal implementation of this formula failed on some tests, e.g. <mono>DigitCount($FFFFFFFF,16)</mono>. This was probably due to floating point rounding errors. Therefore this implementation using only integer operations was used instead.</p>"
1777+
Kind=routine
1778+
Units=SysUtils
1779+
SeeAlso=DigitCount,DigitCount2,DigitCountR
1780+
TestInfo=advanced
1781+
AdvancedTest.Level=unit-tests
1782+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1783+
Snip=658.dat
1784+
DelphiXE=Y
1785+
Delphi12A=Y
1786+
1787+
[DigitsOf]
1788+
DisplayName=DigitsOf
1789+
DescEx="<p>Returns an array containing the digits of integer <var>N</var> when expressed in base <var>Base</var>. The array is ordered with the least significant digit first.</p><p>The returned array contains the <em>decimal value</em> of the digit, for e.g. the hex symbol <em>F</em> is represented by an array element containing the value <em>15</em>.</p><p>Bases up to 255 are supported. If <var>Base</var> &lt; 2 then an <var>EArgumentException</var> exception is raised.</p>"
1790+
Extra="<p>Examples:</p><p>1. <mono>DigitsOf($FACE,16)</mono> returns <mono>[15,10,12,14]</mono></p><p>2. <mono>DigitsOf(12,8)</mono> returns <mono>[1,4]</mono></p><p>3. <mono>DigitsOf(6,2)</mono> returns <mono>[1,1,0]</mono></p><p>4. <mono>DigitsOf(6,10)</mono> returns <mono>[6]</mono></p><p>5. <mono>DigitsOf(0,8)</mono> returns <mono>[0]</mono></p>"
1791+
Kind=routine
1792+
Units=SysUtils
1793+
Depends=DigitCountBase
1794+
TestInfo=advanced
1795+
AdvancedTest.Level=unit-tests
1796+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1797+
Snip=659.dat
1798+
DelphiXE=Y
1799+
Delphi12A=Y

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /