-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
163 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "raw", | ||
"id": "6ef206bd", | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "raw" | ||
} | ||
}, | ||
"source": [ | ||
"---\n", | ||
"title: \"`exp(x)` overflow/underflow in Numpy (float64)\"\n", | ||
"date: \"2024-09-26\"\n", | ||
"date-modified: last-modified\n", | ||
"categories: [Numpy, Python]\n", | ||
"format:\n", | ||
" html: default\n", | ||
"execute: \n", | ||
" enabled: true\n", | ||
"---" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "65950641", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Numpy float info: Machine parameters for float64\n", | ||
"---------------------------------------------------------------\n", | ||
"precision = 15 resolution = 1.0000000000000001e-15\n", | ||
"machep = -52 eps = 2.2204460492503131e-16\n", | ||
"negep = -53 epsneg = 1.1102230246251565e-16\n", | ||
"minexp = -1022 tiny = 2.2250738585072014e-308\n", | ||
"maxexp = 1024 max = 1.7976931348623157e+308\n", | ||
"nexp = 11 min = -max\n", | ||
"smallest_normal = 2.2250738585072014e-308 smallest_subnormal = 4.9406564584124654e-324\n", | ||
"---------------------------------------------------------------\n", | ||
"\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# | code-fold: true\n", | ||
"import numpy as np\n", | ||
"\n", | ||
"np.seterr(all=\"ignore\")\n", | ||
"print(\"Numpy float info:\", np.finfo(float))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "a2453c9d", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# | code-fold: true\n", | ||
"\n", | ||
"# Binary search: find the minimal integer x such that np.exp(x) is not zero\n", | ||
"def find_minimal_exp_x():\n", | ||
" left = -10000\n", | ||
" right = 0\n", | ||
" while right - left > 1:\n", | ||
" mid = (left + right) // 2\n", | ||
" if np.exp(mid) == 0:\n", | ||
" left = mid\n", | ||
" else:\n", | ||
" right = mid\n", | ||
" return right\n", | ||
"\n", | ||
"\n", | ||
"min_int = find_minimal_exp_x()\n", | ||
"\n", | ||
"\n", | ||
"# Binary search: find the maximal integer x such that np.exp(x) is not infinity\n", | ||
"def find_maximal_exp_x():\n", | ||
" left = 0\n", | ||
" right = 10000\n", | ||
" while right - left > 1:\n", | ||
" mid = (left + right) // 2\n", | ||
" if np.exp(mid) != np.inf:\n", | ||
" left = mid\n", | ||
" else:\n", | ||
" right = mid\n", | ||
" return right\n", | ||
"\n", | ||
"\n", | ||
"max_int = find_maximal_exp_x()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "728c922f", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"np.exp(-746) = 0.0\n", | ||
"np.exp(-745) = 5e-324\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(f\"np.exp({min_int - 1}) = {np.exp(min_int - 1)}\")\n", | ||
"print(f\"np.exp({min_int}) = {np.exp(min_int)}\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "0b9d06a5", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"np.exp(709) = 8.218407461554972e+307\n", | ||
"np.exp(710) = inf\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(f\"np.exp({max_int - 1}) = {np.exp(max_int - 1)}\")\n", | ||
"print(f\"np.exp({max_int}) = {np.exp(max_int)}\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |