Arithmetic Functions¶
The calc
module provides various arithmetic and mathematical functions.
Module Reference¶
demoprogram.calc
¶
Arithmetic calculation functions for the demo program package.
Functions¶
add(a: Union[int, float], b: Union[int, float]) -> Union[int, float]
¶
Add two numbers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
Union[int, float]
|
First number |
required |
b
|
Union[int, float]
|
Second number |
required |
Returns:
Type | Description |
---|---|
Union[int, float]
|
Sum of the two numbers |
subtract(a: Union[int, float], b: Union[int, float]) -> Union[int, float]
¶
Subtract second number from first number.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
Union[int, float]
|
First number |
required |
b
|
Union[int, float]
|
Second number |
required |
Returns:
Type | Description |
---|---|
Union[int, float]
|
Difference of the two numbers |
multiply(a: Union[int, float], b: Union[int, float]) -> Union[int, float]
¶
Multiply two numbers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
Union[int, float]
|
First number |
required |
b
|
Union[int, float]
|
Second number |
required |
Returns:
Type | Description |
---|---|
Union[int, float]
|
Product of the two numbers |
divide(a: Union[int, float], b: Union[int, float]) -> float
¶
Divide first number by second number.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
Union[int, float]
|
First number (dividend) |
required |
b
|
Union[int, float]
|
Second number (divisor) |
required |
Returns:
Type | Description |
---|---|
float
|
Quotient of the division |
Raises:
Type | Description |
---|---|
ZeroDivisionError
|
If b is zero |
Source code in demoprogram/calc.py
power(base: Union[int, float], exponent: Union[int, float]) -> Union[int, float]
¶
Raise base to the power of exponent.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base
|
Union[int, float]
|
Base number |
required |
exponent
|
Union[int, float]
|
Exponent |
required |
Returns:
Type | Description |
---|---|
Union[int, float]
|
Result of base raised to exponent |
Source code in demoprogram/calc.py
square_root(number: Union[int, float]) -> float
¶
Calculate the square root of a number.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
number
|
Union[int, float]
|
Number to find square root of |
required |
Returns:
Type | Description |
---|---|
float
|
Square root of the number |
Raises:
Type | Description |
---|---|
ValueError
|
If number is negative |
Source code in demoprogram/calc.py
factorial(n: int) -> int
¶
Calculate the factorial of a non-negative integer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
int
|
Non-negative integer |
required |
Returns:
Type | Description |
---|---|
int
|
Factorial of n |
Raises:
Type | Description |
---|---|
ValueError
|
If n is negative |
Source code in demoprogram/calc.py
gcd(a: int, b: int) -> int
¶
Calculate the greatest common divisor of two integers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
int
|
First integer |
required |
b
|
int
|
Second integer |
required |
Returns:
Type | Description |
---|---|
int
|
Greatest common divisor |
lcm(a: int, b: int) -> int
¶
Calculate the least common multiple of two integers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
int
|
First integer |
required |
b
|
int
|
Second integer |
required |
Returns:
Type | Description |
---|---|
int
|
Least common multiple |
average(numbers: List[Union[int, float]]) -> float
¶
Calculate the average of a list of numbers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
numbers
|
List[Union[int, float]]
|
List of numbers |
required |
Returns:
Type | Description |
---|---|
float
|
Average of the numbers |
Raises:
Type | Description |
---|---|
ValueError
|
If the list is empty |
Source code in demoprogram/calc.py
percentage(part: Union[int, float], total: Union[int, float]) -> float
¶
Calculate what percentage part is of total.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
part
|
Union[int, float]
|
Part value |
required |
total
|
Union[int, float]
|
Total value |
required |
Returns:
Type | Description |
---|---|
float
|
Percentage as a decimal (multiply by 100 for percentage) |
Raises:
Type | Description |
---|---|
ZeroDivisionError
|
If total is zero |
Source code in demoprogram/calc.py
round_to_decimal(number: float, decimal_places: int = 2) -> float
¶
Round a number to a specified number of decimal places.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
number
|
float
|
Number to round |
required |
decimal_places
|
int
|
Number of decimal places (default: 2) |
2
|
Returns:
Type | Description |
---|---|
float
|
Rounded number |