Last active
April 19, 2022 17:45
-
-
Save sanchezcarlosjr/de302811a791735664a65b150534571a to your computer and use it in GitHub Desktop.
1.2.1 Round_off_Errors_and_Computer_arithmetic Decimal Machine numbers.ipynb
This file contains 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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "1.2.1 Round_off_Errors_and_Computer_arithmetic Decimal Machine numbers.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/sanchezcarlosjr/de302811a791735664a65b150534571a/1_2_1_round_off_errors_and_computer_arithmetic.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"## 1.2 Round off Errors and computer arithmetic\n", | |
"### Decimal machine numbers\n", | |
"By [Carlos Eduardo Sanchez Torres](https://twitter.com/CharllierJr)\n", | |
"\n", | |
"[Numerical analysis](https://www.notion.so/sanchezcarlosjr/Numerical-analysis-f774bbfddd834cf1beffda5e9e935ff8)" | |
], | |
"metadata": { | |
"id": "ZAeQx02e01UW" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "fK2Jt8gr0kwg" | |
}, | |
"outputs": [], | |
"source": [ | |
"import math\n", | |
"def floor(x):\n", | |
" t = int(x)\n", | |
" return t if x>=0 or x==t else t-1\n", | |
"\n", | |
"def abs(x):\n", | |
" if x >= 0:\n", | |
" return x\n", | |
" return -x \n", | |
"\n", | |
"def length(x):\n", | |
" if x == 0:\n", | |
" return 1\n", | |
" return floor(math.log10(abs(x))) + 1\n", | |
"\n", | |
"\n", | |
"# Chopping or truncate\n", | |
"def truncate(x,n):\n", | |
" sign = 1 if x >= 0 else -1\n", | |
" return sign*floor(abs(x)*10**n)/(10**n)\n", | |
"\n", | |
"def z(x,n):\n", | |
" return floor(abs(x)/10**n) % 10\n", | |
"\n", | |
"def roundingA(x,k,n):\n", | |
" sign = 1 if x >= 0 else -1\n", | |
" return sign*truncate(abs(x)+5*10**(n-(k+2)),k), n\n", | |
"\n", | |
"def roundingB(x,k):\n", | |
" s = 1*10**(x.n-(k+1)) if z(x,-(k+1)) >= 5 else 0\n", | |
" sign = 1 if x >= 0 else -1\n", | |
" return sign*truncate(abs(x)+s,k)\n", | |
"\n", | |
"def absolute_error(p, p2):\n", | |
" return abs(p-p2)\n", | |
"\n", | |
"def relative_error(p, p2):\n", | |
" return absolute_error(p,p2)/abs(p)\n", | |
"\n", | |
"\n", | |
"def normalize(y):\n", | |
" return (y*10**-length(y), length(y))\n", | |
"\n", | |
"\n", | |
"digits_to_rouding=-1\n", | |
"class fl(float):\n", | |
" def __new__(cls, x=0):\n", | |
" cls.y=x\n", | |
" x,cls.n=normalize(x)\n", | |
" if digits_to_rouding > -1:\n", | |
" x = roundingA(x, digits_to_rouding, cls.n)\n", | |
" cls.digits = digits_to_rouding\n", | |
" return super().__new__(cls,x)\n", | |
" \n", | |
" def __repr__(cls):\n", | |
" t=f\"{cls.real}\"\n", | |
" zeros=\"\"\n", | |
" if hasattr(cls, 'digits'):\n", | |
" n=cls.digits - len(t.split(\".\")[1])\n", | |
" zeros = \"0\"*n if n > 0 else \"\"\n", | |
" return f\"{t}{zeros}x10^{cls.n}\"\n", | |
" \n", | |
" def __add__(cls,fl2):\n", | |
" if not isinstance(fl2, fl):\n", | |
" return cls.y+fl2\n", | |
" return cls.y+fl2.y\n", | |
" \n", | |
" def __mul__(cls,fl2):\n", | |
" if not isinstance(fl2, fl):\n", | |
" return fl(cls.y*fl2)\n", | |
" return fl(cls.y*fl2.y)\n", | |
" \n", | |
" def __truediv__(cls,fl2):\n", | |
" return fl(cls.y/fl2.y)\n", | |
" \n", | |
" def __sub__(cls,fl2):\n", | |
" return fl(cls.y-fl2.y)\n", | |
" \n", | |
" def truncate(cls,n):\n", | |
" return truncate(cls, n)\n", | |
" \n", | |
" def roundingA(cls,n):\n", | |
" return fl(roundingA(cls, n-1, cls.n))\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"assert normalize(1) == (0.1, 1)\n", | |
"assert 0.11000000000000000 <= normalize(1.1)[0] <= 0.11000000000000001" | |
], | |
"metadata": { | |
"id": "7Re9xFe_EEFf" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"assert z(24.19,0) == 4\n", | |
"assert z(24.19,1) == 2\n", | |
"assert z(24.19,-1) == 1\n", | |
"assert z(24.19,-2) == 9\n", | |
"assert z(-24.19,0) == 4\n", | |
"assert z(-24.19,1) == 2\n", | |
"assert z(-24.19,-1) == 1\n", | |
"assert z(-24.19,-2) == 9" | |
], | |
"metadata": { | |
"id": "4A1PWpye7NQY" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"assert fl(25.51).__repr__() == \"0.2551x10^2\"\n", | |
"assert fl(21.51).__repr__() == \"0.2151x10^2\"\n", | |
"assert fl(0.2).__repr__() == \"0.2x10^0\"\n", | |
"assert fl(0.02).__repr__() == \"0.2x10^-1\"" | |
], | |
"metadata": { | |
"id": "wdwf5FaLG5BU" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"e=math.e\n", | |
"assert truncate(e,6) == 2.718281" | |
], | |
"metadata": { | |
"id": "AiQ1vcSW_TeW" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"e=math.e\n", | |
"#assert truncate(fl(e).real,6) == 0.271828\n", | |
"#assert roundingA(fl(e),6) == 0.271828\n", | |
"#assert fl(e).truncate(6) == 0.271828\n", | |
"#assert fl(e).truncate(6).__repr__() == \"0.271828x10^1\"\n", | |
"assert fl(e).roundingA(6).__repr__() == \"0.271828x10^1\"" | |
], | |
"metadata": { | |
"id": "krhVhGVK0-YT" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"phi=(1+math.sqrt(5))/2 # Golden Ratio\n", | |
"#assert truncate(fl(phi),6) == 0.161803\n", | |
"#assert fl(phi).truncate(6) == 0.161803\n", | |
"#assert roundingA(fl(phi),6) == 0.161803\n", | |
"#assert fl(phi).roundingA(6) == 0.161803\n", | |
"#assert fl(-phi).truncate(6) == -0.161803\n", | |
"#assert fl(-phi).roundingA(6) == -0.161803\n", | |
"digits_to_rouding=6\n", | |
"assert fl(phi).__repr__() == \"0.161803x10^1\"\n", | |
"assert fl(-phi).__repr__() == \"-0.161803x10^1\"" | |
], | |
"metadata": { | |
"id": "Nyk8Kzey2KMc" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"pi=math.pi\n", | |
"#assert truncate(fl(pi), 5) == 0.31415\n", | |
"digits_to_rouding=5\n", | |
"assert fl(pi) == 0.31416" | |
], | |
"metadata": { | |
"id": "-P5MHIjfFnvL" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"#assert truncate(fl(-0.12), 1) == -0.1\n", | |
"digits_to_rouding=1\n", | |
"assert fl(-0.12) == -0.1" | |
], | |
"metadata": { | |
"id": "kNErZAT3Rzis" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"assert math.floor(1.5) == floor(1.5)\n", | |
"assert math.floor(1.9999) == floor(1.9999)\n", | |
"assert math.floor(1.9999) == floor(1.9999)\n", | |
"assert math.floor(0) == floor(0)\n", | |
"assert math.floor(-1) == floor(-1)\n", | |
"assert math.floor(1) == floor(1)\n", | |
"assert math.floor(-1.1) == floor(-1.1)" | |
], | |
"metadata": { | |
"id": "ORsktEjirEhU" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"digits_to_rouding=-1\n", | |
"assert fl() == 0\n", | |
"assert fl(1.7) == 0.17\n", | |
"assert fl(0.17) == 0.17\n", | |
"assert fl(0.017) == 0.17\n", | |
"assert fl(0) == 0\n", | |
"assert fl(-1.7) == -0.17" | |
], | |
"metadata": { | |
"id": "OvSvNHE4HwTD" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"digits_to_rouding=-1\n", | |
"assert fl(1.7).__repr__() == \"0.17x10^1\"\n", | |
"assert fl(0).__repr__() == \"0.0x10^1\"\n", | |
"assert fl(-1.7).__repr__() == \"-0.17x10^1\"" | |
], | |
"metadata": { | |
"id": "GXOiU--FLkiy" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"digits_to_rouding=-1\n", | |
"fl(5/7)" | |
], | |
"metadata": { | |
"id": "6YT85HIRoed5", | |
"outputId": "5ea9792a-29b0-4be8-c145-a85a5e73bbaa", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.7142857142857143x10^0" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 35 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"(5/7)+(1/3)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "jrM3W7xoVvW0", | |
"outputId": "97e5ee3b-6c18-414f-dcb0-f20e1d860540" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"1.0476190476190477" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 36 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"roundingA(normalize(normalize(5/7)[0]+normalize(1/3)[0])[0], 3, 1)" | |
], | |
"metadata": { | |
"id": "888uzjCpquJ5", | |
"outputId": "919de997-c7b7-4a5a-dfd9-2cc708eb4f93", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(0.105, 1)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 52 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"normalize(normalize(5/7)[0]+normalize(1/3)[0])" | |
], | |
"metadata": { | |
"id": "Y1kqhxUzZ34e", | |
"outputId": "423b967c-f208-4501-c774-1729e58c59cf", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(0.10476190476190478, 1)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 48 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"digits_to_truncate=5\n", | |
"assert (fl(5/7)-fl(1/3)).display() == \"0.38095x10^0\"" | |
], | |
"metadata": { | |
"id": "wrRPXGc4tNxp" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"digits_to_truncate=5\n", | |
"fl(5/7)*fl(1/3)" | |
], | |
"metadata": { | |
"id": "BxDU3uGvtijL", | |
"outputId": "379b1509-cb48-43b2-9f4b-9100ff40f6fa", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.1x10^1" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 11 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"digits_to_truncate=5\n", | |
"fl(5/7)/fl(1/3)" | |
], | |
"metadata": { | |
"id": "CKnWEmXXuVUT", | |
"outputId": "7c3e3182-a8f7-44b9-87c9-45659eb1c0d0", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"0.7142857142857143\n", | |
"0.3333333333333333\n", | |
"1.0\n" | |
] | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.1x10^1" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 3 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"digits_to_truncate=4\n", | |
"p=fl(3)\n", | |
"p2=fl(3.1)\n", | |
"p3=fl(0.00031)" | |
], | |
"metadata": { | |
"id": "A5Yd1Mig_VAU" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"assert fl(absolute_error(0.3*10,0.31*10)) == fl(0.1)\n", | |
"assert fl(absolute_error(0.3*10**-3,0.31*10**-3)) == fl(0.1*10**-4)\n", | |
"assert fl(absolute_error(0.3*10**4,0.31*10**4)) == fl(0.1*10**3)" | |
], | |
"metadata": { | |
"id": "0RsDcBjLGCa-" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"assert fl(relative_error(0.3*10,0.31*10)) == fl(0.1)\n", | |
"assert fl(relative_error(0.3*10**-3,0.31*10**-3)) == fl(0.1*10**-4)\n", | |
"assert fl(relative_error(0.3*10**4,0.31*10**4)) == fl(0.1*10**3)" | |
], | |
"metadata": { | |
"id": "KldqGgDLK3pM" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"relative_error(0.01,0.01)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "yS9Lk1mPQFF3", | |
"outputId": "80692ca6-0310-43d7-f87d-65194731edd8" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.0" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 161 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"relative_error(1,0.9)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "WjT7LmcjX6Fn", | |
"outputId": "45beea35-ebcd-4f2c-ffe4-bb6d5bc8944e" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"9.999999999999998" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 168 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"relative_error(1,0.9)" | |
], | |
"metadata": { | |
"id": "fQiUN8HjZGsh" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"roundingA(normalize(normalize(5/7)[0]+normalize(1/3)[0])[0], 3, 1)" | |
], | |
"metadata": { | |
"id": "UdkqdYFIbvVH" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from sympy import *\n", | |
"x=N(1/3, 5)\n", | |
"x" | |
], | |
"metadata": { | |
"id": "CnNhUh27cmzd", | |
"outputId": "ad6cd79a-1a8a-4e90-fb2f-2cd89b051e8f", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 38 | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.33333" | |
], | |
"text/latex": "$\\displaystyle 0.33333$" | |
}, | |
"metadata": {}, | |
"execution_count": 5 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"y=N(5/7, 5)\n", | |
"y" | |
], | |
"metadata": { | |
"id": "TyALkVdtdPgU", | |
"outputId": "3db915cb-5a47-49b5-ae79-1218913e0432", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 38 | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.71429" | |
], | |
"text/latex": "$\\displaystyle 0.71429$" | |
}, | |
"metadata": {}, | |
"execution_count": 60 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"normalize(N(x+y, 5))" | |
], | |
"metadata": { | |
"id": "XihctYsddR28", | |
"outputId": "4bfd1c43-1953-4a85-ef14-74fbd8f9a2f3", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(0.10476, 1)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 63 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"normalize(N(y-x, 5))" | |
], | |
"metadata": { | |
"id": "mG0knpNMddlO", | |
"outputId": "99f35909-1686-4b2c-bb06-efd5e43e50d1", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(0.38095, 0)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 65 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"normalize(N(x*y, 5))" | |
], | |
"metadata": { | |
"id": "tFGTB3kFdd6b", | |
"outputId": "8b55d433-f8d6-44f9-cae7-4efd0538b096", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(0.23810, 0)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 66 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"normalize(N(y/x, 5))" | |
], | |
"metadata": { | |
"id": "hcu5AZvjddvf", | |
"outputId": "8291be14-de93-4dd5-83bb-e849ef67d85a", | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(0.21429, 1)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 70 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"x=0.1\n", | |
"digits_to_rouding=4" | |
], | |
"metadata": { | |
"id": "X4PIiItejSXj" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"def denormalize(y):\n", | |
" return y[0] * 10**y[1]" | |
], | |
"metadata": { | |
"id": "YEdyytOunhcR" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"normalize(N(x, digits_to_rouding))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "nlfCqUHqje6m", | |
"outputId": "9672fe0e-5433-4d5a-d223-caac5c989967" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(0.1000, 0)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 9 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"normalize(N(cos(0.1),digits_to_rouding))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "haj3ksA9kDIP", | |
"outputId": "69024d7a-1f86-4666-ea77-c96b3b1b416e" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(0.9950, 0)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 12 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"normalize(N(sin(0.1),digits_to_rouding))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "NeLdI60QlQOL", | |
"outputId": "1268f34c-0916-43a4-8099-fa65fd381f2c" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(0.9983, -1)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 13 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"normalize(N(x*0.9950, digits_to_rouding))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "lAFLnIe8liPq", | |
"outputId": "b76fe09a-3c8a-41bf-d836-7d151127f71a" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(0.9950, -1)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 15 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"x-sin(x)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 38 | |
}, | |
"id": "IwxUXsevob1x", | |
"outputId": "f51a8112-ff0f-48a4-af87-7d6903f1d5e7" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.000166583353171851" | |
], | |
"text/latex": "$\\displaystyle 0.000166583353171851$" | |
}, | |
"metadata": {}, | |
"execution_count": 26 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"normalize(N(N(x,digits_to_rouding)-N(sin(x),digits_to_rouding), digits_to_rouding))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "Acxl8lbemuug", | |
"outputId": "f9df9a97-066e-4760-f719-afa48bbc4b47" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(0.1669, -3)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 18 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"denormalize((0.9950, -1))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "emPOipvxn9BC", | |
"outputId": "7bf0f2fb-d963-4aca-c56e-c36eca8b21e9" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.0995" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 23 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"denormalize((0.9983, -1))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "1ZFWssjmn_yS", | |
"outputId": "d37f1a55-aa95-4d0a-b86b-c7e2213a883e" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.09983" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 24 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"0.1-0.09983 " | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "RRuNqK2JoCxA", | |
"outputId": "de9fc9f9-441e-46df-f778-c11a227e620f" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.00017000000000000348" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 80 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"normalize(N(denormalize((0.9950, -1))-denormalize((0.9983, -1)), digits_to_rouding))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "eqqaYdNCnUB0", | |
"outputId": "b67de1dd-6495-42db-dae3-23694de1b6fa" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(-0.3300, -3)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 21 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"normalize(N(denormalize((-0.3300, -3))/denormalize((0.1669, -3)), digits_to_rouding))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "woUQFWcVo59r", | |
"outputId": "3dcc0767-979b-41e6-957a-0754dfd25fe4" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(-0.1977, 1)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 27 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"x=0.1\n", | |
"normalize(N(x*x, digits_to_rouding))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "q1neIBzqsFsa", | |
"outputId": "c046df39-d6e8-4c20-b5a4-a2c424b866ac" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(1.000, -2)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 33 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"N(x*x, digits_to_rouding)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 38 | |
}, | |
"id": "SMLIG2WJvbbS", | |
"outputId": "b06e4a5a-b11a-48c4-f7fa-a5f0da7f37d9" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.01000" | |
], | |
"text/latex": "$\\displaystyle 0.01$" | |
}, | |
"metadata": {}, | |
"execution_count": 36 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"normalize(0.1**3)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "bYZQp4mzvXDL", | |
"outputId": "d0bd2dfb-7f79-41fc-9444-3ac986fe13e6" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(0.100000000000000, -2)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 55 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"denormalize(normalize(0.1*.1))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 38 | |
}, | |
"id": "LrruH8KPyx1A", | |
"outputId": "d75f772e-c657-442a-c41c-b161ab8b0f27" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.0100000000000000" | |
], | |
"text/latex": "$\\displaystyle 0.01$" | |
}, | |
"metadata": {}, | |
"execution_count": 49 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"1-denormalize(normalize(0.01/2))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 38 | |
}, | |
"id": "Gl7WekjVwkRP", | |
"outputId": "6d0f5c53-1759-4616-da27-01927896e5ee" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.995000000000000" | |
], | |
"text/latex": "$\\displaystyle 0.995$" | |
}, | |
"metadata": {}, | |
"execution_count": 53 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"x=0.001\n", | |
"normalize(N(x/6, digits_to_rouding))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "ZAdM8md3xvOX", | |
"outputId": "5c2e8c05-ceab-44b0-9b18-c3e67dc47850" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(0.1667, -3)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 63 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"denormalize((0.1667, -3))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "sr2Aa9AA1wAG", | |
"outputId": "76981151-c1d8-4b14-fd07-eccd820a7889" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.00016669999999999999" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 64 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"normalize(N(0.1-0.0001667, digits_to_rouding))" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "pm_xRwpq15WP", | |
"outputId": "d7c5d880-57ef-453a-db2f-332a608a58b2" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"(0.9983, -1)" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 68 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"0.1-(0.1**3)/6" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "XwfCESK-2OUO", | |
"outputId": "f559200e-a08f-4a17-af30-7ad3320bd7df" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.09983333333333334" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 69 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"N(0.1-0.09983, digits_to_rouding)" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 38 | |
}, | |
"id": "avSY2UUL22Pr", | |
"outputId": "8d87d4da-a1c9-40cd-b7d5-42a875a76c88" | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"0.0001700" | |
], | |
"text/latex": "$\\displaystyle 0.00017$" | |
}, | |
"metadata": {}, | |
"execution_count": 79 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"relative_error(,-0.1977*10**1)" | |
], | |
"metadata": { | |
"id": "UI_RTlaaIPNy", | |
"outputId": "80fbf5b9-9bcd-4d73-e7ec-0ddfb1687d22", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 177 | |
} | |
}, | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "error", | |
"ename": "TypeError", | |
"evalue": "ignored", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-82-61d734aa03b8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mrelative_error\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m0.1977\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mTypeError\u001b[0m: relative_error() missing 1 required positional argument: 'p2'" | |
] | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment