From f72bb57fff29609336beae56ae148e20d0dd702e Mon Sep 17 00:00:00 2001 From: Josep Sanjuas Date: Sat, 18 Apr 2015 13:40:20 +0200 Subject: [PATCH] Convert to float before sum --- lib/pure/math.nim | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 05c2cf57cb..9bd5945498 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`.