{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "$$\n", "\\def\\CC{\\bf C}\n", "\\def\\QQ{\\bf Q}\n", "\\def\\RR{\\bf R}\n", "\\def\\ZZ{\\bf Z}\n", "\\def\\NN{\\bf N}\n", "$$\n", "# Exact and float point computations - Stability and unstability in dynamics\n", "\n", "Authors \n", "- Vincent Delecroix\n", "\n", "License \n", "CC BY-SA 3.0\n", "\n", "## Exact computations and floating point approximations\n", "\n", "There are a lot of different numbers in Sage. You will need to choose\n", "which kind of numbers you want to use depending on your computations. In\n", "this worksheet we will consider four types of numbers:\n", "\n", "- rationals\n", "- floating point numbers\n", "- algebraic numbers\n", "\n", "If you are interested in all possible ways of representing a real number\n", "in Sage, you can have a look at the tutorial \"Real and complex numbers\n", "in Sage\".\n", "\n", "### Floating point and rationals\n", "\n", "To create an integer or a rational number, you just write it as you\n", "would do on a sheet of paper" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2 + 3^5 # an integer" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "23 / 45 # a rational number" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To create a floating point number, you need to add a dot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "1.0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3.25 + 22.18" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Contrarily to integers and rationals, a floating point number has\n", "limited precision" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2^100 + 2^10 - 2^100" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "2.0^100.0 + 2.0^10.0 - 2.0^100.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because floating point numbers have limited precision they are much\n", "faster. In the following we will see that the the bad choice of numbers\n", "influence the computational time and the correctness.\n", "\n", "### Maps of the interval: fixed points and iteration\n", "\n", "Let us consider the map $f_4(x) = 4 x (1 - x)$ from the interval $[0,1]$\n", "to itself. Prove that $f_4$ is surjective and plot it" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Show that $f_4(3/4) = 3/4$ (in other words, the point $3/4$ is a fixed\n", "point of $f_4$). What do you expect from the following two commands (you\n", "have to guess whether the answer will be $True$ or $False$ and you can\n", "then check your answer by executing the cell):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = 3.0 / 4.0\n", "4 * s * (1 - s) == s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let $f_{7/2}(x) = 7/2 x (1 - x)$. Prove that $5/7$ is a fixed point\n", "of $f_{7/2}$. Is the following $True$ or $False$ :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = 5.0 / 7.0\n", "7.0 / 2.0 * s * (1.0 - s) == s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Perform the same two computations as above with rational numbers instead\n", "of floating point." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# edit here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On a computer a floating point number is a number of the form $m\\, 2^n$\n", "where $m$ (the mantissa) and $n$ (the exponent) have some fixed bounds.\n", "In particular, floating point numbers have *finite* precision.\n", "Computations with floating points numbers are inaccurate but very\n", "efficient.\n", "\n", "Compare the following computation with rationals:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = 1\n", "for i in range(10):\n", " s = (s + 2/s) / 2\n", "print s\n", "print s.numerical_approx()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and the same computation with floating point numbers:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = 1.0\n", "for i in range(10):\n", " s = (s + 2.0 / s) / 2.0\n", "print s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What can you say?\n", "\n", "Now compare the following computation with rationals:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = 5 / 7\n", "for i in range(100):\n", " s = 7 / 2 * s * (1 - s)\n", "print s\n", "print s.numerical_approx()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and the same computation with floating point numbers:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = 5.0 / 7.0\n", "for i in range(100):\n", " s = 7.0 / 2.0 * s * (1.0 - s)\n", "print s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What can you say? What is the difference between the two cases we\n", "considered above? What makes the functions $s \\mapsto (s + 2/s) / 2$ and\n", "$s \\mapsto 7/2 s (1 - s)$ different?" ] } ], "metadata": { "kernelspec": { "display_name": "sagemath", "name": "sagemath" } }, "nbformat": 4, "nbformat_minor": 2 }