common
nimony/lib/std/math/common.nim
Concepts, constants and freestanding pure-Nim helpers shared by every std/math backend. The general math utilities are adapted from Nim's Runtime Library lib/pure/math.nim; the concepts and backend-independent organization are Nimony-specific. Source: https://github.com/nim-lang/Nim/blob/devel/lib/pure/math.nim Copyright (c) 2015 Andreas Rumpf. MIT license, as in Nim's copying.txt. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Nothing here depends on cmath or the per-category primitive modules (sin, exp, ...), so this module remains importable with or without nimNativeIo.
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
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): stringconst 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).func sgn(x: T): int64Sign function.
Returns:
-1for negative numbers andNegInf,1for positive numbers andInf,0for positive zero, negative zero andNaN
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 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 fac(n: int64): int64func binom(n: 0..9223372036854775807; k: 0..9223372036854775807): 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