%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm as normal_dist
import seaborn as sns
sns.set(style='ticks')
print(np.float128(1) + np.float128(2)**np.float128(-63) - np.float128(1) == 0)
print(np.float64(1) + np.float64(2)**np.float64(-52) - np.float64(1) == 0)
print(np.float32(1) + np.float32(2)**np.float32(-23) - np.float32(1) == 0)
print(1 + np.finfo(np.longdouble).eps == 1.0000000000000000001)
False False False False
pol_fact = lambda x: (x-2)**9
pol_exp = lambda x: x**9 - 18*x**8 + 144*x**7 -672*x**6 + 2016*x**5 - 4032*x**4 + 5376*x**3 - 4608*x**2 + 2304*x - 512
x = np.linspace(2-1e-6, 2+1e-6, 10000)
yf = pol_fact(x)
ye = pol_exp(x)
fig, axes = plt.subplots(1, 2, figsize=(8, 4))
ax = axes[0]
ax.plot(x, ye, '.', label='expanded', alpha=0.5)
ax.plot(x, yf, label='$(x-2)^9$')
ax.legend()a=
ax.set(xlabel='x', ylabel=('f(x)'), xticks=(x.min(), 2, x.max()))
ax = axes[1]
sns.distplot(ye - yf, kde=False, fit=normal_dist, ax=axes[1])
ax.set(xlabel='exapanded error', ylabel='frequency')
sns.despine()
a = 1
b = -1.786737601482363
c = 2.054360090947453e-8
pol = lambda x: x**2 + b*x + c
D = b**2 - 4 * c
x1a = (-b + np.sqrt(D)) / 2
x2a = (-b - np.sqrt(D)) / 2
print(x1a, x2a)
print(pol(x1a), pol(x2a))
1.78673758998 1.14978276899e-08 3.1103068352e-16 -2.73126898808e-17
x1b = (-b -np.sign(b) * np.sqrt(D)) / 2
x2b = c / x1b# c/a*x1
print(x1b, x2b)
print(pol(x1b), pol(x2b))
1.78673758998 1.14978276747e-08 3.1103068352e-16 3.30872245021e-24
x1a == x1b, x2a == x2b
(True, False)
x2a / x2b - 1
1.3294987333267727e-09