{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "# Collect value statistics for formats\n", "\n", "This notebook presents various statistics for a variety of float formats.\n", "Some of these are present on the `FormatInfo` class, and are presented for all formats.\n", "Others are obtained by enumerating all values (only for the =16 bit formats). \n", "\n", "## Statistics obtained from FormatInfo\n", "\n", " - name: Format\n", " - B: Bits in the format\n", " - P: Precision in bits\n", " - E: Exponent field width in bits\n", " - T: Trailing significand field width in bits\n", " - max: Largest finite value\n", " - min: Most negative value (typically the same, unless twos complement)\n", " - smallest: Smallest positive value\n", " - smallest_normal: Smallest positive normal value, NaN if all finite values are subnormal\n", " " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
nameBPEsmallestsmallest_normalmaxnum_nansinfs
p3109_k3p2sf321 0.5 1 1.5 10
ocp_e2m1422 0.5 1 6 00
p3109_k4p2sf422 0.25 0.5 3 10
ocp_e2m3642 0.125 1 7.5 00
ocp_e3m2633 0.0625 0.25 28 00
p3109_k6p3sf633 0.03125 0.125 14 10
p3109_k6p4sf642 0.0625 0.5 3.75 10
ocp_e4m3844≈0.00195310.015625 448 20
ocp_e5m2835≈1.5259e-05≈6.1035e-05 57344 62
p3109_k8p1se817≈1.0842e-19≈1.0842e-19≈4.6117e+18 12
p3109_k8p1ue818≈5.8775e-39≈5.8775e-39≈4.2535e+37 11
p3109_k8p3se835≈7.6294e-06≈3.0518e-05 49152 12
p3109_k8p3sf835≈7.6294e-06≈3.0518e-05 57344 10
p3109_k8p3ue836≈1.1642e-10≈4.6566e-10≈2.6844e+09 11
p3109_k8p3uf836≈1.1642e-10≈4.6566e-10≈3.2212e+09 10
p3109_k8p4se844≈0.000976560.0078125 224 12
p3109_k8p4sf844≈0.000976560.0078125 240 10
p3109_k8p4ue845≈3.8147e-06≈3.0518e-05 53248 11
p3109_k8p4uf845≈3.8147e-06≈3.0518e-05 57344 10
p3109_k8p7sf8710.015625 1≈ 1.9844 10
p3109_k8p8uf8810.0078125 1≈ 1.9844 10
binary1616115≈5.9605e-08≈6.1035e-05 65504 20462
bfloat161688≈9.1835e-41≈1.1755e-38≈3.3895e+38 2542
binary3232248≈1.4013e-45≈1.1755e-38≈3.4028e+38≈1.6777e+072
binary646453114.9407e-324≈2.2251e-308≈1.7977e+308≈9.0072e+152
ocp_e8m0818≈5.8775e-39≈5.8775e-39≈1.7014e+38 10
ocp_int88800.015625n/a≈ 1.9844 00
\n" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%run utils.py\n", "D = pandas_render # from utils\n", "import pandas\n", "from functools import partial\n", "from gfloat import *\n", "from gfloat.formats import *\n", "\n", "import numpy as np\n", "\n", "\n", "# Special rendering for float values -\n", "# if they don't round-trip in 8.5g, prepend with \"≈\", or render as rational\n", "def render_float(approx: bool, v):\n", " if not isinstance(v, float):\n", " return str(v)\n", "\n", " if np.isnan(v):\n", " return \"n/a\"\n", "\n", " s = f\"{v:8.5g}\"\n", " if float(s) == v:\n", " return s\n", "\n", " if approx:\n", " return \"≈\" + s\n", " else:\n", " return float_pow2str(v)\n", "\n", "\n", "def collect_stats(fi: FormatInfo):\n", " return dict(\n", " name=fi.name,\n", " B=fi.bits,\n", " P=fi.precision,\n", " E=fi.expBits,\n", " smallest=fi.smallest,\n", " smallest_normal=fi.smallest_normal if not fi.is_all_subnormal else np.nan,\n", " max=fi.max,\n", " num_nans=float(fi.num_nans),\n", " infs=fi.num_infs,\n", " )\n", "\n", "\n", "stats = [collect_stats(fi) for fi in sample_formats]\n", "df = pandas.DataFrame(stats)\n", "D(df, format=partial(render_float, True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Statistics computed by exhaustive inspection\n", "\n", " - lt1: Number of values x such that `0 < x < 1`\n", " - gt1: Number of values x such that `1 < x < Inf`\n", " - rt16: True if all values are exactly representable in IEEE binary16\n", " - min/maxSubnormal: Smallest/largest subnormal value, \"n/a\" if no values are subnormal\n", " - min/maxNormal: Smallest/largest normal value, \"n/a\" if no values are normal\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
nameBPErt16rt32lt1gt1minSubnormalmaxSubnormalminNormalmaxNormal
p3109_k3p2sf321TrueTrue11 0.5 0.5 1 1.5
ocp_e2m1422TrueTrue15 0.5 0.5 1 6
p3109_k4p2sf422TrueTrue33 0.25 0.25 0.5 3
ocp_e2m3642TrueTrue723 0.125 0.875 1 7.5
ocp_e3m2633TrueTrue1119 0.0625 0.1875 0.25 28
p3109_k6p3sf633TrueTrue1515 0.03125 0.09375 0.125 14
p3109_k6p4sf642TrueTrue1515 0.0625 0.4375 0.5 3.75
ocp_e4m3844TrueTrue5570≈0.0019531≈0.0136720.015625 448
ocp_e5m2835TrueTrue5963≈1.5259e-05≈4.5776e-05≈6.1035e-05 57344
p3109_k8p1se817FalseTrue6362n/an/a≈1.0842e-19≈4.6117e+18
p3109_k8p1ue818FalseTrue127125n/an/a≈5.8775e-39≈4.2535e+37
p3109_k8p3se835TrueTrue6362≈7.6294e-06≈2.2888e-05≈3.0518e-05 49152
p3109_k8p3sf835TrueTrue6363≈7.6294e-06≈2.2888e-05≈3.0518e-05 57344
p3109_k8p3ue836FalseTrue127125≈1.1642e-10≈3.4925e-10≈4.6566e-10≈2.6844e+09
p3109_k8p3uf836FalseTrue127126≈1.1642e-10≈3.4925e-10≈4.6566e-10≈3.2212e+09
p3109_k8p4se844TrueTrue6362≈0.00097656≈0.00683590.0078125 224
p3109_k8p4sf844TrueTrue6363≈0.00097656≈0.00683590.0078125 240
p3109_k8p4ue845TrueTrue127125≈3.8147e-06≈2.6703e-05≈3.0518e-05 53248
p3109_k8p4uf845TrueTrue127126≈3.8147e-06≈2.6703e-05≈3.0518e-05 57344
p3109_k8p7sf871TrueTrue63630.015625≈ 0.98438 1≈ 1.9844
p3109_k8p8uf881TrueTrue1271260.0078125≈ 0.99219 1≈ 1.9844
binary1616115TrueTrue1535916383≈5.9605e-08≈6.0976e-05≈6.1035e-05 65504
bfloat161688FalseTrue1625516383≈9.1835e-41≈1.1663e-38≈1.1755e-38≈3.3895e+38
ocp_e8m0818FalseTrue127127n/an/a≈5.8775e-39≈1.7014e+38
ocp_int8880TrueTrue63630.015625≈ 1.9844n/an/a
\n" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def compute_stats(fi: FormatInfo):\n", " # Generate all values\n", " values = [decode_float(fi, i) for i in range(2**fi.bits)]\n", " df = pandas.DataFrame(values)\n", "\n", " # Compute statistics: lt1,gt1\n", " fval = df[\"fval\"]\n", " total_01 = fval.between(0, 1, inclusive=\"neither\").sum()\n", " total_1Inf = fval.between(1, np.inf, inclusive=\"neither\").sum()\n", "\n", " # Compute statistics: maxFinite,minFinite\n", " finite_vals = fval[np.isfinite(fval)]\n", " maxFinite = finite_vals.loc[finite_vals.idxmax()]\n", " minFinite = finite_vals.loc[finite_vals.idxmin()]\n", " assert maxFinite == fi.max\n", " assert minFinite == fi.min\n", " fi.emax\n", "\n", " # Compute statistics: maxNormal,minNormal\n", " normal_vals = fval[(df[\"fclass\"] == FloatClass.NORMAL) & (fval > 0)]\n", " maxNormal = normal_vals.loc[normal_vals.idxmax()] if normal_vals.any() else np.nan\n", " minNormal = normal_vals.loc[normal_vals.idxmin()] if normal_vals.any() else np.nan\n", " assert np.isnan(maxNormal) or maxNormal == fi.max\n", " assert np.isnan(minNormal) or minNormal == fi.smallest_normal\n", "\n", " # Compute statistics: minSubnormal\n", " pos_subnormal = fval[(df[\"fclass\"] == FloatClass.SUBNORMAL) & (fval > 0)]\n", " maxSubnormal = (\n", " pos_subnormal.loc[pos_subnormal.idxmax()] if pos_subnormal.any() else np.nan\n", " )\n", " minSubnormal = (\n", " pos_subnormal.loc[pos_subnormal.idxmin()] if pos_subnormal.any() else np.nan\n", " )\n", " assert np.isnan(minSubnormal) or minSubnormal == fi.smallest_subnormal\n", " assert np.isnan(minNormal) or minNormal == fi.smallest_normal\n", "\n", " assert np.nanmin([minSubnormal, minNormal]) == fi.smallest\n", "\n", " # Compute roundtrips: rt16, rt32\n", " with np.errstate(over=\"ignore\"):\n", " rt16 = (np.float64(np.float16(fval)) == np.float64(fval)) | ~np.isfinite(fval)\n", " rt32 = (np.float64(np.float32(fval)) == np.float64(fval)) | ~np.isfinite(fval)\n", "\n", " rt16 = rt16.all()\n", " rt32 = rt32.all()\n", " # assert rt32 # If not, we should include rt32 in the table\n", "\n", " # Assemble tuple\n", " return dict(\n", " name=fi.name,\n", " B=fi.bits,\n", " P=fi.precision,\n", " E=fi.expBits,\n", " rt16=rt16,\n", " rt32=rt32,\n", " lt1=total_01,\n", " gt1=total_1Inf,\n", " minSubnormal=minSubnormal,\n", " maxSubnormal=maxSubnormal,\n", " minNormal=minNormal,\n", " maxNormal=maxNormal,\n", " )\n", "\n", "\n", "stats = [compute_stats(fi) for fi in sample_formats if fi.bits <= 16]\n", "df2 = pandas.DataFrame(stats)\n", "D(df2, format=partial(render_float, True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Emit the same table, but with exact values\n", "\n", "In this table, float values are printed as decimals, unless the decimals are not an\n", "exact representation of the value, in which case, they are printed as rationals (between 1 and 2) times 2^E." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
nameBPErt16rt32lt1gt1minSubnormalmaxSubnormalminNormalmaxNormal
p3109_k3p2sf321TrueTrue11 0.5 0.5 1 1.5
ocp_e2m1422TrueTrue15 0.5 0.5 1 6
p3109_k4p2sf422TrueTrue33 0.25 0.25 0.5 3
ocp_e2m3642TrueTrue723 0.125 0.875 1 7.5
ocp_e3m2633TrueTrue1119 0.0625 0.1875 0.25 28
p3109_k6p3sf633TrueTrue1515 0.03125 0.09375 0.125 14
p3109_k6p4sf642TrueTrue1515 0.0625 0.4375 0.5 3.75
ocp_e4m3844TrueTrue55702^-97/4*2^-70.015625 448
ocp_e5m2835TrueTrue59632^-163/2*2^-152^-14 57344
p3109_k8p1se817FalseTrue6362n/an/a2^-632^62
p3109_k8p1ue818FalseTrue127125n/an/a2^-1272^125
p3109_k8p3se835TrueTrue63622^-173/2*2^-162^-15 49152
p3109_k8p3sf835TrueTrue63632^-173/2*2^-162^-15 57344
p3109_k8p3ue836FalseTrue1271252^-333/2*2^-322^-315/4*2^31
p3109_k8p3uf836FalseTrue1271262^-333/2*2^-322^-313/2*2^31
p3109_k8p4se844TrueTrue63622^-107/4*2^-80.0078125 224
p3109_k8p4sf844TrueTrue63632^-107/4*2^-80.0078125 240
p3109_k8p4ue845TrueTrue1271252^-187/4*2^-162^-15 53248
p3109_k8p4uf845TrueTrue1271262^-187/4*2^-162^-15 57344
p3109_k8p7sf871TrueTrue63630.01562563/32*2^-1 1127/64*2^0
p3109_k8p8uf881TrueTrue1271260.0078125127/64*2^-1 1127/64*2^0
binary1616115TrueTrue15359163832^-241023/512*2^-152^-14 65504
bfloat161688FalseTrue16255163832^-133127/64*2^-1272^-126255/128*2^127
ocp_e8m0818FalseTrue127127n/an/a2^-1272^127
ocp_int8880TrueTrue63630.015625127/64*2^0n/an/a
\n" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "D(df2, format=partial(render_float, False))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(448, True)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "count_p3109_formats = len(\n", " [\n", " 1\n", " for signedness in (True, False)\n", " for domain in (Domain.Extended, Domain.Finite)\n", " for k in range(2, 16)\n", " for p in range(1, k if signedness else k + 1)\n", " ]\n", ")\n", "count_p3109_formats, count_p3109_formats == format_info_ocp_e4m3.max" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "np.True_" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "all_p3109 = [\n", " format_info_p3109(k, p, signedness, domain)\n", " for signedness in (Signedness.Signed, Signedness.Unsigned)\n", " for domain in (Domain.Extended, Domain.Finite)\n", " for k in range(3, 8)\n", " for p in range(1, k if signedness == Signedness.Signed else k + 1)\n", "]\n", "\n", "stats = [compute_stats(fi) for fi in all_p3109]\n", "df3 = pandas.DataFrame(stats)\n", "df3[\"rt32\"].all()\n", "# D(df3, format=partial(render_float, True))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Tables in RST/Markdown\n", "\n", "These are used to generate gfloat documentation, but may be of use in other\n", "contexts so left here." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============ === === === =========== ================= ============ =========== ======\n", "name B P E smallest smallest_normal max num_nans infs\n", "============ === === === =========== ================= ============ =========== ======\n", "p3109_k3p2sf 3 2 1 0.5 1 1.5 1 0\n", "ocp_e2m1 4 2 2 0.5 1 6 0 0\n", "p3109_k4p2sf 4 2 2 0.25 0.5 3 1 0\n", "ocp_e2m3 6 4 2 0.125 1 7.5 0 0\n", "ocp_e3m2 6 3 3 0.0625 0.25 28 0 0\n", "p3109_k6p3sf 6 3 3 0.03125 0.125 14 1 0\n", "p3109_k6p4sf 6 4 2 0.0625 0.5 3.75 1 0\n", "ocp_e4m3 8 4 4 ≈0.0019531 0.015625 448 2 0\n", "ocp_e5m2 8 3 5 ≈1.5259e-05 ≈6.1035e-05 57344 6 2\n", "p3109_k8p1se 8 1 7 ≈1.0842e-19 ≈1.0842e-19 ≈4.6117e+18 1 2\n", "p3109_k8p1ue 8 1 8 ≈5.8775e-39 ≈5.8775e-39 ≈4.2535e+37 1 1\n", "p3109_k8p3se 8 3 5 ≈7.6294e-06 ≈3.0518e-05 49152 1 2\n", "p3109_k8p3sf 8 3 5 ≈7.6294e-06 ≈3.0518e-05 57344 1 0\n", "p3109_k8p3ue 8 3 6 ≈1.1642e-10 ≈4.6566e-10 ≈2.6844e+09 1 1\n", "p3109_k8p3uf 8 3 6 ≈1.1642e-10 ≈4.6566e-10 ≈3.2212e+09 1 0\n", "p3109_k8p4se 8 4 4 ≈0.00097656 0.0078125 224 1 2\n", "p3109_k8p4sf 8 4 4 ≈0.00097656 0.0078125 240 1 0\n", "p3109_k8p4ue 8 4 5 ≈3.8147e-06 ≈3.0518e-05 53248 1 1\n", "p3109_k8p4uf 8 4 5 ≈3.8147e-06 ≈3.0518e-05 57344 1 0\n", "p3109_k8p7sf 8 7 1 0.015625 1 ≈ 1.9844 1 0\n", "p3109_k8p8uf 8 8 1 0.0078125 1 ≈ 1.9844 1 0\n", "binary16 16 11 5 ≈5.9605e-08 ≈6.1035e-05 65504 2046 2\n", "bfloat16 16 8 8 ≈9.1835e-41 ≈1.1755e-38 ≈3.3895e+38 254 2\n", "binary32 32 24 8 ≈1.4013e-45 ≈1.1755e-38 ≈3.4028e+38 ≈1.6777e+07 2\n", "binary64 64 53 11 4.9407e-324 ≈2.2251e-308 ≈1.7977e+308 ≈9.0072e+15 2\n", "ocp_e8m0 8 1 8 ≈5.8775e-39 ≈5.8775e-39 ≈1.7014e+38 1 0\n", "ocp_int8 8 8 0 0.015625 n/a ≈ 1.9844 0 0\n", "============ === === === =========== ================= ============ =========== ======\n" ] } ], "source": [ "from tabulate import tabulate\n", "\n", "dfstr = df.map(lambda x: render_float(True, x))\n", "print(\n", " tabulate(dfstr, df.columns, tablefmt=\"rst\", showindex=False).replace(\" nan\", \" n/a\")\n", ")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============ === === === =========== ================= ======================================== ====================================== ======\n", "name B P E smallest smallest_normal max num_nans infs\n", "============ === === === =========== ================= ======================================== ====================================== ======\n", "p3109_k3p2sf 3 2 1 0.5 1 1.5 1 0\n", "ocp_e2m1 4 2 2 0.5 1 6 0 0\n", "p3109_k4p2sf 4 2 2 0.25 0.5 3 1 0\n", "ocp_e2m3 6 4 2 0.125 1 7.5 0 0\n", "ocp_e3m2 6 3 3 0.0625 0.25 28 0 0\n", "p3109_k6p3sf 6 3 3 0.03125 0.125 14 1 0\n", "p3109_k6p4sf 6 4 2 0.0625 0.5 3.75 1 0\n", "ocp_e4m3 8 4 4 2^-9 0.015625 448 2 0\n", "ocp_e5m2 8 3 5 2^-16 2^-14 57344 6 2\n", "p3109_k8p1se 8 1 7 2^-63 2^-63 2^62 1 2\n", "p3109_k8p1ue 8 1 8 2^-127 2^-127 2^125 1 1\n", "p3109_k8p3se 8 3 5 2^-17 2^-15 49152 1 2\n", "p3109_k8p3sf 8 3 5 2^-17 2^-15 57344 1 0\n", "p3109_k8p3ue 8 3 6 2^-33 2^-31 5/4*2^31 1 1\n", "p3109_k8p3uf 8 3 6 2^-33 2^-31 3/2*2^31 1 0\n", "p3109_k8p4se 8 4 4 2^-10 0.0078125 224 1 2\n", "p3109_k8p4sf 8 4 4 2^-10 0.0078125 240 1 0\n", "p3109_k8p4ue 8 4 5 2^-18 2^-15 53248 1 1\n", "p3109_k8p4uf 8 4 5 2^-18 2^-15 57344 1 0\n", "p3109_k8p7sf 8 7 1 0.015625 1 127/64*2^0 1 0\n", "p3109_k8p8uf 8 8 1 0.0078125 1 127/64*2^0 1 0\n", "binary16 16 11 5 2^-24 2^-14 65504 2046 2\n", "bfloat16 16 8 8 2^-133 2^-126 255/128*2^127 254 2\n", "binary32 32 24 8 2^-149 2^-126 16777215/8388608*2^127 8388607/4194304*2^23 2\n", "binary64 64 53 11 4.9407e-324 2^-1022 9007199254740991/9007199254740992*2^1024 4503599627370495/4503599627370496*2^53 2\n", "ocp_e8m0 8 1 8 2^-127 2^-127 2^127 1 0\n", "ocp_int8 8 8 0 0.015625 n/a 127/64*2^0 0 0\n", "============ === === === =========== ================= ======================================== ====================================== ======\n" ] } ], "source": [ "from tabulate import tabulate\n", "\n", "dfstr = df.map(lambda x: render_float(False, x))\n", "print(tabulate(dfstr, df.columns, tablefmt=\"rst\", showindex=False))" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| name | B | P | E | rt16 | rt32 | lt1 | gt1 | minSubnormal | maxSubnormal | minNormal | maxNormal |\n", "|--------------|-----|-----|-----|--------|--------|-------|-------|----------------|----------------|-------------|---------------|\n", "| p3109_k3p2sf | 3 | 2 | 1 | True | True | 1 | 1 | 0.5 | 0.5 | 1 | 1.5 |\n", "| ocp_e2m1 | 4 | 2 | 2 | True | True | 1 | 5 | 0.5 | 0.5 | 1 | 6 |\n", "| p3109_k4p2sf | 4 | 2 | 2 | True | True | 3 | 3 | 0.25 | 0.25 | 0.5 | 3 |\n", "| ocp_e2m3 | 6 | 4 | 2 | True | True | 7 | 23 | 0.125 | 0.875 | 1 | 7.5 |\n", "| ocp_e3m2 | 6 | 3 | 3 | True | True | 11 | 19 | 0.0625 | 0.1875 | 0.25 | 28 |\n", "| p3109_k6p3sf | 6 | 3 | 3 | True | True | 15 | 15 | 0.03125 | 0.09375 | 0.125 | 14 |\n", "| p3109_k6p4sf | 6 | 4 | 2 | True | True | 15 | 15 | 0.0625 | 0.4375 | 0.5 | 3.75 |\n", "| ocp_e4m3 | 8 | 4 | 4 | True | True | 55 | 70 | 2^-9 | 7/4*2^-7 | 0.015625 | 448 |\n", "| ocp_e5m2 | 8 | 3 | 5 | True | True | 59 | 63 | 2^-16 | 3/2*2^-15 | 2^-14 | 57344 |\n", "| p3109_k8p1se | 8 | 1 | 7 | False | True | 63 | 62 | n/a | n/a | 2^-63 | 2^62 |\n", "| p3109_k8p1ue | 8 | 1 | 8 | False | True | 127 | 125 | n/a | n/a | 2^-127 | 2^125 |\n", "| p3109_k8p3se | 8 | 3 | 5 | True | True | 63 | 62 | 2^-17 | 3/2*2^-16 | 2^-15 | 49152 |\n", "| p3109_k8p3sf | 8 | 3 | 5 | True | True | 63 | 63 | 2^-17 | 3/2*2^-16 | 2^-15 | 57344 |\n", "| p3109_k8p3ue | 8 | 3 | 6 | False | True | 127 | 125 | 2^-33 | 3/2*2^-32 | 2^-31 | 5/4*2^31 |\n", "| p3109_k8p3uf | 8 | 3 | 6 | False | True | 127 | 126 | 2^-33 | 3/2*2^-32 | 2^-31 | 3/2*2^31 |\n", "| p3109_k8p4se | 8 | 4 | 4 | True | True | 63 | 62 | 2^-10 | 7/4*2^-8 | 0.0078125 | 224 |\n", "| p3109_k8p4sf | 8 | 4 | 4 | True | True | 63 | 63 | 2^-10 | 7/4*2^-8 | 0.0078125 | 240 |\n", "| p3109_k8p4ue | 8 | 4 | 5 | True | True | 127 | 125 | 2^-18 | 7/4*2^-16 | 2^-15 | 53248 |\n", "| p3109_k8p4uf | 8 | 4 | 5 | True | True | 127 | 126 | 2^-18 | 7/4*2^-16 | 2^-15 | 57344 |\n", "| p3109_k8p7sf | 8 | 7 | 1 | True | True | 63 | 63 | 0.015625 | 63/32*2^-1 | 1 | 127/64*2^0 |\n", "| p3109_k8p8uf | 8 | 8 | 1 | True | True | 127 | 126 | 0.0078125 | 127/64*2^-1 | 1 | 127/64*2^0 |\n", "| binary16 | 16 | 11 | 5 | True | True | 15359 | 16383 | 2^-24 | 1023/512*2^-15 | 2^-14 | 65504 |\n", "| bfloat16 | 16 | 8 | 8 | False | True | 16255 | 16383 | 2^-133 | 127/64*2^-127 | 2^-126 | 255/128*2^127 |\n", "| ocp_e8m0 | 8 | 1 | 8 | False | True | 127 | 127 | n/a | n/a | 2^-127 | 2^127 |\n", "| ocp_int8 | 8 | 8 | 0 | True | True | 63 | 63 | 0.015625 | 127/64*2^0 | n/a | n/a |\n" ] } ], "source": [ "dfstr = df2.map(lambda x: render_float(False, x))\n", "print(\n", " tabulate(dfstr, df2.columns, tablefmt=\"github\", showindex=False).replace(\n", " \" nan\", \" n/a\"\n", " )\n", ")" ] } ], "metadata": { "kernelspec": { "display_name": "gfloat", "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.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }