math
nimony/lib/std/math.nim
type Arithmetic = concept func +(x: Self, y: Self): Self func -(x: Self, y: Self): Self func *(x: Self, y: Self): Self func mod(x: Self, y: Self): Self func ==(x: Self, y: Self): bool func <(x: Self, y: Self): bool func <=(x: Self, y: Self): bool
type IntegerArithmetic = concept of Arithmetic func div(x: Self, y: Self): Self func inc(x: var Self, y: Self) func dec(x: var Self, y: Self)
type SignedArithmetic = concept of Arithmetic func -(x: Self): Self
type FloatArithmetic = concept of SignedArithmetic func /(x: Self, y: Self): Self
type HasSqrt = concept func sqrt(x: Self): Self
type HasExp = concept func exp(x: Self): Self
type HasLn = concept func ln(x: Self): Self
type HasSin = concept func sin(x: Self): Self
type HasCos = concept func cos(x: Self): Self
type HasHypot = concept func hypot(x: Self, y: Self): Self
type HasArctan2 = concept func arctan2(y: Self, x: Self): Self
const PI: float64The circle constant PI (Ludolph's number).
const TAU: float64The circle constant TAU (= 2 * PI).
const E: float64Euler's number.
const MaxFloat64Precision: int64Maximum number of meaningful digits after the decimal point for Nim's
float64type.const MaxFloat32Precision: int64Maximum number of meaningful digits after the decimal point for Nim's
float32type.const MaxFloatPrecision: int64Maximum number of meaningful digits after the decimal point for Nim's
floattype.const MinFloatNormal: float64Smallest normal number for Nim's
floattype (= 2^-1022).type FloatClass = enum fcNormal = (0, "fcNormal") fcSubnormal = (1, "fcSubnormal") fcZero = (2, "fcZero") fcNegZero = (3, "fcNegZero") fcNan = (4, "fcNan") fcInf = (5, "fcInf") fcNegInf = (6, "fcNegInf")
func dollar`.FloatClass(e: FloatClass): stringfunc signbit(x: T): boolReturns true if
xis negative, false otherwise.func copySign(x: float32; y: float32): float32func copySign(x: float64; y: float64): float64Returns a value with the magnitude of
xand the sign ofy; this works even if x or y are NaN, infinity or zero, all of which can carry a sign.func classify(x: T): FloatClassClassifies a floating point value.
Returns
x's class as specified by theFloatClass enum<#FloatClass>_.func isNaN(x: T): boolReturns whether
xis aNaN, more efficiently than viaclassify(x) == fcNan.func almostEqual(x: T; y: T; unitsInLastPlace: int64): boolChecks if two float values are almost equal, using the machine epsilon.
unitsInLastPlaceis the max number of units in the last place difference tolerated when comparing two numbers. The larger the value, the more error is allowed. A0value means that two numbers must be exactly the same to be considered equal.The machine epsilon has to be scaled to the magnitude of the values used and multiplied by the desired precision in ULPs unless the difference is subnormal.
func sgn(x: T): int64Sign function.
Returns:
-1for negative numbers andNegInf,1for positive numbers andInf,0for positive zero, negative zero andNaN
func frexp(x: T): tuple[frac: T, exp: int64]Splits
xinto a normalized fractionfracand an integral power of 2exp, such thatabs(frac) in 0.5..<1andx == frac * 2 ^ exp, except for special cases shown below.func floor(x: float32): float32func floor(x: float64): float64Computes the floor function (i.e. the largest integer not greater than
x).See also:
func ceil(x: float32): float32func ceil(x: float64): float64Computes the ceiling function (i.e. the smallest integer not smaller than
x).See also:
func round(x: float32): float32func round(x: float64): float64Returns the nearest integer value to
x, rounding halfway cases away from zero.See also:
func trunc(x: float32): float32func trunc(x: float64): float64func mod(x: float32; y: float32): float32func mod(x: float64; y: float64): float64Computes the modulo operation for float values (the remainder of
xdivided byy).See also:
- floorMod func for Python-like (
%operator) behavior
- floorMod func for Python-like (
func floorMod(x: T; y: T): TFloor modulo is conceptually defined as
x - (floorDiv(x, y) * y).This func behaves the same as the
%operator in Python.See also:
func floorDiv(x: T; y: T): TFloor division is conceptually defined as
floor(x / y).This is different from the system.div operator, which is defined as
trunc(x / y). That is,divrounds towards0andfloorDivrounds down.See also:
- system.div func for integer division
- floorMod func for Python-like (
%operator) behavior
func euclDiv(x: T; y: T): TReturns euclidean division of
xbyy.func euclMod(x: T; y: T): TReturns euclidean modulo of
xbyy.euclMod(x, y)is non-negative.func ceilDiv(x: T; y: T): TCeil division is conceptually defined as
ceil(x / y).Assumes
x >= 0andy > 0(andx + y - 1 <= high(T)if T is SomeUnsignedInt).This is different from the system.div operator, which works like
trunc(x / y). That is,divrounds towards0andceilDivrounds up.This function has the above input limitation, because that allows the compiler to generate faster code and it is rarely used with negative values or unsigned integers close to
high(T)/2. If you need aceilDivthat works with any input, see: https://github.com/demotomohiro/divmath.See also:
- system.div func for integer division
- floorDiv func for integer division which rounds down.
func divmod(x: T; y: T): tuple[T, T]Computes both division and modulus. Return structure is: (quotient, remainder)
func sum(x: openArray[T]): Tfunc cumsum(x: var openArray[T])Transforms
xin-place (must be declared asvar) into its cumulative (aka prefix) summation.See also:
- sum func
- cumsummed func for a version which
returns a cumsummed sequence
func prod(x: openArray[T]): Tfunc cumprod(x: var openArray[T])Transforms
xin-place (must be declared asvar) into its product.See also:
- prod func
- cumproded func for a version which
returns cumproded sequence
func sqrt(x: float32): float32func sqrt(x: float64): float64func cbrt(x: float32): float32func cbrt(x: float64): float64func pow(x: float32; y: float32): float32func pow(x: float64; y: float64): float64func hypot(x: float32; y: float32): float32func hypot(x: float64; y: float64): float64Computes the length of the hypotenuse of a right-angle triangle with
xas its base andyas its height. Equivalent tosqrt(x*x + y*y).func exp(x: float32): float32func exp(x: float64): float64func ln(x: float32): float32func ln(x: float64): float64func log2(x: float32): float32func log2(x: float64): float64func log10(x: float32): float32func log10(x: float64): float64func log1p(x: float32): float32func log1p(x: float64): float64func log(x: T; base: T): Tfunc ^(x: T; y: int64): Tfunc isPowerOfTwo(x: int64): boolReturns
true, ifxis a power of two,falseotherwise.Zero and negative numbers are not a power of two.
See also:
func nextPowerOfTwo(x: int64): int64Returns
xrounded up to the nearest power of two.Zero and negative numbers get rounded up to 1.
See also:
func degToRad(d: T): Tfunc radToDeg(r: T): Tfunc sin(x: float32): float32func sin(x: float64): float64func cos(x: float32): float32func cos(x: float64): float64func tan(x: float32): float32func tan(x: float64): float64func arcsin(x: float32): float32func arcsin(x: float64): float64func arccos(x: float32): float32func arccos(x: float64): float64func arctan(x: float32): float32func arctan(x: float64): float64func arctan2(y: float32; x: float32): float32func arctan2(y: float64; x: float64): float64Calculate the arc tangent of
y/x.It produces correct results even when the resulting angle is near
PI/2or-PI/2(xnear 0).See also:
func sinh(x: float32): float32func sinh(x: float64): float64func cosh(x: float32): float32func cosh(x: float64): float64func tanh(x: float32): float32func tanh(x: float64): float64func arcsinh(x: float32): float32func arcsinh(x: float64): float64func arccosh(x: float32): float32func arccosh(x: float64): float64func arctanh(x: float32): float32func arctanh(x: float64): float64func erf(x: float32): float32func erf(x: float64): float64Computes the error function for
x.func erfc(x: float32): float32func erfc(x: float64): float64Computes the complementary error function for
x.func gamma(x: float32): float32func gamma(x: float64): float64Computes the gamma function for
x.See also:
- lgamma func for the natural logarithm of the gamma function
func lgamma(x: float32): float32func lgamma(x: float64): float64func cot(x: float32): float32func cot(x: float64): float64func sec(x: float32): float32func sec(x: float64): float64func csc(x: float32): float32func csc(x: float64): float64func coth(x: float32): float32func coth(x: float64): float64func sech(x: float32): float32func sech(x: float64): float64func csch(x: float32): float32func csch(x: float64): float64func arccot(x: float32): float32func arccot(x: float64): float64func arcsec(x: float32): float32func arcsec(x: float64): float64func arccsc(x: float32): float32func arccsc(x: float64): float64func arccoth(x: float32): float32func arccoth(x: float64): float64func arcsech(x: float32): float32func arcsech(x: float64): float64func arccsch(x: float32): float32func arccsch(x: float64): float64func splitDecimal(x: T): tuple[intpart: T, floatpart: T]Breaks
xinto an integer and a fractional part.Returns a tuple containing
intpartandfloatpart, representing the integer part and the fractional part, respectively.Both parts have the same sign as
x. Analogous to themodffunction in C.func fac(n: int64): int64func binom(n: int64; k: int64): int64Computes the binomial coefficient.
func gcd(x: T; y: T): Tfunc gcd(x: openArray[T]): TComputes the greatest common (positive) divisor of the elements of
x.See also:
- gcd func for a version with two arguments
func lcm(x: T; y: T): Tfunc lcm(x: openArray[T]): TComputes the least common multiple of the elements of
x.See also:
- lcm func for a version with two arguments