diff options
Diffstat (limited to 'lib/pure/math.nim')
-rwxr-xr-x | lib/pure/math.nim | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim index 8e3dd4bdb..c9f71b64b 100755 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -159,6 +159,7 @@ when not defined(ECMAScript): ## same as ``sqrt(x*x + y*y)``. proc sinh*(x: float): float {.importc: "sinh", header: "<math.h>".} + proc sin*(x: float): float {.importc: "sin", header: "<math.h>".} proc tan*(x: float): float {.importc: "tan", header: "<math.h>".} proc tanh*(x: float): float {.importc: "tanh", header: "<math.h>".} proc pow*(x, y: float): float {.importc: "pow", header: "<math.h>".} @@ -209,6 +210,7 @@ else: proc cosh*(x: float): float = return (exp(x)+exp(-x))*0.5 proc hypot*(x, y: float): float = return sqrt(x*x + y*y) proc sinh*(x: float): float = return (exp(x)-exp(-x))*0.5 + proc sin*(x: float): float {.importc: "Math.sin", nodecl.} proc tan*(x: float): float {.importc: "Math.tan", nodecl.} proc tanh*(x: float): float = var y = exp(2.0*x) @@ -226,21 +228,27 @@ proc push*(s: var TRunningStat, x: float) = inc(s.n) # See Knuth TAOCP vol 2, 3rd edition, page 232 if s.n == 1: + s.min = x + s.max = x s.oldM = x s.mean = x s.oldS = 0.0 else: + if s.min > x: s.min = x + if s.max < x: s.max = x s.mean = s.oldM + (x - s.oldM)/toFloat(s.n) s.newS = s.oldS + (x - s.oldM)*(x - s.mean) # set up for next iteration: s.oldM = s.mean s.oldS = s.newS - s.sum = s.sum + x - if s.min > x: s.min = x - if s.max < x: s.max = x - + +proc push*(s: var TRunningStat, x: int) = + ## pushes a value `x` for processing. `x` is simply converted to ``float`` + ## and the other push operation is called. + push(s, toFloat(x)) + proc variance*(s: TRunningStat): float = ## computes the current variance of `s` if s.n > 1: result = s.newS / (toFloat(s.n - 1)) |