|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "raw", |
| 5 | + "id": "6ef206bd", |
| 6 | + "metadata": { |
| 7 | + "vscode": { |
| 8 | + "languageId": "raw" |
| 9 | + } |
| 10 | + }, |
| 11 | + "source": [ |
| 12 | + "---\n", |
| 13 | + "title: \"`exp(x)` overflow/underflow in Numpy (float64)\"\n", |
| 14 | + "date: \"2024-09-26\"\n", |
| 15 | + "date-modified: last-modified\n", |
| 16 | + "categories: [Numpy, Python]\n", |
| 17 | + "format:\n", |
| 18 | + " html: default\n", |
| 19 | + "execute: \n", |
| 20 | + " enabled: true\n", |
| 21 | + "---" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": 1, |
| 27 | + "id": "65950641", |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [ |
| 30 | + { |
| 31 | + "name": "stdout", |
| 32 | + "output_type": "stream", |
| 33 | + "text": [ |
| 34 | + "Numpy float info: Machine parameters for float64\n", |
| 35 | + "---------------------------------------------------------------\n", |
| 36 | + "precision = 15 resolution = 1.0000000000000001e-15\n", |
| 37 | + "machep = -52 eps = 2.2204460492503131e-16\n", |
| 38 | + "negep = -53 epsneg = 1.1102230246251565e-16\n", |
| 39 | + "minexp = -1022 tiny = 2.2250738585072014e-308\n", |
| 40 | + "maxexp = 1024 max = 1.7976931348623157e+308\n", |
| 41 | + "nexp = 11 min = -max\n", |
| 42 | + "smallest_normal = 2.2250738585072014e-308 smallest_subnormal = 4.9406564584124654e-324\n", |
| 43 | + "---------------------------------------------------------------\n", |
| 44 | + "\n" |
| 45 | + ] |
| 46 | + } |
| 47 | + ], |
| 48 | + "source": [ |
| 49 | + "# | code-fold: true\n", |
| 50 | + "import numpy as np\n", |
| 51 | + "\n", |
| 52 | + "np.seterr(all=\"ignore\")\n", |
| 53 | + "print(\"Numpy float info:\", np.finfo(float))" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": 2, |
| 59 | + "id": "a2453c9d", |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [], |
| 62 | + "source": [ |
| 63 | + "# | code-fold: true\n", |
| 64 | + "\n", |
| 65 | + "# Binary search: find the minimal integer x such that np.exp(x) is not zero\n", |
| 66 | + "def find_minimal_exp_x():\n", |
| 67 | + " left = -10000\n", |
| 68 | + " right = 0\n", |
| 69 | + " while right - left > 1:\n", |
| 70 | + " mid = (left + right) // 2\n", |
| 71 | + " if np.exp(mid) == 0:\n", |
| 72 | + " left = mid\n", |
| 73 | + " else:\n", |
| 74 | + " right = mid\n", |
| 75 | + " return right\n", |
| 76 | + "\n", |
| 77 | + "\n", |
| 78 | + "min_int = find_minimal_exp_x()\n", |
| 79 | + "\n", |
| 80 | + "\n", |
| 81 | + "# Binary search: find the maximal integer x such that np.exp(x) is not infinity\n", |
| 82 | + "def find_maximal_exp_x():\n", |
| 83 | + " left = 0\n", |
| 84 | + " right = 10000\n", |
| 85 | + " while right - left > 1:\n", |
| 86 | + " mid = (left + right) // 2\n", |
| 87 | + " if np.exp(mid) != np.inf:\n", |
| 88 | + " left = mid\n", |
| 89 | + " else:\n", |
| 90 | + " right = mid\n", |
| 91 | + " return right\n", |
| 92 | + "\n", |
| 93 | + "\n", |
| 94 | + "max_int = find_maximal_exp_x()" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": 3, |
| 100 | + "id": "728c922f", |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [ |
| 103 | + { |
| 104 | + "name": "stdout", |
| 105 | + "output_type": "stream", |
| 106 | + "text": [ |
| 107 | + "np.exp(-746) = 0.0\n", |
| 108 | + "np.exp(-745) = 5e-324\n" |
| 109 | + ] |
| 110 | + } |
| 111 | + ], |
| 112 | + "source": [ |
| 113 | + "print(f\"np.exp({min_int - 1}) = {np.exp(min_int - 1)}\")\n", |
| 114 | + "print(f\"np.exp({min_int}) = {np.exp(min_int)}\")" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "code", |
| 119 | + "execution_count": 4, |
| 120 | + "id": "0b9d06a5", |
| 121 | + "metadata": {}, |
| 122 | + "outputs": [ |
| 123 | + { |
| 124 | + "name": "stdout", |
| 125 | + "output_type": "stream", |
| 126 | + "text": [ |
| 127 | + "np.exp(709) = 8.218407461554972e+307\n", |
| 128 | + "np.exp(710) = inf\n" |
| 129 | + ] |
| 130 | + } |
| 131 | + ], |
| 132 | + "source": [ |
| 133 | + "print(f\"np.exp({max_int - 1}) = {np.exp(max_int - 1)}\")\n", |
| 134 | + "print(f\"np.exp({max_int}) = {np.exp(max_int)}\")" |
| 135 | + ] |
| 136 | + } |
| 137 | + ], |
| 138 | + "metadata": { |
| 139 | + "kernelspec": { |
| 140 | + "display_name": "Python 3 (ipykernel)", |
| 141 | + "language": "python", |
| 142 | + "name": "python3" |
| 143 | + }, |
| 144 | + "language_info": { |
| 145 | + "codemirror_mode": { |
| 146 | + "name": "ipython", |
| 147 | + "version": 3 |
| 148 | + }, |
| 149 | + "file_extension": ".py", |
| 150 | + "mimetype": "text/x-python", |
| 151 | + "name": "python", |
| 152 | + "nbconvert_exporter": "python", |
| 153 | + "pygments_lexer": "ipython3", |
| 154 | + "version": "3.9.13" |
| 155 | + } |
| 156 | + }, |
| 157 | + "nbformat": 4, |
| 158 | + "nbformat_minor": 5 |
| 159 | +} |
0 commit comments