View Issue Details
ID  Project  Category  View Status  Date Submitted  Last Update 

0032804  FPC  RTL  public  20171209 15:57  20171229 11:33 
Reporter  Anton Shepelev  Assigned To  Michael Van Canneyt  
Priority  normal  Severity  minor  Reproducibility  always 
Status  resolved  Resolution  fixed  
Product Version  3.0.2  Product Build  
Target Version  3.2.0  Fixed in Version  3.1.1  
Summary  0032804: The functions for variance in the Math unit are broken  
Description  Hello, 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 Reproduce  Compile and run the attached program Vartest.pas .  
Tags  No tags attached.  
Fixed in Revision  37791  
FPCOldBugId  
FPCTarget  
Attached Files 


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

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

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

FixedPatch.zip (1,759 bytes) 

A fixed patch uploaded in FixedPatch.zip. 

Applied patch, added example to documentation. Thank you very much! 
Date Modified  Username  Field  Change 

20171209 15:57  Anton Shepelev  New Issue  
20171209 15:57  Anton Shepelev  File Added: math.variance.bug.zip  
20171209 16:12  Anton Shepelev  Note Added: 0104609  
20171210 10:52  Anton Shepelev  Note Added: 0104619  
20171210 13:10  Anton Shepelev  File Added: FixedPatch.zip  
20171210 13:10  Anton Shepelev  Note Added: 0104621  
20171224 10:58  Michael Van Canneyt  Assigned To  => Michael Van Canneyt 
20171224 10:58  Michael Van Canneyt  Status  new => assigned 
20171229 11:33  Michael Van Canneyt  Fixed in Revision  => 37791 
20171229 11:33  Michael Van Canneyt  Note Added: 0105095  
20171229 11:33  Michael Van Canneyt  Status  assigned => resolved 
20171229 11:33  Michael Van Canneyt  Fixed in Version  => 3.1.1 
20171229 11:33  Michael Van Canneyt  Resolution  open => fixed 
20171229 11:33  Michael Van Canneyt  Target Version  => 3.2.0 