<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
Am 23.12.2017 um 13:24 schrieb Anton Shepelev:<br>
<blockquote type="cite"
cite="mid:20171223152411.009732210e4cde7e700319a8@gmail.com">
<pre wrap="">Hello, all
May I ask you if there is any chance of fixing the
error in the Math unit that I reported here:
<a class="moz-txt-link-freetext" href="https://bugs.freepascal.org/view.php?id=32804">https://bugs.freepascal.org/view.php?id=32804</a>
and provided a patch? Here is a test case:
Program Vartest;
uses Math;
const Size = 1000000;
var dataS: array of Single;
dataD: array of Double;
dataE: array of Extended;
i,n: longint;
begin
WriteLn('Each run should return a value near unity.');
WriteLn('Single:');
SetLength( dataS, Size );
for n := 1 to 4 do
begin
for i := 0 to Size - 1 do
begin
dataS[i] := 10000000 + RandG(0,1);
end;
WriteLn( Math.Variance( dataS ):5:3 );
end;
WriteLn('Double:');
SetLength( dataD, Size );
for n := 1 to 4 do
begin
for i := 0 to Size - 1 do
begin
dataD[i] := 1000000000000000 + RandG(0,1);
end;
WriteLn( Math.Variance( dataD ):5:3 );
end;
WriteLn('Extended:');
SetLength( dataE, Size );
for n := 1 to 4 do
begin
for i := 0 to Size - 1 do
begin
dataE[i] := 1000000000000000000 + RandG(0,1);
end;
WriteLn( Math.Variance( dataE ):5:3 );
end;
end.
</pre>
</blockquote>
<br>
Yes, the formula used isĀ SS-Sqr(S)/N, and this is the bad one.
Explained somewhere in the Numerical Recipes, but also in Widepedia
(<a class="moz-txt-link-freetext" href="https://en.wikipedia.org/wiki/Variance#Formulae_for_the_variance">https://en.wikipedia.org/wiki/Variance#Formulae_for_the_variance</a>):
"it is not advised for computer calculations as it suffers from <a
href="https://en.wikipedia.org/wiki/Catastrophic_cancellation"
class="mw-redirect" title="Catastrophic cancellation">catastrophic
cancellation</a> if the two components of the equation are similar
in magnitude and floating point arithmetic is used."
</body>
</html>