Skip to content

Instantly share code, notes, and snippets.

@dhermes
Created April 7, 2023 18:02
Show Gist options
  • Save dhermes/42420ad20ef735f94a5ca1c2a8e82bd2 to your computer and use it in GitHub Desktop.
Save dhermes/42420ad20ef735f94a5ca1c2a8e82bd2 to your computer and use it in GitHub Desktop.
[2023-04-07] Understanding float64
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "8a35baed",
"metadata": {},
"outputs": [],
"source": [
"import struct\n",
"\n",
"import numpy as np\n",
"\n",
"\n",
"_ = np.seterr(divide=\"ignore\", invalid=\"ignore\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9b7cead3",
"metadata": {},
"outputs": [],
"source": [
"def show_bits(value):\n",
" float_bytes = struct.pack(\">d\", value)\n",
" float_bits = ''.join([f\"{digit:08b}\" for digit in float_bytes])\n",
" switches = float_bits.replace(\"0\", \"-\").replace(\"1\", \"+\")\n",
" return switches[0] + \"|\" + switches[1:12] + \"|\" + switches[12:]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f365a06e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'-|-++++++++++|----------------------------------------------------'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(1.0)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "7f94a9d2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'-|-++++++++++|+---------------------------------------------------'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(1.5)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a2ece44e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'-|-++++++++++|++--------------------------------------------------'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(1.75)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a0b6f8d9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'-|+----------|----------------------------------------------------'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(2.0)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "dc6e7a47",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'-|+---------+|----------------------------------------------------'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(4.0)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "f3def30e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'-|+--------+-|----------------------------------------------------'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(8.0)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "546074bc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'-|+--------++|----------------------------------------------------'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(16.0)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "b6d7db70",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'-|+-------+--|----------------------------------------------------'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(32.0)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "c11e9dd7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'-|+-------+-+|----------------------------------------------------'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(64.0)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "32a6b237",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'-|+----------|+---------------------------------------------------'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(3.0)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "2e2270a3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'-|+---------+|+---------------------------------------------------'"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(6.0)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "08d72016",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'-|-----------|----------------------------------------------------'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(0.0)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "4323d6e5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'+|-----------|----------------------------------------------------'"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(-0.0)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "438180cf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'-|+++++++++++|----------------------------------------------------'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(float(\"inf\"))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "d039b6fe",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'-|+++++++++++|+---------------------------------------------------'"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(float(\"nan\"))"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "d6d1486e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"nan"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"all_ones, = struct.unpack(\">d\", b\"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff\")\n",
"all_ones"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "a82b4e71",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'+|+++++++++++|++++++++++++++++++++++++++++++++++++++++++++++++++++'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(all_ones)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "4937ec26",
"metadata": {},
"outputs": [
{
"ename": "ZeroDivisionError",
"evalue": "float division by zero",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[20], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;241;43m0.0\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0.0\u001b[39;49m\n",
"\u001b[0;31mZeroDivisionError\u001b[0m: float division by zero"
]
}
],
"source": [
"0.0 / 0.0"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "1ed2b2e4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"nan"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.float64(0.0) / np.float64(0.0)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "ce2d7f6d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"inf"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.float64(1.0) / np.float64(0.0)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "63b60c87",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-inf"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.float64(-1.0) / np.float64(0.0)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "05b28470",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'-|+++++++++++|----------------------------------------------------'"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(float(\"inf\"))"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "374824e5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'+|+++++++++++|----------------------------------------------------'"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"show_bits(float(\"-inf\"))"
]
}
],
"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.11.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment