Math-Module
Sun 29 June 2025
#Number Basics
import math
print(math.ceil(4.3))
5
print(math.floor(4.8))
4
print(math.floor(-4.2))
-5
print(math.fabs(-3.7))
3.7
print(math.fabs(3.7))
3.7
print(math.trunc(3.9))
3
print(math.trunc(-3.9))
-3
print(math.copysign(10, -1))
-10.0
print(math.copysign(0, -5))
-0.0
print(math.fmod(20, 3))
2.0
print(math.fmod(7.5, 2.0))
1.5
print(math.isclose(0.1 + 0.2, 0.3))
True
print(math.isclose(0.1 + 0.2, 0.3001)) # False
False
print(math.remainder(7, 3))
1.0
print(math.remainder(-7, 3)) # -1.0
-1.0
print(math.modf(2.75)) # (0.75, 2.0)
(0.75, 2.0)
print(math.isfinite(10)) # True
True
print(math.isfinite(math.inf))
False
print(math.isnan(math.nan))
True
print(math.isnan(5.0))
False
print(math.isinf(math.inf))
True
print(math.isinf(100))
False
print(math.frexp(8))
(0.5, 4)
print(math.frexp(10.5))
(0.65625, 4)
print(math.ldexp(0.5, 3))
4.0
print(math.ldexp(0.65625, 4))
10.5
print(math.copysign(0.0, -0.0))
-0.0
print(math.modf(0))
(0.0, 0.0)
#Power, Roots, and Logarithms
print(math.pow(2, 3))
8.0
print(math.pow(4.5, 2))
20.25
print(math.sqrt(16))
4.0
print(math.sqrt(2.25))
1.5
print(math.isqrt(10))
3
print(math.isqrt(25))
5
print(math.exp(1))
2.718281828459045
print(math.exp(-1))
0.36787944117144233
print(math.expm1(0.01))
0.010050167084168058
print(math.log(10))
2.302585092994046
print(math.log10(1000))
3.0
print(math.log2(32))
5.0
print(math.log1p(1e-5))
9.99995000033333e-06
print(math.log(25, 5))
2.0
print(math.pow(9, 0.5))
3.0
print(math.sqrt(0))
0.0
print(math.isqrt(0))
0
print(math.log1p(0))
0.0
print(math.exp(0))
1.0
print(math.expm1(0))
0.0
print(math.log(1))
0.0
print(math.log2(1))
0.0
print(math.log10(1))
0.0
print(math.pow(2, -2))
0.25
print(math.log(math.e))
1.0
print(round(math.exp(5), 2))
148.41
print(math.pow(10, 6))
1000000.0
print(math.sqrt(1000000))
1000.0
try:
print(math.log(-1)) # Error
except ValueError:
print("Math domain error")
Math domain error
try:
print(math.sqrt(-9)) # Error
except ValueError:
print("Math domain error")
Math domain error
#Trigonometry Functions
print(math.sin(0))
0.0
print(math.sin(math.pi / 2))
1.0
print(math.sin(math.pi))
1.2246467991473532e-16
print(math.cos(0))
1.0
print(math.cos(math.pi))
-1.0
print(math.tan(0))
0.0
print(math.tan(math.pi / 4))
0.9999999999999999
try:
print(math.tan(math.pi / 2)) # very large
except:
print("Undefined")
1.633123935319537e+16
print(math.asin(1))
1.5707963267948966
print(math.asin(0))
0.0
print(math.acos(1))
0.0
print(math.acos(0))
1.5707963267948966
print(math.atan(1))
0.7853981633974483
print(math.atan(0))
0.0
print(math.atan2(1, 1))
0.7853981633974483
print(math.atan2(-1, -1))
-2.356194490192345
print(math.sin(-math.pi / 2))
-1.0
print(math.cos(-math.pi))
-1.0
print(math.tan(-math.pi / 4))
-0.9999999999999999
try:
print(math.asin(2))
except ValueError:
print("Invalid domain")
Invalid domain
try:
print(math.acos(-2))
except ValueError:
print("Invalid domain")
Invalid domain
print(math.atan(-1))
-0.7853981633974483
print(math.sin(math.radians(30)))
0.49999999999999994
print(math.cos(math.radians(60)))
0.5000000000000001
print(math.tan(math.radians(45)))
0.9999999999999999
x = math.pi / 6
print(math.asin(math.sin(x)))
0.5235987755982988
print(math.acos(math.cos(x)))
0.5235987755982987
print(math.atan(math.tan(x)))
0.5235987755982988
print(math.atan2(0, 0))
0.0
print(math.atan2(1, -1))
2.356194490192345
#Angle Conversions & Constants
print(math.degrees(math.pi))
180.0
print(math.degrees(math.pi / 2))
90.0
print(math.degrees(2 * math.pi))
360.0
print(math.radians(180))
3.141592653589793
print(math.radians(90))
1.5707963267948966
print(math.radians(360))
6.283185307179586
print(math.hypot(3, 4))
5.0
print(math.hypot(0, 5))
5.0
print(math.hypot(-3, 4))
5.0
print(math.isclose(1.0001, 1.0002, rel_tol=1e-3))
True
print(math.pi)
3.141592653589793
print(math.e)
2.718281828459045
print(math.tau)
6.283185307179586
print(math.inf)
inf
print(math.nan)
nan
print(math.isfinite(math.nan))
False
print(math.isinf(-math.inf))
True
print(math.isnan(0))
False
r = 3
print(math.pi * r**2)
28.274333882308138
print(math.tau * r)
18.84955592153876
deg = 60
rad = math.radians(deg)
print(math.degrees(rad))
59.99999999999999
print(math.hypot(3, 4, 12))
13.0
print(math.atan2(1, 0))
1.5707963267948966
print(math.atan2(-1, 0))
-1.5707963267948966
print(math.tau / 2)
3.141592653589793
r = 5
print((math.pi * r ** 2) / 2)
39.269908169872416
print(math.isclose(1.001, 1.002, abs_tol=0.01))
True
print(math.e ** 2)
7.3890560989306495
print(math.log(math.e))
1.0
print(math.radians(0))
0.0
print(math.factorial(5))
120
print(math.factorial(0))
1
print(math.factorial(1))
1
print(math.comb(5, 2))
10
print(math.comb(4, 4))
1
print(math.comb(6, 0))
1
print(math.perm(5, 2))
20
print(math.perm(4, 4))
24
print(math.perm(7, 0))
1
print(math.gamma(5))
24.0
print(math.gamma(2.5))
1.3293403881791372
print(math.lgamma(5))
3.178053830347945
print(math.lgamma(1))
0.0
print(math.gamma(0.5))
1.7724538509055159
print(math.lgamma(0.5))
0.5723649429247004
print(math.factorial(10))
3628800
print(math.comb(10, 3))
120
print(math.perm(10, 3))
720
print(math.gamma(1))
1.0
print(math.gamma(2))
1.0
print(math.gamma(3))
2.0
print(math.gamma(4))
6.0
print(math.lgamma(2))
0.0
print(math.comb(20, 10))
184756
print(math.perm(8, 5))
6720
print(round(math.gamma(4.5), 2))
11.63
print(round(math.lgamma(4.5), 2))
2.45
try:
print(math.factorial(-5))
except ValueError:
print("Negative factorial error")
Negative factorial error
try:
print(math.comb(4, 5))
except ValueError:
print("k > n error")
0
try:
print(math.perm(4, 5))
except ValueError:
print("k > n error")
0
#Advanced, Nested, and Composed Usage
x = math.pi / 4
print(round(math.sin(x)**2 + math.cos(x)**2, 5)) # 1.0
1.0
a, b, c = 1, -3, 2
root1 = (-b + math.sqrt(b**2 - 4*a*c)) / (2*a)
print(root1)
2.0
v = (3, 4)
print(math.hypot(*v)) # 5.0
5.0
deg = 75
print(math.degrees(math.radians(deg))) # 75.0
75.0
dx = 4
dy = 3
print(math.degrees(math.atan2(dy, dx))) # 36.87...
36.86989764584402
x = 1.5
print(1 / (1 + math.exp(-x)))
0.8175744761936437
a, b = 100, 10
print(math.log(a) / math.log(b)) # log base 10
2.0
a, b, c = 5, 5, 6
s = (a + b + c) / 2
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
print(area)
12.0
A0, k, t = 100, 0.1, 5
print(A0 * math.exp(-k * t))
60.653065971263345
P, r, n, t = 1000, 0.05, 12, 10
print(P * math.pow(1 + r/n, n*t))
1647.00949769028
print(math.sin(math.radians(30))) # 0.5
0.49999999999999994
print(math.atan2(-3, -3))
-2.356194490192345
print(math.hypot(3, 4, 12))
13.0
angle = -3
print((angle + 2 * math.pi) % (2 * math.pi))
3.2831853071795862
d = 30
r = math.radians(d)
s = math.sin(r)
print(s)
0.49999999999999994
x = math.pi / 3
print((math.sin(x) + math.cos(x)) / 2)
0.6830127018922194
decimal, whole = math.modf(3.75)
print(whole, decimal)
3.0 0.75
x = 1e-10
print(math.log1p(x))
9.999999999500001e-11
print(math.expm1(x))
1.00000000005e-10
a, b = 4, 6
print(2 * a * b / (a + b))
4.8
a, b = 4, 9
print(math.sqrt(a * b))
6.0
r = 10
theta = math.radians(60)
print(r * theta)
10.471975511965976
print(0.5 * r**2 * theta)
52.35987755982988
print(math.sin(math.tau)) # ~0
print(math.cos(math.tau)) # ~1
-2.4492935982947064e-16
1.0
a, b = 10, 0
try:
result = a / b
print(math.isfinite(result))
except ZeroDivisionError:
print("Divide by zero!")
Divide by zero!
x, min_x, max_x = 7, 5, 15
print((x - min_x) / (max_x - min_x))
0.2
print(((x - min_x) / (max_x - min_x)) * 100)
20.0
r, theta = 5, math.radians(60)
x = r * math.cos(theta)
y = r * math.sin(theta)
print(x, y)
2.5000000000000004 4.330127018922193
r = math.hypot(x, y)
theta = math.degrees(math.atan2(y, x))
print(r, theta)
5.0 59.99999999999999
a, b, angle = 3, 4, math.radians(90)
c = math.sqrt(a**2 + b**2 - 2*a*b*math.cos(angle))
print(c) # Should be 5
5.0
Score: 185
Category: basics