Skip to content

Reference

This part of the project documentation focuses on an information-oriented approach. Use it as a reference for the technical implementation of the calculator project code.

Provide several sample math calculations.

This module allows the user to make mathematical calculations.

Examples:

>>> from calculator import calculations
>>> calculations.add(2, 4)
6.0
>>> calculations.multiply(2.0, 4.0)
8.0
>>> from calculator.calculations import divide
>>> divide(4.0, 2)
2.0

The module contains the following functions:

  • add(a, b) - Returns the sum of two numbers.
  • subtract(a, b) - Returns the difference of two numbers.
  • multiply(a, b) - Returns the product of two numbers.
  • divide(a, b) - Returns the quotient of two numbers.

add(a, b)

Compute and return the sum of two numbers.

Examples:

>>> add(4.0, 2.0)
6.0
>>> add(4, 2)
6.0

Parameters:

Name Type Description Default
a

A number representing the first addend in the addition.

required
b

A number representing the second addend in the addition.

required

Returns:

Type Description
float

A number representing the arithmetic sum of a and b.

Source code in calculator/calculations.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def add(a: Union[float, int], b: Union[float, int]) -> float:
    """Compute and return the sum of two numbers.

    Examples:
        >>> add(4.0, 2.0)
        6.0
        >>> add(4, 2)
        6.0


    Args:
        a : A number representing the first addend in the addition.
        b : A number representing the second addend in the addition.

    Returns:
        A number representing the arithmetic sum of `a` and `b`.
    """
    return float(a + b)

divide(a, b)

Compute and return the quotient of two numbers.

Examples:

>>> divide(4.0, 2.0)
2.0
>>> divide(4, 2)
2.0
>>> divide(4, 0)
Traceback (most recent call last):
...
ZeroDivisionError: division by zero

Parameters:

Name Type Description Default
a

A number representing the dividend in the division.

required
b

A number representing the divisor in the division.

required

Returns:

Type Description
float

A number representing the quotient of a and b.

Raises:

Type Description
ZeroDivisionError

An error occurs when the divisor is 0.

Source code in calculator/calculations.py
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def divide(a: Union[float, int], b: Union[float, int]) -> float:
    """Compute and return the quotient of two numbers.

    Examples:
        >>> divide(4.0, 2.0)
        2.0
        >>> divide(4, 2)
        2.0
        >>> divide(4, 0)
        Traceback (most recent call last):
        ...
        ZeroDivisionError: division by zero

    Args:
        a : A number representing the dividend in the division.
        b : A number representing the divisor in the division.

    Returns:
        A number representing the quotient of `a` and `b`.

    Raises:
        ZeroDivisionError: An error occurs when the divisor is `0`.
    """
    if b == 0:
        raise ZeroDivisionError("division by zero")
    return float(a / b)

multiply(a, b)

Compute and return the product of two numbers.

Examples:

>>> multiply(4.0, 2.0)
8.0
>>> multiply(4, 2)
8.0

Parameters:

Name Type Description Default
a

A number representing the multiplicand in the multiplication.

required
b

A number representing the multiplier in the multiplication.

required

Returns:

Type Description
float

A number representing the product of a and b.

Source code in calculator/calculations.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def multiply(a: Union[float, int], b: Union[float, int]) -> float:
    """Compute and return the product of two numbers.

    Examples:
        >>> multiply(4.0, 2.0)
        8.0
        >>> multiply(4, 2)
        8.0

    Args:
        a : A number representing the multiplicand in the multiplication.
        b : A number representing the multiplier in the multiplication.

    Returns:
        A number representing the product of `a` and `b`.
    """
    return float(a * b)

subtract(a, b)

Calculate the difference of two numbers.

Examples:

>>> subtract(4.0, 2.0)
2.0
>>> subtract(4, 2)
2.0

Parameters:

Name Type Description Default
a

A number representing the minuend in the subtraction.

required
b

A number representing the subtrahend in the subtraction.

required

Returns:

Type Description
float

A number representing the difference between a and b.

Source code in calculator/calculations.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def subtract(a: Union[float, int], b: Union[float, int]) -> float:
    """Calculate the difference of two numbers.

    Examples:
        >>> subtract(4.0, 2.0)
        2.0
        >>> subtract(4, 2)
        2.0

    Args:
        a : A number representing the minuend in the subtraction.
        b : A number representing the subtrahend in the subtraction.

    Returns:
        A number representing the difference between `a` and `b`.
    """
    return float(a - b)