[Python] math: inf, prod, floor(), ceil(), isqrt(), pow()…

math Module

math.inf ∞, ≡ float(“inf”)
math.prod(iterable, *, start=1)

def prod(iterable, *, start=1):
  for element in iterable:
    start *= element
  return start

math.floor(f) -> int  
math.ceil(f) -> int  
math.sqrt(x) -> float

≡ x ** (1/2), if x >= 0

  • math.sqrt(x) is significantly faster than x ** 0.5.
  • About processing negative numbers:

    That means sqrt() only works on positive values.

    But the ** operator is able to return a complex number, 
    note the weird rounding error where the real part should be zero
    (ex: (-4) ** 0.5 ⇒ (1.2246467991473532e-16+2j)).

    cmath.sqrt() returns the perfect complex value probably
    (ex: cmath.sqrt(-4) ⇒ 2j), 
    because as opposed to **sqrt()
    is a specialized square root computation, 
    not just a float pow method.

math.isqrt(x: int) -> int ≡ floor(x ** (1/2))
math.pow(x, y[, z])

≡ (x ** (y)) [% z]
math.pow(x, y, z) is quite faster than (x ** y) % z

Last Updated on 2024/12/12 by A1go

目錄

目錄
Bitnami