{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "# GFloat Basics\n", "\n", "This notebook shows the use of `decode_float` to explore properties of some float formats.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Install packages\n", "from pandas import DataFrame\n", "import numpy as np\n", "\n", "from gfloat import decode_float\n", "from gfloat.formats import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List all the values in a format\n", "\n", "The first example shows how to list all values in a given format.\n", "We will choose the [OCP](https://www.opencompute.org/documents/ocp-8-bit-floating-point-specification-ofp8-revision-1-0-2023-12-01-pdf-1) E5M2 format.\n", "\n", "The object `format_info_ocp_e5m2` is from the `gfloat.formats` package, and describes the characteristics of that format:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "FormatInfo(name='ocp_e5m2', k=8, precision=3, emax=15, has_nz=True, has_infs=True, num_high_nans=3, has_subnormals=True, is_signed=True, is_twos_complement=False)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "format_info_ocp_e5m2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We shall use the format to decode all values from 0..255, and gather them in a pandas DataFrame.\n", "We see that `decode_float` returns a lot more than just the value - it also splits out the exponent, significand, and sign, and returns the `FloatClass`, which allows us to distinguish normal and subnormal numbers, as well as zero, infinity, and nan." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | fval | \n", "exp | \n", "expval | \n", "significand | \n", "fsignificand | \n", "signbit | \n", "fclass | \n", "
|---|---|---|---|---|---|---|---|
| code | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
| 0 | \n", "0.000000e+00 | \n", "0 | \n", "-14 | \n", "0 | \n", "0.00 | \n", "0 | \n", "FloatClass.ZERO | \n", "
| 1 | \n", "1.525879e-05 | \n", "0 | \n", "-14 | \n", "1 | \n", "0.25 | \n", "0 | \n", "FloatClass.SUBNORMAL | \n", "
| 2 | \n", "3.051758e-05 | \n", "0 | \n", "-14 | \n", "2 | \n", "0.50 | \n", "0 | \n", "FloatClass.SUBNORMAL | \n", "
| 3 | \n", "4.577637e-05 | \n", "0 | \n", "-14 | \n", "3 | \n", "0.75 | \n", "0 | \n", "FloatClass.SUBNORMAL | \n", "
| 4 | \n", "6.103516e-05 | \n", "1 | \n", "-14 | \n", "0 | \n", "1.00 | \n", "0 | \n", "FloatClass.NORMAL | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 251 | \n", "-5.734400e+04 | \n", "30 | \n", "15 | \n", "3 | \n", "1.75 | \n", "1 | \n", "FloatClass.NORMAL | \n", "
| 252 | \n", "-inf | \n", "31 | \n", "16 | \n", "0 | \n", "1.00 | \n", "1 | \n", "FloatClass.INFINITE | \n", "
| 253 | \n", "NaN | \n", "31 | \n", "16 | \n", "1 | \n", "1.25 | \n", "1 | \n", "FloatClass.NAN | \n", "
| 254 | \n", "NaN | \n", "31 | \n", "16 | \n", "2 | \n", "1.50 | \n", "1 | \n", "FloatClass.NAN | \n", "
| 255 | \n", "NaN | \n", "31 | \n", "16 | \n", "3 | \n", "1.75 | \n", "1 | \n", "FloatClass.NAN | \n", "
256 rows × 7 columns
\n", "