 # EPSILON = 1E-14 + abs Function

Hey y’all. I’m back. Hopefully I don’t get too annoying over the next little bit, but I’m really interested in learning to code. The course I’m currently taking in Uni uses the following code to demonstrate comparisons of numbers/strings. The only block I’m really struggling with is the bit I’ve outlined with a comment (EPSILON block). Can anybody explain to me what’s happening there?

Thanks!

``````##
# This program demonstrates comparisons of numbers and strings
#

from math import sqrt

# Comparing integers
m = 2
n = 4

if m * m == n :
print ("2 times 2 is four.")

# Comparing floating point numbers
x = sqrt(2)
y = 2.0

if x * x == y :
print ("sqrt(2) times sqrt(2) is 2")
else :
print ("sqrt(2) times sqrt(2) is not two but %.18f" % (x * x)) #%.18f shows the whole number + 18 decimal spaces

# Not understanding this next block. What is EPSILON? What does 1E-14 equal?
EPSILON = 1E-14
if abs(x * x - y) < EPSILON : # Not fully understanding the abs function
print("sqrt(2) times sqrt(2) is approximately 2")

# Comparing strings
s = "120"
t = "20"

if s == t :
comparison = "is the same as"
else :
comparison = "is not the same as"

print ("The string '%s' %s the string '%s'." % (s, comparison, t))

u = "1" + t
if s != u :
comparison = "not "
else :
comparison = ""

print ("The strings '%s' and '%s' are %sidentical." % (s, u, comparison))
``````

`1E-14` is programming/engineering notation for the number 1×10^(-14)

This kind of notation is very useful for writing very small or very large numbers, but it might be easier to illustrate with numbers closer to ±1:

``````>>> 1e-3
0.001
>>> 2e-4
0.0002
>>> 3e0
3.0
>>> 1 * 10**(-3)
0.001
>>> 1e3
1000.0
>>> 2e2
200.0
>>>
``````

`abs()` gives you the absolute value of a number

``````>>> abs(1)
1
>>> abs(-1)
1
>>> abs(-7)
7
>>> abs(10)
10
>>> abs(-.2)
0.2
``````

Hi Ryan,

For simple questions like “what does abs() do?” your first stop should
be the interactive help in the Python interpreter:

``````help(abs)
``````

which ought to show something like:

``````Help on built-in function abs in module builtins:
abs(x, /)
Return the absolute value of the argument.
``````

You can try it out in the interpeter:

``````abs(3.5)   # returns 3.5
abs(-3.5)  # also returns 3.5
``````

https://docs.python.org/3/library/functions.html#abs

As for the mystery epsilon,

``````EPSILON = 1E-14
``````

if you are familiar with scientific calculators, you might remember
seeing a button that might be called “E” (for Exponent or exponential
notation). The number after the E tells you how many places to move the
decimal point:

``````1E5 --> 1.000000E5
move the decimal point five places to the right
--> 100000.0

1E-5 --> 000001.0E5
move the decimal point five places to the left
--> 0.00001
``````

The E notation is short-cut for multiplying by ten to the given power:

``````1E-14 == 1.0 * 10**-14
``````

The name “epsilon” is a bit of mathematics jargon. Epsilon is the Greek
letter for “E”, and for historical reasons, mathematicians use the Greek
epsilon Îµ to represent “a tiny amount”.

So this equation:

``````if abs(x * x - y) < EPSILON
``````

tests whether the difference between x squared (x times x) and y is less
than “a tiny amount” (1e-14), and if it is, it prints the message that
follows.

I think I understand. So the general idea is to compare the absolute (sqrt(2) * sqrt(2)) to the EPSILON variant. Since the sqrt of 2 is 1.4142135637 (and not a perfectly even number), the code is looking to see whether the difference is a roundoff error or not, correct? So because the difference is lower than 1e-14, it types out that it’s approximately 2. Right?

@Steven.daprano: Thanks for the help(abs) bit. I didn’t realize this was a function (is it called a function?)

`abs()` is indeed a function, so is `sqrt()` and `print()`. And yep that’s what this code is for. Generally it’s a good idea to do this sort of check rather than equality with floats, since you will likely have slight inprecision after doing calculations.The `math` module actually also includes the function `isclose()`, which does the subtraction, absolute value and comparison for you as well as a percentage-difference check to make it more convenient to do the comparison.

1 Like

Hi Ryan,

Yes, you are correct: the error message is saying that you don’t have
the matplotlib module installed.

Unfortunately, installing new modules is one of those things that
experts find easy and beginners maybe not so much. In the simplest
case, all you need to do is run

``````pip install matplotlib
``````

from your OS command line, and (in theory) it will Just Work.

Assuming that you have pip installed, that there is only one version of
Python on your computer, that you have the right permissions on the
computer, that pip isn’t blocked from accessing the internet, etc.

When everything goes right, installing things is easy. When things go
wrong, it is often tricky to work out why it didn’t work.

You can try the instructions here:

https://matplotlib.org/stable/users/installing.html

and if you need help, feel free to ask.

(It might be better to start a new discussion thread for that.)