{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "n = 10**4\n", "x = np.abs(np.random.rand(n).astype(np.float32))\n", "\n", "sum0 = np.sum(x)\n", "\n", "b = x.astype(np.float64)\n", "sum1 = np.sum(b)\n", "\n", "# https://en.wikipedia.org/wiki/Kahan_summation_algorithm\n", "sum2 = x[0]\n", "c = np.single(0.0)\n", "for i in range(1,n):\n", " y = x[i] - c # assume c is zero.\n", " t = sum2 + y # sum2 is big, y is small\n", " c = (t - sum2) - y # c should still be zero--but isn't. Why?\n", " sum2 = t\n", "\n", "# large to smallest\n", "d = np.sort(x)\n", "d = d[::-1]\n", "sum3 = d.sum()\n", "\n", "# smallest to largest\n", "e = np.sort(x)\n", "sum4 = e.sum()\n", "\n", "print \"single = %.20g\"%sum0\n", "print \"double = %.20g\"%sum1\n", "print \"Kahan = %.20g\"%sum2\n", "print \"large to small = %.20g\"%sum3\n", "print \"small to large = %.20g\"%sum4" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "single = 4993.818359375\n", "double = 4993.8146699111312046\n", "Kahan = 4993.814453125\n", "large to small = 4993.80126953125\n", "small to large = 4993.81787109375\n" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Which of these is going to do well, which poorly?" ] } ], "metadata": {} } ] }