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.

Hola! Prueba de CICD!

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 float

A number representing the first addend in the addition.

required
b float

A number representing the second addend in the addition.

required

Returns:

Name Type Description
float

A number representing the arithmetic sum of a and b.

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

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

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

    Returns:
        float: 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

Raises:

Type Description
ValueError

If the divisor (b) is zero.

Returns:

Name Type Description
float

A number representing the arithmetic quotient of a and b.

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

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

    Args:
        >>> divide(4.0, 2.0)
        2.0
        >>> divide(4, 2)
        2.0

    Raises:
        ValueError: If the divisor (`b`) is zero.

    Returns:
        float: A number representing the arithmetic quotient of `a` and `b`.
    """
    if b == 0:
        raise ValueError("Division by zero is not allowed.")

    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 float

A number representing the minuend in the subtraction.

required
b float

A number representing the subtrahend in the subtraction.

required

Returns:

Name Type Description
float

A number representing the arithmetic difference of a and b.

Source code in calculator/calculations.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def multiply(a, b):
    """Compute and return the product of two numbers.

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

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

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

power(a, b)

Compute and return the power of a number.

Examples:

>>> power(4.0, 2.0)
16.0
>>> power(4, 2)
16.0

Parameters:

Name Type Description Default
a float

A number representing the base in the power operation.

required
b float

A number representing the exponent in the power operation.

required

Returns:

Name Type Description
float

A number representing the result of raising a to the power of b.

Source code in calculator/calculations.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
def power(a, b):
    """Compute and return the power of a number.

    Examples:
        >>> power(4.0, 2.0)
        16.0
        >>> power(4, 2)
        16.0

    Args:
        a (float): A number representing the base in the power operation.
        b (float): A number representing the exponent in the power operation.

    Returns:
        float: A number representing the result of raising `a` to the power of `b`.
    """
    return float(a ** b)

substract(a, b)

Compute and return the difference of two numbers.

Examples:

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

Parameters:

Name Type Description Default
a float

A number representing the minuend in the subtraction.

required
b float

A number representing the subtrahend in the subtraction.

required

Returns:

Name Type Description
float

A number representing the arithmetic difference of a and b.

Source code in calculator/calculations.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def substract(a, b):
    """Compute and return the difference of two numbers.

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

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

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