TP: Implementing the Exponential Function (3/5)#

Part 3: Comparing floating-point numbers and relative precision#

Regarding floating-point numbers (float), the == operator is not always very reliable because of

rounding errors:

1.0 + 1e20 - 1e20 == 1e20 - 1e20 + 1.0
False

Run the following five cells. What do you notice?

BEGIN SOLUTION

a and b do have different values but are displayed in the same way.

END SOLUTION

a = 16
b = 15.9999999999999999
a
16
b
16.0
(a == b)
True

Definition: relative precision#

Whenever we want to compare two floating-point numbers, we must specify with what

precision we want to compare them.

The simplest way is to set a threshold of absolute precision \(\eta\), that is, the difference in value below which we consider two numbers to be equal:

\(x \simeq y\) if \(|x - y| < \eta\).

However, this absolute precision value can be difficult to set, especially when the values are very variable in order of magnitude. For example, if we consider the heights of mountain peaks, we could be largely satisfied with being precise to the nearest ten meters (\(\eta = 10^1\)m). On the other hand, if we compare the sizes of people, we would like to be precise to the centimeter (\(\eta = 10^{-2}\)m). We would therefore need totally different absolute precisions depending on the order of magnitude of the values.

In fact, it is most often the number of significant digits in common that is relevant to measure. Formally, we then use a relative precision. If we want to compare \(x\) and \(y\) with a relative precision of the order of five significant digits, we will take \(\varepsilon=10^{-5}=0,00001\), and we will say that \(x\) is equal to

\(y\) to within \(\varepsilon\) if:

|x - y| < \varepsilon|x| \qquad \text{et} \qquad |x - y| < \varepsilon |y|^^^math_blockMorally: the difference between \(x\) and \(y\) is negligible compared to \(x\) and compared to \(y\).

This concept is particularly relevant in our case, because the exponential gives values of very different orders of magnitude: \(e^1 \simeq 10^0\), \(e^5 \simeq 10^2\),

\(e^{10} \simeq 10^5\)

Exercise

In each of the following examples, is \(x\simeq y\) with a relative precision

\(\varepsilon=0.1\)? a relative precision \(\varepsilon=0.01\)?

  • \(x=1.22\), \(y=1.24\)

  • \(x=1220\), \(y=1240\)

  • \(x=0.0122\), \(y=0.0124\)

  • \(x=0\), \(y=0.01\)

Implementation#

Implement the function for which the documentation is given to you:

  • egal which takes the numbers \(x\), \(y\) and \(\varepsilon\) as parameters.

You can use the predefined function in Python abs(n) which returns the absolute value of the number n.

print(abs(-1.5))
print(abs( 2.2))
1.5
2.2
def egal(x, y, epsilon):
    """ Égalité entre deux flottants avec précision relative
     * Paramètre x : un flottant
     * Paramètre y : un flottant
     * Paramètre epsilon : flottant
     * @return true si la valeur absolue de x - y est plus petite que epsilon * |x| et que epsilon * |y|
    """
### BEGIN SOLUTION
    v = abs(x-y)
    return ((v < epsilon * abs(x)) and (v < epsilon * abs(y)))
### END SOLUTION
egal(15.999999, 16, 0.00001)
True
assert( egal(15.999999, 16, 0.00001) == True  )
assert( egal(15.99, 16, 0.00001)     == False )

Find values of epsilon such that the numbers below are considered

equal by egal:

egal(15, 16, 0.001)
False
egal(0.0001, 0.002, 0.00001)
False

What happens when \(x\) or \(y\) are equal to \(0\)?

BEGIN SOLUTION

When \(x\) or \(y\) are equal to \(0\) (even when both are equal to \(0\)), the function egal

always returns false. We cannot speak of relative precision to zero.

END SOLUTION

egal(0.00001, 0, 0.0000001)
False

Review of Part 3#

Now that the concept of relative precision is well defined, and you have

implemented the comparison of floating-point numbers with a fixed relative precision, you

can move on to part 4.