Home-made Mathematics library using the Python programming language. It will emulate the math - Mathematical functions library in Python, although it will be a much less complete version.
The math library will be rudimentary but fully functional, including all elementary arithmetic operations (i.e., addition, subtraction, multiplication, and division) and some more advanced topics (i.e., exponents and logarithms) and two special functions (absolute value and division and modulus operation).
LIMITATION
The only limitation is that only the Python +
and -
operators are allowed to be utilized. Every other function MUST use these. That is, no operational code is allowed to use 3rd party libraries or the operators exceptions +
and -
.
The mult
function uses the fact that multiplication is a series of additions or subtractions in compute the multiplication of two numbers. So 3 * 2 is
sum = 0
for _ in range(2):
sum = sum + 3
Operation | Python Code |
---|---|
sum = 0; for _ in range(2): sum = sum + 3 |
|
sum = 0; for _ in range(2): sum = sum - 3 |
Once created, the math_lib
will have the ability do multiplications.
mult(3, 2)
- Elementary Operations
- Addition (
add
,+
) - Subtraction (
sub
,-
) - Multiplication (
mult
) - Division (
div
)
- Addition (
- Advanced Functions
- Square Root (
sqrt
) - Exponentiation (
exp
) - Logarithm (
log
)
- Square Root (
- Special Functions
- Absolute Value (
abs
) - Division and Modulus (
divmod
)
- Absolute Value (
- Addition (
add
,+
) - Subtraction (
sub
,-
) - Multiplication (
mult
) ** 2 - Division (
div
)
Note
Only the Python
+
and-
operators are allowed to be utilized in this library.
This is a binary function that takes in two numbers (augend
and addend
) and returns the sum of two numbers
.
def add(augend: float, addend: float) -> float:
"""Addition function.
Args:
augend (float): The augend of the addition.
addend (float): The addend of the addition.
Returns:
float: The sum of augend and addend.
"""
return augend + addend
This is a binary function that takes in two numbers (minuend
and subtrahend
) and returns the difference of two numbers
.
def sub(minuend: float, subtrahend: float) -> float:
"""Subtraction function.
Args:
minuend (float): The number being subtracted from.
subtrahend (float): The number being subtracted from another.
Returns:
float: The difference of minuend and subtrahend.
"""
return minuend - subtrahend
This is a binary function that takes in two numbers (multiplier
and multiplicand
) and returns the product of the two numbers
.
def mult(multiplier: float, multiplicand: float) -> float:
"""Multiplication function.
Args:
multiplier (float): Multiplier.
multiplicand (float): Multiplicand.
Returns:
float: Product of multiplier and multiplicand.
"""
value = range(multiplicand)
func = add
if multiplicand < 0:
value = range(0, multiplicand, -1)
func = sub
sum = 0
for _ in value:
sum = func(sum, multiplier)
return sum
- Square Root (
sqrt
) - Exponentiation (
exp
) ** 4 - Logarithm (
log
)
- Absolute Value (
abs
) ** 1 - Division and Modulus (
divmod
) ** 3
This function returns the absolute value of a number
.
def abs(number: float) -> float:
"""Absolute function.
Args:
number (float): Real number.
Returns:
float: Absolute value of number.
"""
if number < 0:
number = -1 * number
return number