In [10]:
%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
import math
import pylab
from matplotlib.pyplot import *
from numpy import *

def exercise1():
    """
    The expression is not true because of the way floating point integers
    are working in a computer program. 
    """

    print("Exercise 1:")
    x = 0.1 + 0.1 + 0.1 + 0.1
    y = 0.5 
    
    # Confirm they are unequal
    if x != y:
        print("First statement is false!")
    
    # We can check whether the difference between x and y is less than desired accuracy
    if x - y < 0.1e-15:
        print("Second statement is true!")
        
    # Or we can format the number into a desired accuracy and check the results
    if float("{0:.15f}".format(x)) == float("{0:.15f}".format(y)):
        print('Third statement is true! x = {}, y = {}'.format(x, y)) # Print values to show they aren't actually the same
    
    # With large numbers we can cast the float to int - assuming we don't care about accuracy (e.g. speedometer)
    # In some scenarios this might round of the values differently so it's not completely accurate (0.4 -> 0, 0.5 -> 1)
    if int(x) == int(y):
        print("Fourth statement is true! x = {}, y = {}".format(int(x), int(y)))

def exercise2():
    print("\nExercise 2:")
    
    eps = 1.0
    
    while eps + 1.0 > 1.0:
        eps = eps/2.0
    
    eps = 2.0 * eps
    print("Final value for eps is {}".format(eps))

def exercise3():
    print("\nExercise 3:")
    
    x = 0.1
    
    while x > 0:
        
        # Break if the result is zero as it's impossible to recover if the value is actually zero
        if x / 2 == 0:
            break
            
        x = x / 2
        
    print("Smallest possible number to differ from 0 is {}".format(x))

def exercise4(increment, count):
    print("\nExercise 4:")

    y = 1.0
    
    i = 1
    while i <= count:
        eps = y
        while eps + 1.0 > 1.0:
            eps = eps/2.0
        
        eps = 2.0 * eps
        print("Round {}, y = {}: Value for epsilon is {}".format(i, y, eps))
        i += 1
        y += increment
        
exercise1()
exercise2()
exercise3()
exercise4(0.1, 15)


Exercise 1:
First statement is false!
Second statement is true!
Fourth statement is true! x = 0, y = 0

Exercise 2:
Final value for eps is 2.220446049250313e-16

Exercise 3:
Smallest possible number to differ from 0 is 5e-324

Exercise 4:
Round 1, y = 1.0: Value for epsilon is 2.220446049250313e-16
Round 2, y = 1.1: Value for epsilon is 1.2212453270876723e-16
Round 3, y = 1.2000000000000002: Value for epsilon is 1.332267629550188e-16
Round 4, y = 1.3000000000000003: Value for epsilon is 1.4432899320127038e-16
Round 5, y = 1.4000000000000004: Value for epsilon is 1.5543122344752196e-16
Round 6, y = 1.5000000000000004: Value for epsilon is 1.6653345369377353e-16
Round 7, y = 1.6000000000000005: Value for epsilon is 1.776356839400251e-16
Round 8, y = 1.7000000000000006: Value for epsilon is 1.8873791418627668e-16
Round 9, y = 1.8000000000000007: Value for epsilon is 1.9984014443252826e-16
Round 10, y = 1.9000000000000008: Value for epsilon is 2.1094237467877983e-16
Round 11, y = 2.000000000000001: Value for epsilon is 1.110223024625157e-16
Round 12, y = 2.100000000000001: Value for epsilon is 1.165734175856415e-16
Round 13, y = 2.200000000000001: Value for epsilon is 1.2212453270876728e-16
Round 14, y = 2.300000000000001: Value for epsilon is 1.2767564783189307e-16
Round 15, y = 2.4000000000000012: Value for epsilon is 1.3322676295501885e-16