Module math

Constructive mathematics is naturally typed. -- Simon Thompson

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

Note that the trigonometric functions naturally operate on radians. The helper functions degToRad and radToDeg provide conversion between radians and degrees.

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'f64
the circle constant PI (Ludolph's number)   Source
E = 2.718281828459045'f64
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 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 classify(x: float): FloatClass {.raises: [], tags: [].}
Classifies a floating point value. Returns x's class as specified by FloatClass.   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[T](x: openArray[T]): float {.noSideEffect.}
Computes the mean of the elements in x, which are first converted to floats. If x is empty, NaN is returned. toFloat(x: T): float must be defined.   Source
proc variance[T](x: openArray[T]): float {.noSideEffect.}
Computes the variance of the elements in x. If x is empty, NaN is returned. toFloat(x: T): float must be defined.   Source
proc sqrt(x: float): float {.importc: "sqrt", header: "<math.h>".}
Computes the square root of x.   Source
proc cbrt(x: float): float {.importc: "cbrt", header: "<math.h>".}
Computes the cubic root of x   Source
proc ln(x: float): float {.importc: "log", header: "<math.h>".}
Computes the natural log of x   Source
proc log10(x: float): float {.importc: "log10", header: "<math.h>".}
Computes the common logarithm (base 10) of x   Source
proc log2(x: float): float {.raises: [], tags: [].}
Computes the binary logarithm (base 2) of x   Source
proc exp(x: float): float {.importc: "exp", header: "<math.h>".}
Computes the exponential function of x (pow(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>".}
Computes the arc cosine of x   Source
proc arcsin(x: float): float {.importc: "asin", header: "<math.h>".}
Computes the arc sine of x   Source
proc arctan(x: float): float {.importc: "atan", header: "<math.h>".}
Calculate the arc tangent of y / x   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>".}
Computes the cosine of x   Source
proc cosh(x: float): float {.importc: "cosh", header: "<math.h>".}
Computes the hyperbolic cosine of x   Source
proc hypot(x, y: float): float {.importc: "hypot", header: "<math.h>".}
Computes the hypotenuse of a right-angle triangle with x and y as its base and height. Equivalent to sqrt(x*x + y*y).   Source
proc sinh(x: float): float {.importc: "sinh", header: "<math.h>".}
Computes the hyperbolic sine of x   Source
proc sin(x: float): float {.importc: "sin", header: "<math.h>".}
Computes the sine of x   Source
proc tan(x: float): float {.importc: "tan", header: "<math.h>".}
Computes the tangent of x   Source
proc tanh(x: float): float {.importc: "tanh", header: "<math.h>".}
Computes the hyperbolic tangent of x   Source
proc pow(x, y: float): float {.importc: "pow", header: "<math.h>".}
Computes x to power of y.   Source
proc erf(x: float): float {.importc: "erf", header: "<math.h>".}
The error function   Source
proc erfc(x: float): float {.importc: "erfc", header: "<math.h>".}
The complementary error function   Source
proc lgamma(x: float): float {.importc: "lgamma", header: "<math.h>".}
Natural log of the gamma function   Source
proc tgamma(x: float): float {.importc: "tgamma", header: "<math.h>".}
The gamma function   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. Nor does it work for NimScript.   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>".}
Truncates x to the decimal point
echo trunc(PI) # 3.0
  Source
proc floor(x: float): float {.importc: "floor", header: "<math.h>".}
Computes the floor function (i.e., the largest integer not greater than x)
echo floor(-3.5) ## -4.0
  Source
proc ceil(x: float): float {.importc: "ceil", header: "<math.h>".}
Computes the ceiling function (i.e., the smallest integer not less than x)
echo ceil(-2.1) ## -2.0
  Source
proc fmod(x, y: float): float {.importc: "fmod", header: "<math.h>".}
Computes the remainder of x divided by y
echo fmod(-2.5, 0.3) ## -0.1
  Source
proc degToRad[T](d: T): T {.inline.}
Convert from degrees to radians   Source
proc radToDeg[T](d: T): T {.inline.}
Convert from radians to degrees   Source
proc `mod`(x, y: float): float {.raises: [], tags: [].}
Computes the modulo operation for float operators. Equivalent to x - y * floor(x/y). Note that the remainder will always have the same sign as the divisor.
echo (4.0 mod -3.1) # -2.2
  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. Note that for floats, the result cannot always be interpreted as "greatest decimal z such that z*N == x and z*M == y where N and M are positive integers."   Source
proc lcm[T](x, y: T): T
Computes the least common multiple of x and y.   Source