Math Module
Import using: use math
Constants
math.PI- Ratio of a circle's circumference to its diameter (~3.14159).math.E- Euler's constant (~2.71828).math.TAU- Equal to 2 * PI (~6.28318).
use math
print(str(math.PI)) # 3.141592653589793
print(str(math.E)) # 2.718281828459045
print(str(math.TAU)) # 6.283185307179586
Trigonometry
Angle parameters are in radians:
math.sin(x),math.cos(x),math.tan(x)- Sine, cosine, tangentmath.asin(x),math.acos(x),math.atan(x),math.atan2(y, x)- Inverse trigmath.sinh(x),math.cosh(x),math.tanh(x)- Hyperbolic trigmath.deg(rad)- Radians to degreesmath.rad(deg)- Degrees to radians
use math
print(str(math.sin(math.PI / 2))) # 1.0
print(str(math.cos(math.PI))) # -1.0
print(str(math.tan(0))) # 0.0
print(str(math.deg(math.PI))) # 180.0
print(str(math.rad(180))) # 3.141592653589793
Exponentiation & Logs
math.sqrt(x)- Square rootmath.cbrt(x)- Cube rootmath.pow(x, y)- x raised to power ymath.exp(x)- e^xmath.log(x)- Natural logarithmmath.log10(x)- Base-10 logarithmmath.log2(x)- Base-2 logarithm
use math
print(str(math.sqrt(25))) # 5.0
print(str(math.cbrt(27))) # 3.0
print(str(math.pow(2, 10))) # 1024.0
print(str(math.exp(1))) # 2.718281828459045
print(str(math.log(math.E))) # 1.0
print(str(math.log10(100))) # 2.0
print(str(math.log2(8))) # 3.0
Rounding & Absolute
math.ceil(x)- Round up to nearest integermath.floor(x)- Round down to nearest integermath.round(x)- Round half-up to nearest integermath.trunc(x)- Truncate fractional partmath.abs(x)- Absolute value
use math
print(str(math.ceil(3.2))) # 4.0
print(str(math.floor(3.8))) # 3.0
print(str(math.round(3.5))) # 4.0
print(str(math.trunc(3.9))) # 3.0
print(str(math.abs(-5))) # 5.0
Comparison & Statistics
math.min(a, b)- Smaller of two numbersmath.max(a, b)- Larger of two numbersmath.clamp(val, min, max)- Clamp to inclusive rangemath.sum(arr)- Arithmetic sum of arraymath.mean(arr)- Arithmetic mean (Null if empty)math.gcd(a, b)- Greatest Common Divisormath.lcm(a, b)- Least Common Multiplemath.factorial(n)- Factorial of non-negative integer
use math
print(str(math.min(3, 7))) # 3.0
print(str(math.max(3, 7))) # 7.0
print(str(math.clamp(15, 0, 10))) # 10.0
print(str(math.sum([1, 2, 3]))) # 6.0
print(str(math.mean([1, 2, 3]))) # 2.0
print(str(math.gcd(12, 8))) # 4
print(str(math.lcm(4, 6))) # 12
print(str(math.factorial(5))) # 120