diff options
author | Josep Sanjuas <jsanjuas@gmail.com> | 2015-04-18 13:40:20 +0200 |
---|---|---|
committer | Josep Sanjuas <jsanjuas@gmail.com> | 2015-04-18 13:40:20 +0200 |
commit | f72bb57fff29609336beae56ae148e20d0dd702e (patch) | |
tree | cb2a988e1549a45d1324b92e26bf85300a0e243e /lib | |
parent | 44246b670921aca1d31cb74dcc00139b5ed9a2c2 (diff) | |
download | Nim-f72bb57fff29609336beae56ae148e20d0dd702e.tar.gz |
Convert to float before sum
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/math.nim | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 05c2cf57c..9bd594549 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -117,10 +117,11 @@ proc sum*[T](x: openArray[T]): T {.noSideEffect.} = template toFloat(f: float): float = f proc mean*[T](x: openArray[T]): float {.noSideEffect.} = - ## computes the mean of the elements in `x`. + ## computes the mean of the elements in `x`, which are first converted to floats. ## If `x` is empty, NaN is returned. ## ``toFloat(x: T): float`` must be defined. - result = toFloat(sum(x)) / toFloat(len(x)) + for i in items(x): result = result + toFloat(i) + result = result / toFloat(len(x)) proc variance*[T](x: openArray[T]): float {.noSideEffect.} = ## computes the variance of the elements in `x`. |