Skip to main content

Educational python module to parse floats and inspect the IEEE754 algorithm's internals.

Project description

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).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

floatedu-0.0.3.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

floatedu-0.0.3-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file floatedu-0.0.3.tar.gz.

File metadata

  • Download URL: floatedu-0.0.3.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for floatedu-0.0.3.tar.gz
Algorithm Hash digest
SHA256 322647a13e6f97786ea86fac5f0e4c50bf9b90da175d60bf8dc427a301970125
MD5 31f9703f1440d2b69ac8ccfdf04bf4ba
BLAKE2b-256 cc472de2c69d4f1c70398b81bf84bcf35f651871d549cba2fb88c78ad4e89102

See more details on using hashes here.

File details

Details for the file floatedu-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: floatedu-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for floatedu-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 19673dd7b5d26ffe8776b34550d606c86ab56e3174693de35f7ae064488c098b
MD5 f9c854614153a18580729155500ac15e
BLAKE2b-256 281c3614750ee2028872b0eebfd2f9245130cfd6963e1c86f524b4494f48877e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page