Created
March 21, 2015 15:05
-
-
Save restrepo/53eb9abcf687034df64d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {"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