#!/usr/bin/env python # coding: utf-8 # # Error due to subtraction # Consider computing # $$ # x = 10^{-8}, \qquad y = \sqrt{1+x^2} - 1 # $$ # In[1]: from math import sqrt x = 1.0e-8 y = sqrt(1.0 + x**2) - 1.0 print("%20.10e" % y) # The result is zero due to round-off error. An equivalent expression is # $$ # y = \frac{x^2}{\sqrt{1 + x^2} + 1} # $$ # In[2]: y = x**2/(sqrt(1.0+x**2) + 1.0) print("%20.10e" % y)