View Issue Details

IDProjectCategoryView StatusLast Update
0032804FPCRTLpublic2017-12-29 11:33
ReporterAnton ShepelevAssigned ToMichael Van Canneyt 
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Product Version3.0.2Product Build 
Target Version3.2.0Fixed in Version3.1.1 
Summary0032804: The functions for variance in the Math unit are broken
DescriptionHello, all.

The functions for the calculation of variance and
standard deviaion in the Math unit are broken. See
for yourselves:

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.

The blame is on the formula used throughout the
unit:
               Summ(x_i^)/n - mean^2

which, although mathematically correct, is totally
unfit for numerical calculations. Attached is a
patch that fixed the problem.
Steps To ReproduceCompile and run the attached program Vartest.pas .
TagsNo tags attached.
Fixed in Revision37791
FPCOldBugId
FPCTarget
Attached Files

Activities

Anton Shepelev

2017-12-09 15:57

reporter  

math.variance.bug.zip (1,543 bytes)

Anton Shepelev

2017-12-09 16:12

reporter   ~0104609

To clarify—I have attached both a test program and a suggested fix in a single archive.

Anton Shepelev

2017-12-10 10:52

reporter   ~0104619

I have made an error in the patch, so please disregard it until I upload a correct one.

Anton Shepelev

2017-12-10 13:10

reporter  

FixedPatch.zip (1,759 bytes)

Anton Shepelev

2017-12-10 13:10

reporter   ~0104621

A fixed patch uploaded in FixedPatch.zip.

Michael Van Canneyt

2017-12-29 11:33

administrator   ~0105095

Applied patch, added example to documentation. Thank you very much!

Issue History

Date Modified Username Field Change
2017-12-09 15:57 Anton Shepelev New Issue
2017-12-09 15:57 Anton Shepelev File Added: math.variance.bug.zip
2017-12-09 16:12 Anton Shepelev Note Added: 0104609
2017-12-10 10:52 Anton Shepelev Note Added: 0104619
2017-12-10 13:10 Anton Shepelev File Added: FixedPatch.zip
2017-12-10 13:10 Anton Shepelev Note Added: 0104621
2017-12-24 10:58 Michael Van Canneyt Assigned To => Michael Van Canneyt
2017-12-24 10:58 Michael Van Canneyt Status new => assigned
2017-12-29 11:33 Michael Van Canneyt Fixed in Revision => 37791
2017-12-29 11:33 Michael Van Canneyt Note Added: 0105095
2017-12-29 11:33 Michael Van Canneyt Status assigned => resolved
2017-12-29 11:33 Michael Van Canneyt Fixed in Version => 3.1.1
2017-12-29 11:33 Michael Van Canneyt Resolution open => fixed
2017-12-29 11:33 Michael Van Canneyt Target Version => 3.2.0