Skip to content

Instantly share code, notes, and snippets.

@restrepo
Created March 21, 2015 15:05
Show Gist options
  • Save restrepo/53eb9abcf687034df64d to your computer and use it in GitHub Desktop.
Save restrepo/53eb9abcf687034df64d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{"metadata": {"language_info": {"name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "codemirror_mode": {"name": "ipython", "version": 2}, "file_extension": ".py", "mimetype": "text/x-python", "version": "2.7.6"}, "kernelspec": {"name": "python2", "display_name": "Python 2", "language": "python"}}, "cells": [{"cell_type": "markdown", "metadata": {}, "source": "# Rounding problem"}, {"cell_type": "code", "execution_count": 23, "outputs": [], "source": ">>> a=1E-11\n>>> b=11000\n>>> c=-a-b", "metadata": {"collapsed": true, "trusted": true}}, {"cell_type": "markdown", "metadata": {}, "source": "Right result"}, {"cell_type": "code", "execution_count": 24, "outputs": [{"execution_count": 24, "metadata": {}, "data": {"text/plain": "9.060529822707175e-13"}, "output_type": "execute_result"}], "source": ">>> 1.0001*a+(b+c)", "metadata": {"collapsed": false, "trusted": true}}, {"cell_type": "markdown", "metadata": {}, "source": "### Wrong result!"}, {"cell_type": "code", "execution_count": 25, "outputs": [{"execution_count": 25, "metadata": {}, "data": {"text/plain": "0.0"}, "output_type": "execute_result"}], "source": ">>> 1.0001*a+b+c", "metadata": {"collapsed": false, "trusted": true}}, {"cell_type": "markdown", "metadata": {}, "source": "#### Fix de problem:"}, {"cell_type": "code", "execution_count": 26, "outputs": [{"execution_count": 26, "metadata": {}, "data": {"text/plain": "9.060529822707176e-13"}, "output_type": "execute_result"}], "source": ">>> from decimal import Decimal\n>>> float(Decimal(1.0001*a)+Decimal(b)+Decimal(c))", "metadata": {"collapsed": false, "trusted": true}}, {"cell_type": "code", "execution_count": null, "outputs": [], "source": "", "metadata": {"collapsed": true, "trusted": true}}], "nbformat_minor": 0, "nbformat": 4}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment