Module math

Constructive mathematics is naturally typed. -- Simon Thompson

Basic math routines for Nimrod. This module is available for the JavaScript target.

Types

TFloatClass = enum 
  fcNormal,                   ## value is an ordinary nonzero floating point value
  fcSubnormal,                ## value is a subnormal (a very small) floating point value
  fcZero,                     ## value is zero
  fcNegZero,                  ## value is the negative zero
  fcNan,                      ## value is Not-A-Number (NAN)
  fcInf,                      ## value is positive infinity
  fcNegInf                    ## value is negative infinity
describes the class a floating point value belongs to. This is the type that is returned by classify.
TRunningStat = object 
  n*: int                     ## number of pushed data
  sum*, min*, max*, mean*: float ## self-explaining
  oldM, oldS, newS: float
an accumulator for statistical data

Consts

PI = 3.1415926535897936e+00
the circle constant PI (Ludolph's number)
E = 2.7182818284590455e+00
Euler's number
MaxFloat64Precision = 16
maximum number of meaningful digits after the decimal point for Nimrod's float64 type.
MaxFloat32Precision = 8
maximum number of meaningful digits after the decimal point for Nimrod's float32 type.
MaxFloatPrecision = 16
maximum number of meaningful digits after the decimal point for Nimrod's float type.

Procs

proc classify(x: float): TFloatClass {.raises: [], tags: [].}
classifies a floating point value. Returns x's class as specified by TFloatClass.
proc binom(n, k: int): int {.noSideEffect, raises: [], tags: [].}
computes the binomial coefficient
proc fac(n: int): int {.noSideEffect, raises: [], tags: [].}
computes the faculty function
proc isPowerOfTwo(x: int): bool {.noSideEffect, raises: [], tags: [].}
returns true, if x is a power of two, false otherwise. Negative numbers are not a power of two.
proc nextPowerOfTwo(x: int): int {.raises: [], tags: [].}
returns the nearest power of two, so that result**2 >= x > (result-1)**2.
proc countBits32(n: int32): int {.noSideEffect, raises: [], tags: [].}
counts the set bits in n.
proc sum[T](x: openArray[T]): T {.noSideEffect.}
computes the sum of the elements in x. If x is empty, 0 is returned.
proc mean(x: openArray[float]): float {.noSideEffect, raises: [], tags: [].}
computes the mean of the elements in x. If x is empty, NaN is returned.
proc variance(x: openArray[float]): float {.noSideEffect, raises: [], tags: [].}
computes the variance of the elements in x. If x is empty, NaN is returned.
proc sqrt(x: float): float {.importc: "sqrt", header: "<math.h>".}
computes the square root of x.
proc ln(x: float): float {.importc: "log", header: "<math.h>".}
computes ln(x).
proc log10(x: float): float {.importc: "log10", header: "<math.h>".}
proc log2(x: float): float {.raises: [], tags: [].}
proc exp(x: float): float {.importc: "exp", header: "<math.h>".}
computes e**x.
proc frexp(x: float; exponent: var int): float {.importc: "frexp", 
    header: "<math.h>".}
Split a number into mantissa and exponent. frexp calculates the mantissa m (a float greater than or equal to 0.5 and less than 1) and the integer value n such that x (the original float value) equals m * 2**n. frexp stores n in exponent and returns m.
proc round(x: float): int {.importc: "lrint", header: "<math.h>".}
converts a float to an int by rounding.
proc arccos(x: float): float {.importc: "acos", header: "<math.h>".}
proc arcsin(x: float): float {.importc: "asin", header: "<math.h>".}
proc arctan(x: float): float {.importc: "atan", header: "<math.h>".}
proc arctan2(y, x: float): float {.importc: "atan2", header: "<math.h>".}
Calculate the arc tangent of y / x. atan2 returns the arc tangent of y / x; it produces correct results even when the resulting angle is near pi/2 or -pi/2 (x near 0).
proc cos(x: float): float {.importc: "cos", header: "<math.h>".}
proc cosh(x: float): float {.importc: "cosh", header: "<math.h>".}
proc hypot(x, y: float): float {.importc: "hypot", header: "<math.h>".}
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>".}
computes x to power raised of y.
proc random(max: float): float {.raises: [], tags: [].}
returns a random number in the range 0..<max. The sequence of random number is always the same, unless randomize is called which initializes the random number generator with a "random" number, i.e. a tickcount. This is currently not supported for windows.
proc randomize() {.raises: [E_Base], tags: [TEffect].}
initializes the random number generator with a "random" number, i.e. a tickcount. Note: Does nothing for the JavaScript target, as JavaScript does not support this.
proc randomize(seed: int) {.raises: [], tags: [].}
initializes the random number generator with a specific seed. Note: Does nothing for the JavaScript target, as JavaScript does not support this.
proc random(max: int): int {.raises: [], tags: [].}
returns a random number in the range 0..max-1. The sequence of random number is always the same, unless randomize is called which initializes the random number generator with a "random" number, i.e. a tickcount.
proc trunc(x: float): float {.importc: "trunc", header: "<math.h>".}
proc floor(x: float): float {.importc: "floor", header: "<math.h>".}
proc ceil(x: float): float {.importc: "ceil", header: "<math.h>".}
proc fmod(x, y: float): float {.importc: "fmod", header: "<math.h>".}
proc `mod`(x, y: float): float {.raises: [], tags: [].}
proc random[T](x: TSlice[T]): T
proc push(s: var TRunningStat; x: float) {.raises: [], tags: [].}
pushes a value x for processing
proc push(s: var TRunningStat; x: int) {.raises: [], tags: [].}
pushes a value x for processing. x is simply converted to float and the other push operation is called.
proc variance(s: TRunningStat): float {.raises: [], tags: [].}
computes the current variance of s
proc standardDeviation(s: TRunningStat): float {.raises: [], tags: [].}
computes the current standard deviation of s
Generated: 2014-03-11 21:26:38 UTC