math
Mathematical functions and constants.
Available Functions
| Function | Description |
|---|---|
sqrt(x) |
Returns the square root of x |
pow(base, exp) |
Returns base raised to the power of exp |
fabs(x) |
Returns the absolute value of x as a float |
floor(x) |
Rounds x down to the nearest integer |
ceil(x) |
Rounds x up to the nearest integer |
trunc(x) |
Truncates x to the nearest integer toward zero |
sin(x) |
Returns the sine of x (in radians) |
cos(x) |
Returns the cosine of x (in radians) |
tan(x) |
Returns the tangent of x (in radians) |
asin(x) |
Returns the arc sine of x (in radians) |
acos(x) |
Returns the arc cosine of x (in radians) |
atan(x) |
Returns the arc tangent of x (in radians) |
atan2(y, x) |
Returns the arc tangent of y/x (in radians) |
log(x) |
Returns the natural logarithm of x |
log10(x) |
Returns the base-10 logarithm of x |
log2(x) |
Returns the base-2 logarithm of x |
exp(x) |
Returns e raised to the power of x |
degrees(x) |
Converts radians to degrees |
radians(x) |
Converts degrees to radians |
hypot(x, y) |
Returns the Euclidean distance sqrt(xx + yy) |
fmod(x, y) |
Returns the floating-point remainder of x/y |
gcd(a, b) |
Returns the greatest common divisor |
factorial(n) |
Returns the factorial of n |
copysign(x, y) |
Returns x with the sign of y |
isnan(x) |
Returns true if x is NaN (Not a Number) |
isinf(x) |
Returns true if x is positive or negative infinity |
isfinite(x) |
Returns true if x is neither NaN nor infinite |
Constants
| Constant | Description |
|---|---|
pi |
The mathematical constant π |
e |
The mathematical constant e |
inf |
Positive infinity |
nan |
NaN (Not a Number) |
Functions
math.sqrt(x)
Returns the square root of x.
Parameters:
x: Number (integer or float)
Returns: Float
Example:
import math
result = math.sqrt(16) # 4.0math.pow(base, exp)
Returns base raised to the power of exp (base^exp).
Parameters:
base: Base numberexp: Exponent
Returns: Float
Example:
import math
result = math.pow(2, 8) # 256.0math.fabs(x)
Returns the absolute value of x as a float.
Parameters:
x: Number (integer or float)
Returns: Float (always returns floating-point)
Example:
import math
result = math.fabs(-5) # 5.0
result = math.fabs(-3.14) # 3.14Note: For absolute value that preserves integer type, use the builtin
abs()function instead.
math.floor(x)
Rounds x down to the nearest integer.
Parameters:
x: Number
Returns: Integer
Example:
import math
result = math.floor(3.7) # 3math.ceil(x)
Rounds x up to the nearest integer.
Parameters:
x: Number
Returns: Integer
Example:
import math
result = math.ceil(3.2) # 4Note: For rounding to nearest integer, use the builtin
round()function. For min/max values, use the builtinmin()andmax()functions.
math.sin(x)
Returns the sine of x (in radians).
Parameters:
x: Angle in radians
Returns: Float
Example:
import math
result = math.sin(0) # 0.0
result = math.sin(math.pi / 2) # 1.0math.cos(x)
Returns the cosine of x (in radians).
Parameters:
x: Angle in radians
Returns: Float
Example:
import math
result = math.cos(0) # 1.0
result = math.cos(math.pi) # -1.0math.tan(x)
Returns the tangent of x (in radians).
Parameters:
x: Angle in radians
Returns: Float
Example:
import math
result = math.tan(0) # 0.0
result = math.tan(math.pi / 4) # 1.0math.log(x)
Returns the natural logarithm (base e) of x.
Parameters:
x: Number (must be > 0)
Returns: Float
Example:
import math
result = math.log(1) # 0.0
result = math.log(math.e) # 1.0math.exp(x)
Returns e raised to the power of x (e^x).
Parameters:
x: Number
Returns: Float
Example:
import math
result = math.exp(0) # 1.0
result = math.exp(1) # 2.718281828459045math.degrees(x)
Converts angle x from radians to degrees.
Parameters:
x: Angle in radians
Returns: Float
Example:
import math
result = math.degrees(math.pi) # 180.0
result = math.degrees(math.pi / 2) # 90.0math.radians(x)
Converts angle x from degrees to radians.
Parameters:
x: Angle in degrees
Returns: Float
Example:
import math
result = math.radians(180) # 3.141592653589793
result = math.radians(90) # 1.5707963267948966math.fmod(x, y)
Returns the floating-point remainder of x divided by y.
Parameters:
x: Dividendy: Divisor (cannot be 0)
Returns: Float
Example:
import math
result = math.fmod(5.5, 2.0) # 1.5
result = math.fmod(7.0, 3.0) # 1.0math.gcd(a, b)
Returns the greatest common divisor of integers a and b.
Parameters:
a: Integerb: Integer
Returns: Integer
Example:
import math
result = math.gcd(48, 18) # 6
result = math.gcd(100, 75) # 25math.factorial(n)
Returns the factorial of n (n!).
Parameters:
n: Non-negative integer (0 ≤ n ≤ 20)
Returns: Integer
Example:
import math
result = math.factorial(5) # 120
result = math.factorial(0) # 1math.trunc(x)
Truncates x to the nearest integer toward zero.
Parameters:
x: Number (integer or float)
Returns: Integer
Example:
import math
result = math.trunc(3.7) # 3
result = math.trunc(-3.7) # -3math.asin(x)
Returns the arc sine of x in radians.
Parameters:
x: Number in range [-1, 1]
Returns: Float
Example:
import math
result = math.asin(0) # 0.0
result = math.asin(1) # 1.5707963267948966 (pi/2)math.acos(x)
Returns the arc cosine of x in radians.
Parameters:
x: Number in range [-1, 1]
Returns: Float
Example:
import math
result = math.acos(1) # 0.0
result = math.acos(0) # 1.5707963267948966 (pi/2)math.atan(x)
Returns the arc tangent of x in radians.
Parameters:
x: Number
Returns: Float in range [-pi/2, pi/2]
Example:
import math
result = math.atan(0) # 0.0
result = math.atan(1) # 0.7853981633974483 (pi/4)math.atan2(y, x)
Returns the arc tangent of y/x in radians, correctly handling the quadrant.
Parameters:
y: Y coordinatex: X coordinate
Returns: Float in range [-pi, pi]
Example:
import math
result = math.atan2(1, 1) # 0.7853981633974483 (pi/4)
result = math.atan2(-1, 1) # -0.7853981633974483math.log10(x)
Returns the base-10 logarithm of x.
Parameters:
x: Positive number
Returns: Float
Example:
import math
result = math.log10(100) # 2.0
result = math.log10(1000) # 3.0math.log2(x)
Returns the base-2 logarithm of x.
Parameters:
x: Positive number
Returns: Float
Example:
import math
result = math.log2(8) # 3.0
result = math.log2(16) # 4.0math.hypot(x, y)
Returns the Euclidean distance sqrt(xx + yy).
Parameters:
x: First coordinatey: Second coordinate
Returns: Float
Example:
import math
result = math.hypot(3, 4) # 5.0
result = math.hypot(5, 12) # 13.0math.copysign(x, y)
Returns x with the sign of y.
Parameters:
x: Magnitude valuey: Sign value
Returns: Float with magnitude of x and sign of y
Example:
import math
result = math.copysign(5, -1) # -5.0
result = math.copysign(-5, 1) # 5.0math.isnan(x)
Returns true if x is NaN (Not a Number).
Parameters:
x: Number to check
Returns: Boolean
Example:
import math
result = math.isnan(math.nan) # True
result = math.isnan(5) # Falsemath.isinf(x)
Returns true if x is positive or negative infinity.
Parameters:
x: Number to check
Returns: Boolean
Example:
import math
result = math.isinf(math.inf) # True
result = math.isinf(-math.inf) # True
result = math.isinf(5) # Falsemath.isfinite(x)
Returns true if x is neither NaN nor infinite.
Parameters:
x: Number to check
Returns: Boolean
Example:
import math
result = math.isfinite(5) # True
result = math.isfinite(math.inf) # False
result = math.isfinite(math.nan) # FalseConstants
math.pi
The mathematical constant π (pi).
Value: Float (3.141592653589793)
Example:
import math
pi = math.pi # 3.141592653589793math.e
The mathematical constant e (Euler’s number).
Value: Float (2.718281828459045)
Example:
import math
e = math.e # 2.718281828459045math.inf
Positive infinity.
Value: Float (infinity)
Example:
import math
result = math.inf # inf
result = math.isinf(math.inf) # Truemath.nan
NaN (Not a Number).
Value: Float (NaN)
Example:
import math
result = math.nan # nan
result = math.isnan(math.nan) # TrueUsage Example
import math
# Basic operations
result = math.sqrt(16) # 4.0
power = math.pow(2, 8) # 256.0
absolute = math.fabs(-5) # 5.0 (float)
# For integer-preserving abs, use builtin:
int_abs = abs(-5) # 5
# Rounding
floor_val = math.floor(3.7) # 3
ceil_val = math.ceil(3.2) # 4
round_val = round(3.5) # 4 (use builtin round)
# Min/Max (use builtins)
minimum = min(3, 1, 4, 1, 5) # 1
maximum = max(3, 1, 4, 1, 5) # 5
# Trigonometric functions
sin_val = math.sin(0) # 0.0
cos_val = math.cos(0) # 1.0
tan_val = math.tan(0) # 0.0
# Logarithmic and exponential
log_val = math.log(1) # 0.0
exp_val = math.exp(1) # 2.718281828459045
# Angle conversion
degrees_val = math.degrees(math.pi) # 180.0
radians_val = math.radians(180) # 3.141592653589793
# Modular arithmetic
mod_val = math.fmod(5.5, 2.0) # 1.5
gcd_val = math.gcd(48, 18) # 6
fact_val = math.factorial(5) # 120
# Constants
pi = math.pi # 3.141592653589793
e = math.e # 2.718281828459045
# Calculate circle area
radius = 5
area = math.pi * math.pow(radius, 2)
print("Area: " + str(area)) # Area: 78.53981633974483
# Calculate hypotenuse using Pythagoras
a = 3
b = 4
hypotenuse = math.sqrt(math.pow(a, 2) + math.pow(b, 2))
print("Hypotenuse: " + str(hypotenuse)) # Hypotenuse: 5.0