GitHub

API reference for big_int.

Integer functions Effect's BigInt module has and Python's int and math lack.

The rest of BigInt is Python's own int (docs/PARITY.md section 4): math.isqrt is sqrtUnsafe, math.gcd is gcd, and so on. nth_root is math.isqrt for any degree::

big_int.nth_root(30, 3)    # 3
big_int.nth_root(-27, 3)   # -3
big_int.nth_root(-16, 4)   # None

Functions

nth_root

nth_root(n, degree) -> int | None

The integer degree-th root of n, rounded toward zero, or None when n is negative and degree even (Effect's BigInt.nthRoot). degree must be a positive safe integer, else ValueError.

ParameterTypeDescription
nint
degreeint

Returns int | None

def nth_root(n: int, degree: int) -> int | None:
    """The integer ``degree``-th root of ``n``, rounded toward zero, or ``None`` when ``n`` is
    negative and ``degree`` even (Effect's ``BigInt.nthRoot``). ``degree`` must be a positive
    safe integer, else ``ValueError``."""
    if type(degree) is not int or not 1 <= degree <= _MAX_SAFE:
        raise ValueError(f"Degree must be a positive safe integer, got {js_number(degree)}")
    if n >= 0:
        return _root_floor(n, degree)
    return None if degree % 2 == 0 else -_root_floor(-n, degree)

nth_root_unsafe

nth_root_unsafe(n, degree) -> int

nth_root; raises ValueError when n is negative and degree even.

ParameterTypeDescription
nint
degreeint

Returns int

def nth_root_unsafe(n: int, degree: int) -> int:
    """``nth_root``; raises ``ValueError`` when ``n`` is negative and ``degree`` even."""
    root = nth_root(n, degree)
    if root is None:
        raise ValueError(f"Cannot take an integer root of degree {degree} of {n}")
    return root