Skip to content

Commit 56222ce

Browse files
committed
A RMS functions to Maths category
Added Double & Integer overloads of the RMS function. Added a source code file for each overload. Updated maths.ini with the meta data for each function.
1 parent 8196a2e commit 56222ce

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

collection/702.dat

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function RMS(const A: array of Double): Double; overload;
2+
var
3+
Squares: array of Double;
4+
Idx: Integer;
5+
begin
6+
System.SetLength(Squares, System.Length(A));
7+
for Idx := 0 to Pred(System.Length(A)) do
8+
Squares[Idx] := A[Idx] * A[Idx];
9+
// Note: ArithmeticMean raises exception if A is empty
10+
Result := Math.Power(ArithmeticMean(Squares), 0.5);
11+
end;

collection/703.dat

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function RMS(const A: array of Integer): Double; overload;
2+
var
3+
Squares: array of Double;
4+
Idx: Integer;
5+
Elem: Double;
6+
begin
7+
System.SetLength(Squares, System.Length(A));
8+
for Idx := 0 to Pred(System.Length(A)) do
9+
begin
10+
// convert Integer to Double before squaring to reduce change of overflow
11+
Elem := A[Idx];
12+
Squares[Idx] := Elem * Elem;
13+
end;
14+
// Note: ArithmeticMean raises exception if A is empty
15+
Result := Math.Power(ArithmeticMean(Squares), 0.5);
16+
end;

collection/maths.ini

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,3 +2387,31 @@ AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tes
23872387
Snip=701.dat
23882388
DelphiXE=Y
23892389
Delphi12A=Y
2390+
2391+
[RMS_Double]
2392+
DisplayName="RMS (Double overload)"
2393+
DescEx="<p>Calculates the root mean square of the elements of <var>Double</var> floating point array <var>A</var>.</p><p>Raises <var>EArgumentException</var> if <var>A</var> is empty.</p>"
2394+
Kind=routine
2395+
Units=SysUtils,Math
2396+
Depends=ArithmeticMean_Double
2397+
SeeAlso=RMS_Integer
2398+
TestInfo=advanced
2399+
AdvancedTest.Level=unit-tests
2400+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2401+
Snip=702.dat
2402+
DelphiXE=Y
2403+
Delphi12A=Y
2404+
2405+
[RMS_Integer]
2406+
DisplayName="RMS (Integer overload)"
2407+
DescEx="<p>Calculates the root mean square of the elements of <var>Integer</var> array <var>A</var>.</p><p>Raises <var>EArgumentException</var> if <var>A</var> is empty.</p>"
2408+
Kind=routine
2409+
Units=SysUtils,Math
2410+
Depends=ArithmeticMean_Double
2411+
SeeAlso=RMS_Double
2412+
TestInfo=advanced
2413+
AdvancedTest.Level=unit-tests
2414+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2415+
Snip=703.dat
2416+
DelphiXE=Y
2417+
Delphi12A=Y

0 commit comments

Comments
 (0)