summary refs log tree commit diff stats
path: root/lib/pure/stats.nim
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pure/stats.nim')
-rw-r--r--lib/pure/stats.nim16
1 files changed, 11 insertions, 5 deletions
diff --git a/lib/pure/stats.nim b/lib/pure/stats.nim
index 6e2d5fecd..6a4fd8f01 100644
--- a/lib/pure/stats.nim
+++ b/lib/pure/stats.nim
@@ -42,7 +42,7 @@ runnableExamples:
 
   template `~=`(a, b: float): bool = almostEqual(a, b)
 
-  var statistics: RunningStat  ## Must be var
+  var statistics: RunningStat  # must be var
   statistics.push(@[1.0, 2.0, 1.0, 4.0, 1.0, 4.0, 1.0, 2.0])
   doAssert statistics.n == 8
   doAssert statistics.mean() ~= 2.0
@@ -55,6 +55,9 @@ runnableExamples:
 
 from std/math import FloatClass, sqrt, pow, round
 
+when defined(nimPreviewSlimSystem):
+  import std/[assertions, formatfloat]
+
 {.push debugger: off.} # the user does not want to trace a part
                        # of the standard library!
 {.push checks: off, line_dir: off, stack_trace: off.}
@@ -76,7 +79,7 @@ type
 proc clear*(s: var RunningStat) =
   ## Resets `s`.
   s.n = 0
-  s.min = toBiggestFloat(int.high)
+  s.min = 0.0
   s.max = 0.0
   s.sum = 0.0
   s.mom1 = 0.0
@@ -86,11 +89,14 @@ proc clear*(s: var RunningStat) =
 
 proc push*(s: var RunningStat, x: float) =
   ## Pushes a value `x` for processing.
-  if s.n == 0: s.min = x
+  if s.n == 0:
+    s.min = x
+    s.max = x
+  else:
+    if s.min > x: s.min = x
+    if s.max < x: s.max = x
   inc(s.n)
   # See Knuth TAOCP vol 2, 3rd edition, page 232
-  if s.min > x: s.min = x
-  if s.max < x: s.max = x
   s.sum += x
   let n = toFloat(s.n)
   let delta = x - s.mom1