Skip to main content

Float-IEEE754-didactic

Educational python module to parse floats and inspect the IEEE 754 representation internals.

Installation

pip install floatedu

Use case

It might be used to get a glimpse of how the IEEE 754 model works. This means it's useful almost exclusively for educational purposes.

Brief example

Create 3 floats using the float32 specification and print them as native floats:

from floatedu import Float32

f_1 = Float32("0 01111111 00000000000000000000000")
f_num = Float32("0 10001001 00110100100111010011101")
f_inf = Float32("0 11111111 00000000000000000000000")

print(f_1, f_num, f_inf)

# 1.0 1234.4566650390625 inf

Print number details as per general formula:

print(repr(f_num))

# {'value': float('1234.4566650390625'),
#  'kind': 'normal', 'k': 8, 'p': 24, 'bias': 127,
#  'bits': '0_10001001_00110100100111010011101',
#  'sign': 1, 'exponent': 137, 'fraction': 0.20552408695220947,
#  'significand': 1.2055240869522095,
# }

Everything is accessible as a property:

print(f_num.kind, f_num.sign, f_num.exponent, f_num.fraction)

# normal 1 137 0.20552408695220947

Float is a subclass of list and can be updated live:

print(f_num.sign_bit)
print(f_num.exponent_bits)
print(f_num.fraction_bits)
f_num.sign_bit[0] = 1 #  Make number negative
print(f_num)

# [0]
# [1, 0, 0, 0, 1, 0, 0, 1]
# [0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1]
# -1234.4566650390625

Formats and equations

IEEE 754 defines various types of binary floats. This module implements standard and, in addition, a couple of non-standard types.

IEEE 754 bits layouts

Bits are laid out from left to right following the scheme sign . exponent . fraction. p value includes the implicit bit (not actually stored).

For example the BFloat16 is stored as:

BFloat16 bits layout

Kind

A number could be Zero, Infinity, Not a number, Normal or Subnormal. The first three cases are already values. Normal and Subnormal numbers are computed according to an equation.

To determine how to compute the value is sufficient to test exp and fraction against zero (all zeroes) and -1 (all ones, two's complement):

Float type algorithm

Zero and infinity

For Zero and Infinity the value is trivial. If the leftmost bit is 0 then the value is positive (+0 or +inf). Otherwise it is negative (-0 or -inf).

Not a number

In case of Not a number no extra steps is required if not returning an appropriate "non-value".

Normal

Normal numbers values can be computed by the equation (general formula and float32 formula):

General formula for floats Formula for float32

An another way to think about this formula is to consider the stored number as a fixed point binary number with sign bit.

In this case, the integer part would be the exponent and the fractional part (plus 1) would be the significand. I.e.

Float as fixed point binary

Implementation

Every implemented float type is available as a class:

from floatedu import *
[Float, Float8, Float16, BFloat16, Float64, Float32, Float128, Float256]

The actual implementation class is Float and it couldn't be instantiated directly.

It must be subclassed providing p, k, and bias values as class properties (see floatedu/Float.py).

Release files for floatedu 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for floatedu 0.0.3
File Size Uploaded
floatedu-0.0.3.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for floatedu 0.0.3
File Interpreter ABI Platform
floatedu-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 22.9 kB

Release files / floatedu-0.0.3.tar.gz

Download URL floatedu-0.0.3.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
322647a13e6f97786ea86fac5f0e4c50bf9b90da175d60bf8dc427a301970125
BLAKE2b-256 checksum
How to use checksums
cc472de2c69d4f1c70398b81bf84bcf35f651871d549cba2fb88c78ad4e89102
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.8.0

Release files / floatedu-0.0.3-py3-none-any.whl

Download URL floatedu-0.0.3-py3-none-any.whl
Size 17.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
19673dd7b5d26ffe8776b34550d606c86ab56e3174693de35f7ae064488c098b
BLAKE2b-256 checksum
How to use checksums
281c3614750ee2028872b0eebfd2f9245130cfd6963e1c86f524b4494f48877e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.8.0

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page