Pure Programmer

# Math Library

While the arithmetic and logic operators represent the basic operations that our [[CPU]] can perform, there are many other common math functions that com in handy. Since they are so common, programming languages usually have a math library that provides these functions. Logarithms, trigonometry and random number generation are just of few of the types of functions typically provided.

## Math Constants

Math constants provide commonly used mathematical constanst to the highest precesion available. Some of the more useful math constants are summarized below.

Python Math Constants
Constant Description
math.eEuler's constant [[ℯ]], base of the natural logarithm
math.pi[[Pi]], Ratio of a circle's circumference to its diameter

## Math Functions

These most useful math functions are summarized below.

Python Math Functions
Function Description
abs(x)[[Absolute value]] of x
math.acos(x)[[Arc cosine]] of x, result is in the range [0,π] [[Radians]]
math.asin(x)[[Arc sine]] of x, result is in the range [-π/2,π/2] [[Radians]]
math.atan(x)[[Arc tangent]] of x, result is in the range [-π/2,π/2] [[Radians]]
math.atan2(y,x)Angle θ from the conversion of [[rectangular coordinates]] (x,y),
result is in the range [-π/2,π/2] [[Radians]]
math.ceil(x)Smallest integer value greater than x
math.exp(x)[[ℯ]] rasied to the power x, i.e. ℯx
math.floor(x)Largest integer less than x
math.log(x)[[Natural logarithm]] of x
max(x,y)Larger of x and y
min(x,y)Smaller of x and y
math.pow(x,y)x raised to the power y, i.e. xy
random.random()[[Pseudorandom]] number on the interval [0,1)
round(x)Nearest integer to x
math.sqrt(x)[[Square root]] of x

Be sure to add the following import statements to the top of your program in order to use these math functions and constants.
```from Utils import * import math import random ```

