Skip to content

Instantly share code, notes, and snippets.

@kiwamizamurai
Created December 19, 2019 13:45
Show Gist options
  • Save kiwamizamurai/5e9efc0aecc95d266f628be5d9b5beea to your computer and use it in GitHub Desktop.
Save kiwamizamurai/5e9efc0aecc95d266f628be5d9b5beea to your computer and use it in GitHub Desktop.
floating_point_number.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "floating_point_number.ipynb",
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/kiwamizamurai/5e9efc0aecc95d266f628be5d9b5beea/floating_point_number.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "lA-nT4bzUmWD",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 123
},
"outputId": "9491c774-c455-4c07-bfb7-5ba3c9c2b598"
},
"source": [
"import struct\n",
"\n",
"def floatToBinary32(value):\n",
" return ''.join(f'{c:0>8b}' for c in struct.pack('!f', value))\n",
"\n",
"def binaryToFloat(value):\n",
" hx = hex(int(value, 2)) \n",
" return struct.unpack(\"f\", struct.pack(\"I\", int(hx, 16)))[0]\n",
"\n",
"# float to binary\n",
"fl0 = 0.875\n",
"binstr = floatToBinary32(fl0)\n",
"print(f'Binary equivalent of {fl0}: {binstr}')\n",
"\n",
"# binary to float\n",
"fl1 = binaryToFloat(binstr)\n",
"print(f'Decimal equivalent of {binstr}: {fl1}')\n",
"\n",
"print(f'\\nSign ( 1 bit ) = {binstr[0]}')\n",
"print(f'Exponent ( 8 bits) = {binstr[1:9]}')\n",
"print(f'Mantissa (23 bits) = {binstr[9:]}')\n",
"\n",
"assert fl0 == fl1"
],
"execution_count": 76,
"outputs": [
{
"output_type": "stream",
"text": [
"Binary equivalent of 0.875: 00111111011000000000000000000000\n",
"Decimal equivalent of 00111111011000000000000000000000: 0.875\n",
"\n",
"Sign ( 1 bit ) = 0\n",
"Exponent ( 8 bits) = 01111110\n",
"Mantissa (23 bits) = 11000000000000000000000\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Yj8k0Z0RYcuT",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "5b331a95-abfb-403b-90ff-68c09075d60b"
},
"source": [
"struct.pack('!f', fl0)"
],
"execution_count": 87,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"b'?`\\x00\\x00'"
]
},
"metadata": {
"tags": []
},
"execution_count": 87
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "i3Jub4seYJow",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "70d392f2-bda7-47da-e9c7-2caa33a0c38d"
},
"source": [
"[f'{c:0>8b}' for c in struct.pack('!f', fl0)]"
],
"execution_count": 86,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['00111111', '01100000', '00000000', '00000000']"
]
},
"metadata": {
"tags": []
},
"execution_count": 86
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "QDjCBG3yM-m3",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 52
},
"outputId": "75eca445-4103-4214-fc12-b616bbd74476"
},
"source": [
"value = 128\n",
"print(value.to_bytes(2, 'little'))\n",
"print(value.to_bytes(4, 'little'))"
],
"execution_count": 14,
"outputs": [
{
"output_type": "stream",
"text": [
"b'\\x80\\x00'\n",
"b'\\x80\\x00\\x00\\x00'\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "05KfcHVUNMnc",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "bc574335-6256-4d57-9f52-37c8db98fe13"
},
"source": [
"value.to_bytes(4, 'little').hex()"
],
"execution_count": 15,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'80000000'"
]
},
"metadata": {
"tags": []
},
"execution_count": 15
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "0hnFnVWIPZGB",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 52
},
"outputId": "9614101a-0ebc-4c85-8982-ea94d86f1c56"
},
"source": [
"import struct\n",
"\n",
"value = 0.875\n",
"b = struct.pack(\"<f\", value) # float = 32bit littlendian\n",
"print(b)\n",
"ba = bytearray(b)\n",
"print([\"0x%02x\" % b for b in ba])"
],
"execution_count": 89,
"outputs": [
{
"output_type": "stream",
"text": [
"b'\\x00\\x00`?'\n",
"['0x00', '0x00', '0x60', '0x3f']\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "UCSaG9iuXxmc",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 87
},
"outputId": "c48326fc-77f0-479f-86cb-62c041f1eb97"
},
"source": [
"for i in b:\n",
" print(b)"
],
"execution_count": 90,
"outputs": [
{
"output_type": "stream",
"text": [
"b'\\x00\\x00`?'\n",
"b'\\x00\\x00`?'\n",
"b'\\x00\\x00`?'\n",
"b'\\x00\\x00`?'\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "RfDd2d-uYios",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "b684bef9-4253-407d-aff6-213a6cab9c37"
},
"source": [
"[f'{c:0>8b}' for c in b]"
],
"execution_count": 91,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['00000000', '00000000', '01100000', '00111111']"
]
},
"metadata": {
"tags": []
},
"execution_count": 91
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "3VqCOzDIZic1",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 70
},
"outputId": "d314abc9-20aa-4d6d-b836-b9a8aff15a16"
},
"source": [
"print('{:0>10}'.format('test'))\n",
"print('{:0>5}'.format('test'))\n",
"print('{:1>10}'.format('test'))"
],
"execution_count": 96,
"outputs": [
{
"output_type": "stream",
"text": [
"000000test\n",
"0test\n",
"111111test\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "dwN_KMR6QK4D",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "ff3abfbe-a06d-4d66-ab77-29bc515e208e"
},
"source": [
"import binascii\n",
"\n",
"hex_byte = binascii.hexlify(ba)\n",
"hex_byte"
],
"execution_count": 92,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"b'0000603f'"
]
},
"metadata": {
"tags": []
},
"execution_count": 92
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Rx9-LH1VQ5zQ",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "e403578b-6615-4ee5-c0b4-6e9837961e06"
},
"source": [
"[out] = struct.unpack('f', b)\n",
"out"
],
"execution_count": 93,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0.875"
]
},
"metadata": {
"tags": []
},
"execution_count": 93
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BuSUJzUrZ2SJ",
"colab_type": "text"
},
"source": [
"## Reference\n",
"- https://stackoverflow.com/questions/56941757/how-to-converting-floating-point-number"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment