src/pylib/Lib/n_math

Search:
Group by:
Source   Edit  

Nim's Python-like math, but raises no catchable exception, using errno to report math error. a.k.a. non will be raised (but not for Defects)

which allows fast code.

For Python-like error handler (exception-based), see Lib/math

Also this is not just a module that wraps Nim's std/math,

but also providing some extensions that Nim's std/math lacks, for example:

And fix some non-C99 behavior on some systems, e.g. log(-ve) -> -Inf on Solaris

NOTE: Currently int is not acceptable when it comes to float about functions

Consts

e = 2.718281828459045
Source   Edit  
inf = 0x7FF0000000000000'f64
Source   Edit  
nan = 0x7FF7FFFFFFFFFFFF'f64
Source   Edit  
pi = 3.141592653589793
Source   Edit  
tau = 6.283185307179586
Source   Edit  

Procs

func acos[F: SomeFloat](x: F): F
Source   Edit  
func acosh[F: SomeFloat](x: F): F
Source   Edit  
func asin[F: SomeFloat](x: F): F
Source   Edit  
func asinh[F: SomeFloat](x: F): F
Source   Edit  
func atan[F: SomeFloat](x: F): F
Source   Edit  
func atan2[F: SomeFloat](x: F): F
Source   Edit  
func atanh[F: SomeFloat](x: F): F
Source   Edit  
func ceil(x: SomeFloat): int
Hint: unlike C/Nim's returning float, Python's returns int and raises errors for nans and infinities
Source   Edit  
func comb(n, k: int): int {....raises: [], tags: [], forbids: [].}
Hint: Python's math.comb does not accept negative value for n, k but Nim's std/math.binom allows, so this function allows too. For consistent behavior with Python, see Lib/math.comb
Source   Edit  
func degrees[F: SomeFloat](x: F): F
Source   Edit  
func expm1[F: SomeFloat](x: F): F
Source   Edit  
func fabs[F: SomeFloat](x: F): F
Source   Edit  
func factorial(x: Natural): int {....raises: [], tags: [], forbids: [].}
Source   Edit  
func floor(x: SomeFloat): int
Hint: unlike C/Nim's returning float, Python's returns int and raises errors for nans and infinities
Source   Edit  
func fmod[F: SomeFloat](x: F; y: F): F
Source   Edit  
func fsum[T: SomeFloat](iterable: Iterable[T]): T
Source   Edit  
func gamma[F: SomeFloat](x: F): F
Source   Edit  
func gcd[I: SomeInteger](x, y, z: I; args: varargs[I]): I
Source   Edit  
func gcd[I: SomeInteger](x, y: I): I
Source   Edit  
func hypot[I: SomeFloat](x, y, z: I; args: varargs[I]): I
Source   Edit  
func hypot[I: SomeFloat](x, y: I): I
Source   Edit  
func isclose(a, b: SomeFloat; rel_tol = 1e-9; abs_tol = 0.0): bool
Source   Edit  
func isqrt(n: Natural): int {....raises: [], tags: [], forbids: [].}

Example:

assert 2 == isqrt 5
Source   Edit  
func isqrt[T: SomeFloat](x: T): int {....raises: [].}
Hint: assuming x > 0 (raise RangeDefect otherwise (if not danger build))

use math.isqrt if expecting raising ValueError

Source   Edit  
func isqrtPositive(n: Positive): int {.inline, ...raises: [], tags: [], forbids: [].}
EXT: isqrt for Positive only, as we all know, in Python:
  • isqrt(0) == 0
  • isqrt(-int)
Source   Edit  
func lcm[I: SomeInteger](x, y, z: I; args: varargs[I]): I
Source   Edit  
func lcm[I: SomeInteger](x, y: I): I
Source   Edit  
func ldexp[F: SomeFloat](x: F; i: int): F {....raises: [].}
Hint: only set errno
Source   Edit  
func ldexp[F: SomeFloat](x: F; i: int; exc: var ref Exception): F {....raises: [].}

set exception to exc instead of raising it

exc:

  • set to nil if no error
  • set to suitable exc if some error occurs
Source   Edit  
func lgamma[F: SomeFloat](x: F): F
Source   Edit  
func log[F: SomeFloat](x, base: F): F
Source   Edit  
func log[F: SomeFloat](x: F): F
Source   Edit  
func log1p[F: SomeFloat](x: F): F
Source   Edit  
func log2[F: SomeFloat](x: F): F
Source   Edit  
func log10[F: SomeFloat](x: F): F
Source   Edit  
func math_is_error(x: SomeFloat; exc: var ref Exception): bool

inner usage (used by Lib/math).

Call this when errno != 0, and where x is the result libm returned. This will usually set up an exception and return true, but may return false without setting up an exception.

Source   Edit  
func modf[F: SomeFloat](x: F): tuple[intpart: F, floatpart: F]
Source   Edit  
func perm(n, k: Natural): int {....raises: [], tags: [], forbids: [].}
Source   Edit  
func perm(n: int): int {....raises: [], tags: [], forbids: [].}
equal to perm(n,n), returns n! Source   Edit  
func pow[F: SomeFloat](x, y: F): F
Source   Edit  
func prod[T](iterable: Iterable[T]; start = 1.T): T
Source   Edit  
func radians[F: SomeFloat](x: F): F
Source   Edit  
func remainder[F: SomeFloat](x: F; y: F): F
Source   Edit  
func trunc(x: SomeFloat): int
Hint: unlike C/Nim's returning float, Python's returns int and raises errors for nans and infinities
Source   Edit  

Templates

template py_math_isclose_impl(abs)

inner use. Implementation of isclose.

mixin a, b, isinf, rel_tol, abs_tol

Source   Edit