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.
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
PI = 3.141592653589793
TAU = 6.283185307179586
E = 2.718281828459045
MaxFloat64Precision = 16
float64
type. MaxFloat32Precision = 8
float32
type. MaxFloatPrecision = 16
float
type. proc binom(n, k: int): int {...}{.noSideEffect, raises: [], tags: [].}
proc fac(n: int): int {...}{.raises: [], tags: [].}
proc classify(x: float): FloatClass {...}{.raises: [], tags: [].}
proc isPowerOfTwo(x: int): bool {...}{.noSideEffect, raises: [], tags: [].}
proc nextPowerOfTwo(x: int): int {...}{.noSideEffect, raises: [], tags: [].}
proc countBits32(n: int32): int {...}{.noSideEffect, raises: [], tags: [].}
proc sum[T](x: openArray[T]): T {...}{.noSideEffect.}
proc prod[T](x: openArray[T]): T {...}{.noSideEffect.}
x
. If x
is empty, 1 is returned. proc sqrt(x: float32): float32 {...}{.importc: "sqrtf", header: "<math.h>".}
proc sqrt(x: float64): float64 {...}{.importc: "sqrt", header: "<math.h>".}
proc cbrt(x: float32): float32 {...}{.importc: "cbrtf", header: "<math.h>".}
proc cbrt(x: float64): float64 {...}{.importc: "cbrt", header: "<math.h>".}
proc ln(x: float32): float32 {...}{.importc: "logf", header: "<math.h>".}
proc ln(x: float64): float64 {...}{.importc: "log", header: "<math.h>".}
proc log[T: SomeFloat](x, base: T): T
base
of x
proc log10(x: float32): float32 {...}{.importc: "log10f", header: "<math.h>".}
proc log10(x: float64): float64 {...}{.importc: "log10", header: "<math.h>".}
proc exp(x: float32): float32 {...}{.importc: "expf", header: "<math.h>".}
proc exp(x: float64): float64 {...}{.importc: "exp", header: "<math.h>".}
proc sin(x: float32): float32 {...}{.importc: "sinf", header: "<math.h>".}
proc sin(x: float64): float64 {...}{.importc: "sin", header: "<math.h>".}
proc cos(x: float32): float32 {...}{.importc: "cosf", header: "<math.h>".}
proc cos(x: float64): float64 {...}{.importc: "cos", header: "<math.h>".}
proc tan(x: float32): float32 {...}{.importc: "tanf", header: "<math.h>".}
proc tan(x: float64): float64 {...}{.importc: "tan", header: "<math.h>".}
proc sinh(x: float32): float32 {...}{.importc: "sinhf", header: "<math.h>".}
proc sinh(x: float64): float64 {...}{.importc: "sinh", header: "<math.h>".}
proc cosh(x: float32): float32 {...}{.importc: "coshf", header: "<math.h>".}
proc cosh(x: float64): float64 {...}{.importc: "cosh", header: "<math.h>".}
proc tanh(x: float32): float32 {...}{.importc: "tanhf", header: "<math.h>".}
proc tanh(x: float64): float64 {...}{.importc: "tanh", header: "<math.h>".}
proc arccos(x: float32): float32 {...}{.importc: "acosf", header: "<math.h>".}
proc arccos(x: float64): float64 {...}{.importc: "acos", header: "<math.h>".}
proc arcsin(x: float32): float32 {...}{.importc: "asinf", header: "<math.h>".}
proc arcsin(x: float64): float64 {...}{.importc: "asin", header: "<math.h>".}
proc arctan(x: float32): float32 {...}{.importc: "atanf", header: "<math.h>".}
proc arctan(x: float64): float64 {...}{.importc: "atan", header: "<math.h>".}
proc arctan2(y, x: float32): float32 {...}{.importc: "atan2f", header: "<math.h>".}
proc arctan2(y, x: float64): float64 {...}{.importc: "atan2", header: "<math.h>".}
proc arcsinh(x: float32): float32 {...}{.importc: "asinhf", header: "<math.h>".}
proc arcsinh(x: float64): float64 {...}{.importc: "asinh", header: "<math.h>".}
proc arccosh(x: float32): float32 {...}{.importc: "acoshf", header: "<math.h>".}
proc arccosh(x: float64): float64 {...}{.importc: "acosh", header: "<math.h>".}
proc arctanh(x: float32): float32 {...}{.importc: "atanhf", header: "<math.h>".}
proc arctanh(x: float64): float64 {...}{.importc: "atanh", header: "<math.h>".}
proc cot[T: float32 | float64](x: T): T
proc sec[T: float32 | float64](x: T): T
proc csc[T: float32 | float64](x: T): T
proc coth[T: float32 | float64](x: T): T
proc sech[T: float32 | float64](x: T): T
proc csch[T: float32 | float64](x: T): T
proc arccot[T: float32 | float64](x: T): T
proc arcsec[T: float32 | float64](x: T): T
proc arccsc[T: float32 | float64](x: T): T
proc arccoth[T: float32 | float64](x: T): T
proc arcsech[T: float32 | float64](x: T): T
proc arccsch[T: float32 | float64](x: T): T
proc hypot(x, y: float32): float32 {...}{.importc: "hypotf", header: "<math.h>".}
proc hypot(x, y: float64): float64 {...}{.importc: "hypot", header: "<math.h>".}
sqrt(x*x + y*y)
. proc pow(x, y: float32): float32 {...}{.importc: "powf", header: "<math.h>".}
proc pow(x, y: float64): float64 {...}{.importc: "pow", header: "<math.h>".}
computes x to power raised of y.
To compute power between integers, use ^ e.g. 2 ^ 6
proc erf(x: float32): float32 {...}{.importc: "erff", header: "<math.h>".}
proc erf(x: float64): float64 {...}{.importc: "erf", header: "<math.h>".}
proc erfc(x: float32): float32 {...}{.importc: "erfcf", header: "<math.h>".}
proc erfc(x: float64): float64 {...}{.importc: "erfc", header: "<math.h>".}
proc gamma(x: float32): float32 {...}{.importc: "tgammaf", header: "<math.h>".}
proc gamma(x: float64): float64 {...}{.importc: "tgamma", header: "<math.h>".}
proc tgamma(x: float32): float32 {...}{.deprecated: "use gamma instead", importc: "tgammaf", header: "<math.h>".}
proc tgamma(x: float64): float64 {...}{.deprecated: "use gamma instead", importc: "tgamma", header: "<math.h>".}
gamma
instead. proc lgamma(x: float32): float32 {...}{.importc: "lgammaf", header: "<math.h>".}
proc lgamma(x: float64): float64 {...}{.importc: "lgamma", header: "<math.h>".}
proc floor(x: float32): float32 {...}{.importc: "floorf", header: "<math.h>".}
proc floor(x: float64): float64 {...}{.importc: "floor", header: "<math.h>".}
echo floor(-3.5) ## -4.0
proc ceil(x: float32): float32 {...}{.importc: "ceilf", header: "<math.h>".}
proc ceil(x: float64): float64 {...}{.importc: "ceil", header: "<math.h>".}
echo ceil(-2.1) ## -2.0
proc trunc(x: float32): float32 {...}{.importc: "truncf", header: "<math.h>".}
proc trunc(x: float64): float64 {...}{.importc: "trunc", header: "<math.h>".}
echo trunc(PI) # 3.0
proc fmod(x, y: float32): float32 {...}{.deprecated: "use mod instead", importc: "fmodf", header: "<math.h>".}
proc fmod(x, y: float64): float64 {...}{.deprecated: "use mod instead", importc: "fmod", header: "<math.h>".}
echo fmod(-2.5, 0.3) ## -0.1
proc `mod`(x, y: float32): float32 {...}{.importc: "fmodf", header: "<math.h>".}
proc `mod`(x, y: float64): float64 {...}{.importc: "fmod", header: "<math.h>".}
proc round[T: float32 | float64](x: T; places: int = 0): T
Round a floating point number.
If places is 0 (or omitted), round to the nearest integral value following normal mathematical rounding rules (e.g. round(54.5) -> 55.0). If places is greater than 0, round to the given number of decimal places, e.g. round(54.346, 2) -> 54.35. If places is negative, round to the left of the decimal place, e.g. round(537.345, -1) -> 540.0
proc floorDiv[T: SomeInteger](x, y: T): T
floor(x / y)
. This is different from the div
operator, which is defined as trunc(x / y)
. That is, div
rounds towards 0
and floorDiv
rounds down. proc floorMod[T: SomeNumber](x, y: T): T
x - (floorDiv(x, y) * y). This proc behaves the same as the ``%
operator in python. proc c_frexp(x: float32; exponent: var int32): float32 {...}{.importc: "frexp", header: "<math.h>".}
proc c_frexp(x: float64; exponent: var int32): float64 {...}{.importc: "frexp", header: "<math.h>".}
proc frexp[T, U](x: T; exponent: var U): T
proc log2(x: float32): float32 {...}{.importc: "log2f", header: "<math.h>".}
proc log2(x: float64): float64 {...}{.importc: "log2", header: "<math.h>".}
proc splitDecimal[T: float32 | float64](x: T): tuple[intpart: T, floatpart: T]
Breaks x into an integral and a fractional part.
Returns a tuple containing intpart and floatpart representing the integer part and the fractional part respectively.
Both parts have the same sign as x. Analogous to the modf function in C.
proc degToRad[T: float32 | float64](d: T): T {...}{.inline.}
proc radToDeg[T: float32 | float64](d: T): T {...}{.inline.}
proc sgn[T: SomeNumber](x: T): int {...}{.inline.}
proc `^`[T](x: T; y: Natural): T
x
to the power y
. x
must be non-negative, use pow for negative exponents. proc gcd[T](x, y: T): T
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." proc gcd(x, y: SomeInteger): SomeInteger
x
and y
. Using binary GCD (aka Stein's) algorithm. proc lcm[T](x, y: T): T
x
and y
.
© 2006–2018 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/math.html