Skip to content

Commit

Permalink
Update.
Browse files Browse the repository at this point in the history
  • Loading branch information
pipme committed Sep 26, 2024
1 parent 4defc32 commit cabc198
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 4 deletions.
2 changes: 1 addition & 1 deletion _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ format:
html:
theme:
light: cosmo
dark: darkly
dark: darkly # https://github.com/quarto-dev/quarto-cli/issues/6741
css: styles.css
2 changes: 1 addition & 1 deletion blog+gist.qmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "All blogs and gists."
listing:
contents: "posts/**/*.qmd"
contents: ["posts/**/*.qmd", "posts/**/*.ipynb"]
sort: "date desc"
fields: [date, title, reading-time]
type: table
Expand Down
2 changes: 1 addition & 1 deletion gist.qmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Gist collection."
listing:
contents: "posts/**/*gist.qmd"
contents: ["posts/**/*gist.qmd", "posts/**/*gist.ipynb"]
sort: "date desc"
fields: [date, title, reading-time]
type: table
Expand Down
2 changes: 1 addition & 1 deletion index.qmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Blogs"
listing:
contents: posts/**/*[!gist].qmd
contents: ["posts/**/*[!gist].qmd", "posts/**/*[!gist].ipynb"]
sort: "date desc"
type: default
categories: true
Expand Down
159 changes: 159 additions & 0 deletions posts/2024-09-26-Float64/index-gist.ipynb
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
}

0 comments on commit cabc198

Please sign in to comment.