Math1.py
```#!/usr/bin/env python3;
###############################################################################
# This program demonstrates the math library.
#
###############################################################################

import Utils
import math
import random

# Begin Main
a = math.pi / 6
b = math.pi / 4
c = -a * 2
d = -b * 2
e = math.e

print("pi = {0:f}".format(math.pi))
print("e = {0:f}".format(math.e))

# abs, floor, ceil, round, trunc, min, max
print("abs({0:f}) = {1:f}".format(a, abs(a)))
print("abs({0:f}) = {1:f}".format(c, abs(c)))
print("floor({0:f}) = {1:f}".format(a, math.floor(a)))
print("floor({0:f}) = {1:f}".format(c, math.floor(c)))
print("ceil({0:f}) = {1:f}".format(a, math.ceil(a)))
print("ceil({0:f}) = {1:f}".format(c, math.ceil(c)))
print("round({0:f}) = {1:f}".format(a, round(a)))
print("round({0:f}) = {1:f}".format(c, round(c)))
print("trunc({0:f}) = {1:f}".format(a, math.trunc(a)))
print("trunc({0:f}) = {1:f}".format(c, math.trunc(c)))
print("min({0:f}, {1:f}) = {2:f}".format(a, c, min(a, c)))
print("max({0:f}, {1:f}) = {2:f}".format(a, c, max(a, c)))

# sin, cos, tan, atan, atan2, acos, asin
print("sin({0:f}) = {1:f}".format(a, math.sin(a)))
print("sin({0:f}) = {1:f}".format(b, math.sin(b)))
print("sin({0:f}) = {1:f}".format(c, math.sin(c)))
print("sin({0:f}) = {1:f}".format(d, math.sin(d)))
print("cos({0:f}) = {1:f}".format(a, math.cos(a)))
print("cos({0:f}) = {1:f}".format(b, math.cos(b)))
print("cos({0:f}) = {1:f}".format(c, math.cos(c)))
print("cos({0:f}) = {1:f}".format(d, math.cos(d)))
print("tan({0:f}) = {1:f}".format(a, math.tan(a)))
print("tan({0:f}) = {1:f}".format(b, math.tan(b)))
print("tan({0:f}) = {1:f}".format(c, math.tan(c)))
print("asin({0:f}) = {1:f}".format(math.sin(a), math.asin(math.sin(a))))
print("asin({0:f}) = {1:f}".format(math.sin(b), math.asin(math.sin(b))))
print("asin({0:f}) = {1:f}".format(math.sin(c), math.asin(math.sin(c))))
print("asin({0:f}) = {1:f}".format(math.sin(d), math.asin(math.sin(d))))
print("acos({0:f}) = {1:f}".format(math.cos(a), math.acos(math.cos(a))))
print("acos({0:f}) = {1:f}".format(math.cos(b), math.acos(math.cos(b))))
print("acos({0:f}) = {1:f}".format(math.cos(c), math.acos(math.cos(c))))
print("acos({0:f}) = {1:f}".format(math.cos(d), math.acos(math.cos(d))))
print("atan({0:f}) = {1:f}".format(math.tan(a), math.atan(math.tan(a))))
print("atan({0:f}) = {1:f}".format(math.tan(b), math.atan(math.tan(b))))
print("atan({0:f}) = {1:f}".format(math.tan(c), math.atan(math.tan(c))))
# 45 degrees
print("atan2({0:f}, {1:f}) = {2:f}".format(1.0, 1.0, math.atan2(1.0, 1.0)))
# 30 degrees
print("atan2({0:f}, {1:f}) = {2:f}".format(1.0, math.sqrt(3.0), math.atan2(1.0, math.sqrt(3.0))))

# sinh, cosh, tanh, atanh, acosh, asinh
print("sinh({0:f}) = {1:f}".format(a, math.sinh(a)))
print("sinh({0:f}) = {1:f}".format(b, math.sinh(b)))
print("sinh({0:f}) = {1:f}".format(c, math.sinh(c)))
print("sinh({0:f}) = {1:f}".format(d, math.sinh(d)))
print("cosh({0:f}) = {1:f}".format(a, math.cosh(a)))
print("cosh({0:f}) = {1:f}".format(b, math.cosh(b)))
print("cosh({0:f}) = {1:f}".format(c, math.cosh(c)))
print("cosh({0:f}) = {1:f}".format(d, math.cosh(d)))
print("tanh({0:f}) = {1:f}".format(a, math.tanh(a)))
print("tanh({0:f}) = {1:f}".format(b, math.tanh(b)))
print("tanh({0:f}) = {1:f}".format(c, math.tanh(c)))
print("tanh({0:f}) = {1:f}".format(d, math.tanh(d)))
print("asinh({0:f}) = {1:f}".format(math.sinh(a), math.asinh(math.sinh(a))))
print("asinh({0:f}) = {1:f}".format(math.sinh(b), math.asinh(math.sinh(b))))
print("asinh({0:f}) = {1:f}".format(math.sinh(c), math.asinh(math.sinh(c))))
print("asinh({0:f}) = {1:f}".format(math.sinh(d), math.asinh(math.sinh(d))))
print("acosh({0:f}) = {1:f}".format(math.cosh(a), math.acosh(math.cosh(a))))
print("acosh({0:f}) = {1:f}".format(math.cosh(b), math.acosh(math.cosh(b))))
print("acosh({0:f}) = {1:f}".format(math.cosh(c), math.acosh(math.cosh(c))))
print("acosh({0:f}) = {1:f}".format(math.cosh(d), math.acosh(math.cosh(d))))
print("atanh({0:f}) = {1:f}".format(math.tanh(a), math.atanh(math.tanh(a))))
print("atanh({0:f}) = {1:f}".format(math.tanh(b), math.atanh(math.tanh(b))))
print("atanh({0:f}) = {1:f}".format(math.tanh(c), math.atanh(math.tanh(c))))
print("atanh({0:f}) = {1:f}".format(math.tanh(d), math.atanh(math.tanh(d))))

# log, log10, exp, pow, sqrt
print("log({0:f}) = {1:f}".format(a, math.log(a)))
print("log({0:f}) = {1:f}".format(b, math.log(b)))
print("log({0:f}) = {1:f}".format(-c, math.log(-c)))
print("log({0:f}) = {1:f}".format(-d, math.log(-d)))
print("log({0:f}) = {1:f}".format(e, math.log(e)))
print("log10({0:f}) = {1:f}".format(a, math.log10(a)))
print("log10({0:f}) = {1:f}".format(b, math.log10(b)))
print("log10({0:f}) = {1:f}".format(-c, math.log10(-c)))
print("log10({0:f}) = {1:f}".format(-d, math.log10(-d)))
print("log10({0:f}) = {1:f}".format(e, math.log10(e)))
print("exp({0:f}) = {1:f}".format(0.5, math.exp(0.5)))
print("exp({0:f}) = {1:f}".format(1.0, math.exp(1.0)))
print("exp({0:f}) = {1:f}".format(2.0, math.exp(2.0)))
print("pow({0:f}, {1:f}) = {2:f}".format(10.0, 0.5, math.pow(10.0, 0.5)))
print("pow({0:f}, {1:f}) = {2:f}".format(10.0, 1.0, math.pow(10.0, 1.0)))
print("pow({0:f}, {1:f}) = {2:f}".format(10.0, 2.0, math.pow(10.0, 2.0)))
print("sqrt({0:f}) = {1:f}".format(0.5, math.sqrt(0.5)))
print("sqrt({0:f}) = {1:f}".format(2.0, math.sqrt(2.0)))
print("sqrt({0:f}) = {1:f}".format(10.0, math.sqrt(10.0)))

# random numbers
print("random() = {0:f}".format(random.random()))
print("random() = {0:f}".format(random.random()))
print("random() = {0:f}".format(random.random()))

```
Output
\$ python3 Math1.py pi = 3.141593 e = 2.718282 abs(0.523599) = 0.523599 abs(-1.047198) = 1.047198 floor(0.523599) = 0.000000 floor(-1.047198) = -2.000000 ceil(0.523599) = 1.000000 ceil(-1.047198) = -1.000000 round(0.523599) = 1.000000 round(-1.047198) = -1.000000 trunc(0.523599) = 0.000000 trunc(-1.047198) = -1.000000 min(0.523599, -1.047198) = -1.047198 max(0.523599, -1.047198) = 0.523599 sin(0.523599) = 0.500000 sin(0.785398) = 0.707107 sin(-1.047198) = -0.866025 sin(-1.570796) = -1.000000 cos(0.523599) = 0.866025 cos(0.785398) = 0.707107 cos(-1.047198) = 0.500000 cos(-1.570796) = 0.000000 tan(0.523599) = 0.577350 tan(0.785398) = 1.000000 tan(-1.047198) = -1.732051 asin(0.500000) = 0.523599 asin(0.707107) = 0.785398 asin(-0.866025) = -1.047198 asin(-1.000000) = -1.570796 acos(0.866025) = 0.523599 acos(0.707107) = 0.785398 acos(0.500000) = 1.047198 acos(0.000000) = 1.570796 atan(0.577350) = 0.523599 atan(1.000000) = 0.785398 atan(-1.732051) = -1.047198 atan2(1.000000, 1.000000) = 0.785398 atan2(1.000000, 1.732051) = 0.523599 sinh(0.523599) = 0.547853 sinh(0.785398) = 0.868671 sinh(-1.047198) = -1.249367 sinh(-1.570796) = -2.301299 cosh(0.523599) = 1.140238 cosh(0.785398) = 1.324609 cosh(-1.047198) = 1.600287 cosh(-1.570796) = 2.509178 tanh(0.523599) = 0.480473 tanh(0.785398) = 0.655794 tanh(-1.047198) = -0.780714 tanh(-1.570796) = -0.917152 asinh(0.547853) = 0.523599 asinh(0.868671) = 0.785398 asinh(-1.249367) = -1.047198 asinh(-2.301299) = -1.570796 acosh(1.140238) = 0.523599 acosh(1.324609) = 0.785398 acosh(1.600287) = 1.047198 acosh(2.509178) = 1.570796 atanh(0.480473) = 0.523599 atanh(0.655794) = 0.785398 atanh(-0.780714) = -1.047198 atanh(-0.917152) = -1.570796 log(0.523599) = -0.647030 log(0.785398) = -0.241564 log(1.047198) = 0.046118 log(1.570796) = 0.451583 log(2.718282) = 1.000000 log10(0.523599) = -0.281001 log10(0.785398) = -0.104910 log10(1.047198) = 0.020029 log10(1.570796) = 0.196120 log10(2.718282) = 0.434294 exp(0.500000) = 1.648721 exp(1.000000) = 2.718282 exp(2.000000) = 7.389056 pow(10.000000, 0.500000) = 3.162278 pow(10.000000, 1.000000) = 10.000000 pow(10.000000, 2.000000) = 100.000000 sqrt(0.500000) = 0.707107 sqrt(2.000000) = 1.414214 sqrt(10.000000) = 3.162278 random() = 0.210281 random() = 0.076644 random() = 0.937783
Math2.py
```#!/usr/bin/env python3;
###############################################################################
# This program demonstrates the math integer functions.
#
###############################################################################

import Utils
import math
import random

# Begin Main
a = 5
b = 10
c = -2

# abs, floor, ceil, round, trunc, min, max
print("abs({0:d}) = {1:d}".format(a, abs(a)))
print("abs({0:d}) = {1:d}".format(c, abs(c)))
print("min({0:d}, {1:d}) = {2:d}".format(a, b, min(a, b)))
print("max({0:d}, {1:d}) = {2:d}".format(a, b, max(a, b)))
print("min({0:d}, {1:d}) = {2:d}".format(b, c, min(b, c)))
print("max({0:d}, {1:d}) = {2:d}".format(b, c, max(b, c)))

# random numbers
print("random({0:d}) = {1:d}".format(a, int(a * random.random())))
print("random({0:d}) = {1:d}".format(a, int(a * random.random())))
print("random({0:d}) = {1:d}".format(a, int(a * random.random())))
print("random({0:d}) = {1:d}".format(a, int(a * random.random())))
print("random({0:d}) = {1:d}".format(a, int(a * random.random())))
print("random({0:d}) = {1:d}".format(b, int(b * random.random())))
print("random({0:d}) = {1:d}".format(b, int(b * random.random())))
print("random({0:d}) = {1:d}".format(b, int(b * random.random())))
print("random({0:d}) = {1:d}".format(b, int(b * random.random())))
print("random({0:d}) = {1:d}".format(b, int(b * random.random())))
print("random(2) = {0:d}".format(int(2 * random.random())))
print("random(2) = {0:d}".format(int(2 * random.random())))
print("random(2) = {0:d}".format(int(2 * random.random())))
print("random(2) = {0:d}".format(int(2 * random.random())))
print("random(2) = {0:d}".format(int(2 * random.random())))
print("random() = {0:f}".format(random.random()))
print("random() = {0:f}".format(random.random()))
print("random() = {0:f}".format(random.random()))
print("random() = {0:f}".format(random.random()))
print("random() = {0:f}".format(random.random()))

```
Output
\$ python3 Math2.py abs(5) = 5 abs(-2) = 2 min(5, 10) = 5 max(5, 10) = 10 min(10, -2) = -2 max(10, -2) = 10 random(5) = 0 random(5) = 2 random(5) = 4 random(5) = 1 random(5) = 0 random(10) = 3 random(10) = 6 random(10) = 6 random(10) = 2 random(10) = 2 random(2) = 0 random(2) = 0 random(2) = 0 random(2) = 1 random(2) = 0 random() = 0.960084 random() = 0.352941 random() = 0.339514 random() = 0.670385 random() = 0.017732

## Random Numbers

Explain how to generate uniform random numbers, int and fp.

## Questions

• {{Write an expression that yields √5.}}
• {{Write an expression that yields 3√5.}}
• {{Write an expression that yields the secant of π/4.}}
• {{Write an expression that yields log16 100.}}

## Projects

More ★'s indicate higher difficulty level.

## References

• [[Python Language Reference]]
• [[Python Standard Library]]
• [[Python at TutorialsPoint]]