Skip to content

Commit 9f090ee

Browse files
committed
Update var.h
Variance: Change from the unstable one-pass formula back to the stable two-pass formula. Details: #223
1 parent a2fc8a3 commit 9f090ee

File tree

1 file changed

+4
-4
lines changed
  • inst/include/Rcpp/sugar/functions

1 file changed

+4
-4
lines changed

inst/include/Rcpp/sugar/functions/var.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class Var : public Lazy< double , Var<RTYPE,NA,T> > {
3434
Var( const VEC_TYPE& object_ ) : object(object_){}
3535

3636
double get() const{
37-
double
38-
m = mean(object).get(),
39-
ssq = sum( pow(object, 2.0) ).get();
40-
return (ssq - m * m * object.size()) / (object.size() - 1);
37+
const double
38+
average = mean(object).get(),
39+
sum_squared_deviations = sum( pow(object - average, 2.0) ).get();
40+
return sum_squared_deviations / (object.size() - 1);
4141
}
4242

4343
private:

0 commit comments

Comments
 (0)