The math module implements many of the IEEE functions that would normally be found in the native platform C libraries for complex mathematical operations using floating point values, including logarithms and trigonometric operations.
In [1]:
import math
print('pi', math.pi)
print('e', math.e)
print('nan', math.nan)
print('inf', math.inf)
In [2]:
import math
print('{:^3} {:6} {:6} {:6}'.format(
'e', 'x', 'x**2', 'isinf'))
print('{:-^3} {:-^6} {:-^6} {:-^6}'.format(
'', '', '', ''))
for e in range(0, 201, 20):
x = 10.0 ** e
y = x * x
print('{:3d} {:<6g} {:<6g} {!s:6}'.format(
e, x, y, math.isinf(y),
))
In [3]:
x = 10.0 ** 200
print('x=',x)
print('x*x=', x*x)
print('x**2=', end='')
try:
print(x**2)
except OverflowError as err:
print(err)
In [5]:
import math
INPUTS = [
(1000, 900, 0.1),
(100, 90, 0.1),
(10, 9, 0.1),
(1, 0.9, 0.1),
(0.1, 0.09, 0.1),
]
print('{:^8} {:^8} {:^8} {:^8} {:^8} {:^8}'.format(
'a', 'b', 'rel_tol', 'abs(a-b)', 'tolerance', 'close')
)
print('{:-^8} {:-^8} {:-^8} {:-^8} {:-^8} {:-^8}'.format(
'-', '-', '-', '-', '-', '-'),
)
fmt = '{:8.2f} {:8.2f} {:8.2f} {:8.2f} {:8.2f} {!s:>8}'
for a, b, rel_tol in INPUTS:
close = math.isclose(a, b, rel_tol=rel_tol)
tolerance = rel_tol * max(abs(a), abs(b))
abs_diff = abs(a - b)
print(fmt.format(a, b, rel_tol, abs_diff, tolerance, close))
In [6]:
import math
INPUTS = [
(1.0, 1.0 + 1e-07, 1e-08),
(1.0, 1.0 + 1e-08, 1e-08),
(1.0, 1.0 + 1e-09, 1e-08),
]
print('{:^8} {:^11} {:^8} {:^10} {:^8}'.format(
'a', 'b', 'abs_tol', 'abs(a-b)', 'close')
)
print('{:-^8} {:-^11} {:-^8} {:-^10} {:-^8}'.format(
'-', '-', '-', '-', '-'),
)
for a, b, abs_tol in INPUTS:
close = math.isclose(a, b, abs_tol=abs_tol)
abs_diff = abs(a - b)
print('{:8.2f} {:11} {:8} {:0.9f} {!s:>8}'.format(
a, b, abs_tol, abs_diff, close))
In [7]:
import math
HEADINGS = ('i', 'int', 'trunk', 'floor', 'ceil')
print('{:^5} {:^5} {:^5} {:^5} {:^5}'.format(*HEADINGS))
print('{:-^5} {:-^5} {:-^5} {:-^5} {:-^5}'.format(
'', '', '', '', '',
))
fmt = '{:5.1f} {:5.1f} {:5.1f} {:5.1f} {:5.1f}'
TEST_VALUES = [
-1.5,
-0.8,
-0.5,
-0.2,
0,
0.2,
0.5,
0.8,
1,
]
for i in TEST_VALUES:
print(fmt.format(
i,
int(i),
math.trunc(i),
math.floor(i),
math.ceil(i),
))
In [8]:
import math
values = [0.1] * 10
print('Input values:', values)
print('sum() : {:.20f}'.format(sum(values)))
s = 0.0
for i in values:
s += i
print('for-loop : {:.20f}'.format(s))
print('math.fsum() : {:.20f}'.format(math.fsum(values)))
In [9]:
import math
print('{:^7} {:^7} {:^7}'.format(
'Degrees', 'Radians', 'Expected'))
print('{:-^7} {:-^7} {:-^7}'.format(
'', '', ''))
INPUTS = [
(0, 0),
(30, math.pi / 6),
(45, math.pi / 4),
(60, math.pi / 3),
(90, math.pi / 2),
(180, math.pi),
(270, 3 / 2.0 * math.pi),
(360, 2 * math.pi),
]
for deg, expected in INPUTS:
print('{:7d} {:7.2f} {:7.2f}'.format(
deg,
math.radians(deg),
expected,
))
In [10]:
import math
INPUTS = [
(0, 0),
(math.pi / 6, 30),
(math.pi / 4, 45),
(math.pi / 3, 60),
(math.pi / 2, 90),
(math.pi, 180),
(3 * math.pi / 2, 270),
(2 * math.pi, 360),
]
print('{:^8} {:^8} {:^8}'.format(
'Radians', 'Degrees', 'Expected'))
print('{:-^8} {:-^8} {:-^8}'.format('', '', ''))
for rad, expected in INPUTS:
print('{:8.2f} {:8.2f} {:8.2f}'.format(
rad,
math.degrees(rad),
expected,
))