Let us evaluate $$ y = x^3 - 3 x^2 + 3 x - 1 $$ in single precision. Note that it can also be written as $$ y = (x-1)^3 $$
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
import numpy as np
import matplotlib.pyplot as plt
We evaluate in the interval $[0.99,1.01]$.
x = np.linspace(0.99,1.01,50,dtype=np.float32)
y = x**3 - 3.0*x**2 + 3.0*x - 1.0
plt.plot(x,y,'-o',x,(x-1)**3)
plt.xlabel('x')
plt.ylabel('y')
plt.legend(('Single precision','Exact'));
Now, let us do it in double precision
x = np.linspace(0.99,1.01,50,dtype=np.float64)
y = x**3 - 3.0*x**2 + 3.0*x - 1.0
plt.plot(x,y,'-o',x,(x-1)**3)
plt.xlabel('x')
plt.ylabel('y')
plt.legend(('Double precision','Exact'));