summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorJosep Sanjuas <jsanjuas@gmail.com>2015-04-18 13:28:48 +0200
committerJosep Sanjuas <jsanjuas@gmail.com>2015-04-18 13:28:48 +0200
commite76f54e059fc89bb2cdab0a61700988c56b609d3 (patch)
tree2f4cd9e6c837d96aa1173951a79a81bc7563e40b /lib/pure
parentd0ff06b8c14e3b505d0c14ca5c9c37e8b667c875 (diff)
downloadNim-e76f54e059fc89bb2cdab0a61700988c56b609d3.tar.gz
Generalize variance to other types
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/math.nim6
1 files changed, 3 insertions, 3 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim
index 0051c9d88..ef9c69e3c 100644
--- a/lib/pure/math.nim
+++ b/lib/pure/math.nim
@@ -122,13 +122,13 @@ proc mean*[T](x: openArray[T]): float {.noSideEffect.} =
   ## ``toFloat(x: T): float`` must be defined.
   result = toFloat(sum(x)) / toFloat(len(x))
 
-proc variance*(x: openArray[float]): float {.noSideEffect.} = 
+proc variance*[T](x: openArray[T]): float {.noSideEffect.} =
   ## computes the variance of the elements in `x`. 
   ## If `x` is empty, NaN is returned.
   result = 0.0
   var m = mean(x)
-  for i in 0 .. high(x):
-    var diff = x[i] - m
+  for i in items(x):
+    var diff = toFloat(i) - m
     result = result + diff*diff
   result = result / toFloat(len(x))