Constructive mathematics is naturally typed. -- Simon Thompson
Basic math routines for Nimrod. This module is available for the ECMAScript 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* {.pure, final.} = 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* = MaxFloat64Precision
- maximum number of meaningful digits after the decimal point for Nimrod's float type.
Procs
proc classify*(x: float): TFloatClass
- classifies a floating point value. Returns x's class as specified by TFloatClass.
proc binom*(n, k: int): int {.noSideEffect.}
- computes the binomial coefficient
proc fac*(n: int): int {.noSideEffect.}
- computes the faculty function
proc isPowerOfTwo*(x: int): bool {.noSideEffect.}
- returns true, if x is a power of two, false otherwise. Negative numbers are not a power of two.
proc nextPowerOfTwo*(x: int): int
- returns the nearest power of two, so that result**2 >= x > (result-1)**2.
proc countBits32*(n: int32): int {.noSideEffect.}
- 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.}
- computes the mean of the elements in x. If x is empty, NaN is returned.
proc variance*(x: openarray[float]): float {.noSideEffect.}
- computes the variance of the elements in x. If x is empty, NaN is returned.
proc random*(max: int): int
- 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 random*(max: float): float
- 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*()
- initializes the random number generator with a "random" number, i.e. a tickcount. Note: Does nothing for the ECMAScript target, as ECMAScript does not support this.
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
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", nodecl.}
- 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 trunc*(x: float): float {.importc: "trunc", nodecl.}
proc floor*(x: float): float {.importc: "floor", nodecl.}
proc ceil*(x: float): float {.importc: "ceil", nodecl.}
proc fmod*(x, y: float): float {.importc: "fmod", header: "<math.h>".}
proc `mod`*(x, y: float): float
proc random*[T](x: TSlice[T]): T
proc push*(s: var TRunningStat; x: float)
- pushes a value x for processing
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.
proc variance*(s: TRunningStat): float
- computes the current variance of s
proc standardDeviation*(s: TRunningStat): float
- computes the current standard deviation of s