Skip to content

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

Source code in demoprogram/calc.py
def add(a: Union[int, float], b: Union[int, float]) -> Union[int, float]:
    """
    Add two numbers.

    Args:
        a: First number
        b: Second number

    Returns:
        Sum of the two numbers
    """
    return a + b

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

Source code in demoprogram/calc.py
def subtract(a: Union[int, float], b: Union[int, float]) -> Union[int, float]:
    """
    Subtract second number from first number.

    Args:
        a: First number
        b: Second number

    Returns:
        Difference of the two numbers
    """
    return a - b

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

Source code in demoprogram/calc.py
def multiply(a: Union[int, float], b: Union[int, float]) -> Union[int, float]:
    """
    Multiply two numbers.

    Args:
        a: First number
        b: Second number

    Returns:
        Product of the two numbers
    """
    return a * b

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
def divide(a: Union[int, float], b: Union[int, float]) -> float:
    """
    Divide first number by second number.

    Args:
        a: First number (dividend)
        b: Second number (divisor)

    Returns:
        Quotient of the division

    Raises:
        ZeroDivisionError: If b is zero
    """
    if b == 0:
        raise ZeroDivisionError("Cannot divide by zero")
    return a / b

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
def power(base: Union[int, float], exponent: Union[int, float]) -> Union[int, float]:
    """
    Raise base to the power of exponent.

    Args:
        base: Base number
        exponent: Exponent

    Returns:
        Result of base raised to exponent
    """
    return base ** exponent

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
def square_root(number: Union[int, float]) -> float:
    """
    Calculate the square root of a number.

    Args:
        number: Number to find square root of

    Returns:
        Square root of the number

    Raises:
        ValueError: If number is negative
    """
    if number < 0:
        raise ValueError("Cannot calculate square root of negative number")
    return math.sqrt(number)

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
def factorial(n: int) -> int:
    """
    Calculate the factorial of a non-negative integer.

    Args:
        n: Non-negative integer

    Returns:
        Factorial of n

    Raises:
        ValueError: If n is negative
    """
    if n < 0:
        raise ValueError("Factorial is not defined for negative numbers")
    if n == 0 or n == 1:
        return 1
    result = 1
    for i in range(2, n + 1):
        result *= i
    return result

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

Source code in demoprogram/calc.py
def gcd(a: int, b: int) -> int:
    """
    Calculate the greatest common divisor of two integers.

    Args:
        a: First integer
        b: Second integer

    Returns:
        Greatest common divisor
    """
    return math.gcd(a, b)

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

Source code in demoprogram/calc.py
def lcm(a: int, b: int) -> int:
    """
    Calculate the least common multiple of two integers.

    Args:
        a: First integer
        b: Second integer

    Returns:
        Least common multiple
    """
    return abs(a * b) // math.gcd(a, b)

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
def average(numbers: List[Union[int, float]]) -> float:
    """
    Calculate the average of a list of numbers.

    Args:
        numbers: List of numbers

    Returns:
        Average of the numbers

    Raises:
        ValueError: If the list is empty
    """
    if not numbers:
        raise ValueError("Cannot calculate average of empty list")
    return sum(numbers) / len(numbers)

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
def percentage(part: Union[int, float], total: Union[int, float]) -> float:
    """
    Calculate what percentage part is of total.

    Args:
        part: Part value
        total: Total value

    Returns:
        Percentage as a decimal (multiply by 100 for percentage)

    Raises:
        ZeroDivisionError: If total is zero
    """
    if total == 0:
        raise ZeroDivisionError("Total cannot be zero")
    return part / total

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

Source code in demoprogram/calc.py
def round_to_decimal(number: float, decimal_places: int = 2) -> float:
    """
    Round a number to a specified number of decimal places.

    Args:
        number: Number to round
        decimal_places: Number of decimal places (default: 2)

    Returns:
        Rounded number
    """
    return round(number, decimal_places)