{ "metadata": { "name": "", "signature": "sha256:30d4983d328c7423d055ee8319184f3624ed813d076cebcb4ce513c81d8669d9" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Error due to subtraction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Consider computing\n", "$$\n", "x = 10^{-8}, \\qquad y = \\sqrt{1+x^2} - 1\n", "$$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = 1.0e-8\n", "y = sqrt(1.0 + x**2) - 1.0\n", "print \"%20.10e\" % y" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " 0.0000000000e+00\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The result is zero due to round-off error. An equivalent expression is\n", "$$\n", "y = \\frac{x^2}{\\sqrt{1 + x^2} + 1}\n", "$$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "y = x**2/(sqrt(1.0+x**2) + 1.0)\n", "print \"%20.10e\" % y" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " 5.0000000000e-17\n" ] } ], "prompt_number": 3 } ], "metadata": {} } ] }