Module math

Constructive mathematics is naturally typed. -- Simon Thompson

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

Types

FloatClass = 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.   Source
RunningStat = object 
  n*: int                     ## number of pushed data
  sum*, min*, max*, mean*: float ## self-explaining
  oldM, oldS, newS: float
an accumulator for statistical data   Source

Consts

PI = 3.141592653589793
the circle constant PI (Ludolph's number)   Source
E = 2.718281828459045
Euler's number   Source
MaxFloat64Precision = 16
maximum number of meaningful digits after the decimal point for Nim's float64 type.   Source
MaxFloat32Precision = 8
maximum number of meaningful digits after the decimal point for Nim's float32 type.   Source
MaxFloatPrecision = 16
maximum number of meaningful digits after the decimal point for Nim's float type.   Source

Procs

proc classify(x: float): FloatClass {.raises: [], tags: [].}
classifies a floating point value. Returns x's class as specified by FloatClass.   Source
proc binom(n, k: int): int {.noSideEffect, raises: [], tags: [].}
computes the binomial coefficient   Source
proc fac(n: int): int {.noSideEffect, raises: [], tags: [].}
computes the faculty/factorial function.   Source
proc isPowerOfTwo(x: int): bool {.noSideEffect, raises: [], tags: [].}
returns true, if x is a power of two, false otherwise. Zero and negative numbers are not a power of two.   Source
proc nextPowerOfTwo(x: int): int {.noSideEffect, raises: [], tags: [].}
returns x rounded up to the nearest power of two. Zero and negative numbers get rounded up to 1.   Source
proc countBits32(n: int32): int {.noSideEffect, raises: [], tags: [].}
counts the set bits in n.   Source
proc sum[T](x: openArray[T]): T {.noSideEffect.}
computes the sum of the elements in x. If x is empty, 0 is returned.   Source
proc mean(x: openArray[float]): float {.noSideEffect, raises: [], tags: [].}
computes the mean of the elements in x. If x is empty, NaN is returned.   Source
proc variance(x: openArray[float]): float {.noSideEffect, raises: [], tags: [].}
computes the variance of the elements in x. If x is empty, NaN is returned.   Source
proc sqrt(x: float): float {.importc: "sqrt", header: "<math.h>".}
computes the square root of x.   Source
proc ln(x: float): float {.importc: "log", header: "<math.h>".}
computes ln(x).   Source
proc log10(x: float): float {.importc: "log10", header: "<math.h>".}
  Source
proc log2(x: float): float {.raises: [], tags: [].}
  Source
proc exp(x: float): float {.importc: "exp", header: "<math.h>".}
computes e**x.   Source
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.   Source
proc round(x: float): int {.importc: "lrint", header: "<math.h>".}
converts a float to an int by rounding.   Source
proc arccos(x: float): float {.importc: "acos", header: "<math.h>".}
  Source
proc arcsin(x: float): float {.importc: "asin", header: "<math.h>".}
  Source
proc arctan(x: float): float {.importc: "atan", header: "<math.h>".}
  Source
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).   Source
proc cos(x: float): float {.importc: "cos", header: "<math.h>".}
  Source
proc cosh(x: float): float {.importc: "cosh", header: "<math.h>".}
  Source
proc hypot(x, y: float): float {.importc: "hypot", header: "<math.h>".}
same as sqrt(x*x + y*y).   Source
proc sinh(x: float): float {.importc: "sinh", header: "<math.h>".}
  Source
proc sin(x: float): float {.importc: "sin", header: "<math.h>".}
  Source
proc tan(x: float): float {.importc: "tan", header: "<math.h>".}
  Source
proc tanh(x: float): float {.importc: "tanh", header: "<math.h>".}
  Source
proc pow(x, y: float): float {.importc: "pow", header: "<math.h>".}
computes x to power raised of y.   Source
proc random(max: float): float {.gcsafe, locks: 0, 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 has a 16-bit resolution on windows and a 48-bit resolution on other platforms.   Source
proc randomize() {.gcsafe, locks: 0, raises: [Exception], 
                   tags: [RootEffect, TimeEffect].}
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.   Source
proc randomize(seed: int) {.gcsafe, locks: 0, raises: [], tags: [].}
initializes the random number generator with a specific seed. Note: Does nothing for the JavaScript target, as JavaScript does not support this.   Source
proc random(max: int): int {.gcsafe, locks: 0, 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.   Source
proc trunc(x: float): float {.importc: "trunc", header: "<math.h>".}
  Source
proc floor(x: float): float {.importc: "floor", header: "<math.h>".}
  Source
proc ceil(x: float): float {.importc: "ceil", header: "<math.h>".}
  Source
proc fmod(x, y: float): float {.importc: "fmod", header: "<math.h>".}
  Source
proc `mod`(x, y: float): float {.raises: [], tags: [].}
  Source
proc random[T](x: Slice[T]): T
For a slice a .. b returns a value in the range a .. b-1.   Source
proc random[T](a: openArray[T]): T
returns a random element from the openarray a.   Source
proc push(s: var RunningStat; x: float) {.raises: [], tags: [].}
pushes a value x for processing   Source
proc push(s: var RunningStat; x: int) {.raises: [], tags: [].}
pushes a value x for processing. x is simply converted to float and the other push operation is called.   Source
proc variance(s: RunningStat): float {.raises: [], tags: [].}
computes the current variance of s   Source
proc standardDeviation(s: RunningStat): float {.raises: [], tags: [].}
computes the current standard deviation of s   Source
proc `^`[T](x, y: T): T
Computes x to the power y`. ``x must be non-negative, use pow <#pow,float,float> for negative exponents.   Source
proc gcd[T](x, y: T): T
Computes the greatest common divisor of x and y.   Source
proc lcm[T](x, y: T): T
Computes the least common multiple of x and y.   Source