A small Python library which gives you the IEEE-754 representation of a floating point number.

Overview

ieee754

ieee754 is small Python library which gives you the IEEE-754 representation of a floating point number. You can specify a precision given in the list below or you can even use your own custom precision.

  • Half Precision (16 bit: 1 bit for sign + 5 bits for exponent + 10 bits for mantissa)
  • Single Precision (32 bit: 1 bit for sign + 8 bits for exponent + 23 bits for mantissa)
  • Double Precision (64 bit: 1 bit for sign + 11 bits for exponent + 52 bits for mantissa)
  • Quadrupole Precision (128 bit: 1 bit for sign + 15 bits for exponent + 112 bits for mantissa)
  • Octuple Precision (256 bit: 1 bit for sign + 19 bits for exponent + 236 bits for mantissa)
  • Prerequisites

    ieee754 uses numpy, so you should install numpy first.

    $ pip install numpy

    Installing

    To download ieee754, either fork this github repo or simply use Pypi via pip.

    $ pip install ieee754

    Using

    After installation, you can import ieee754 and use it in your projects.

    Default Options

    Default precision is Double Precision and you can get the output by just calling the instance as a string.

    from ieee754 import IEEE754
    
    x = 13.375
    a = IEEE754(x)
    # you should call the instance as a string
    print(str(a))
    print(f"{a}")
    # you can get the hexadecimal presentation like this
    print(a.str2hex())

    Select a Precision

    You can use Half (p=0), Single (p=1), Double (p=2), Quadrupole (p=3) or Octuple precision (p=4).

    from ieee754 import IEEE754
    
    for p in range(5):
        a = IEEE754(x, p)
        print("x = %f | b = %s | h = %s" % (13.375, a, a.str2hex()))

    Using a Custom Precision

    You can force total length, exponent, and mantissa size, and also the bias.

    a = IEEE754(x, force_length=19, force_exponent=6, force_mantissa=12, force_bias=31)
    print(f"{a}")

    License

    MIT License

    Copyright (c) 2021 Bora Canbula

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

You might also like...
Built with Python programming language and QT library and Guess the number in three easy, medium and hard rolls
Built with Python programming language and QT library and Guess the number in three easy, medium and hard rolls

guess-the-numbers Built with Python programming language and QT library and Guess the number in three easy, medium and hard rolls Number guessing game

Built with Python programming language and QT library and Guess the number in three easy, medium and hard rolls
Built with Python programming language and QT library and Guess the number in three easy, medium and hard rolls

password-generator Built with Python programming language and QT library and Guess the number in three easy, medium and hard rolls Password generator

This Python library searches through a static directory and appends artist, title, track number, album title, duration, and genre to a .json object

This Python library searches through a static directory (needs to match your environment) and appends artist, title, track number, album title, duration, and genre to a .json object. This .json object is then used to post data to a specified table in a local MySQL database, credentials of which the user must set.

Funchacks - Fun module which is a small set of utilities
Funchacks - Fun module which is a small set of utilities

funchacks 👋 Introduction Funchacks is a fun module that provides a small packag

Wordless - the #1 app for helping you cheat at Wordle, which is sure to make you popular at parties

Wordless Wordless is the #1 app for helping you cheat at Wordle, which is sure t

Small exercises to get you used to reading and writing Python code!

Pythonlings Welcome to Pythonlings, an automated Python tutorial program (inspired by Rustlings and Haskellings). WIP This program is still working in

Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Advent Of Code 2021 - Python English Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels th

This is a vscode extension with a Virtual Assistant that you can play with when you are bored or you need help..

VS Code Virtual Assistant This is a vscode extension with a Virtual Assistant that you can play with when you are bored or you need help. Its currentl

Comments
  • Inaccurate results for 0.5 and 0.75 for half-precision.

    Inaccurate results for 0.5 and 0.75 for half-precision.

    For 0.5 by using this tool, you can find the accurate IEEE754 Half Precision representation for 0.5 as follows: image But in this repo, we have: image This is inaccurate by 2 bits.

    opened by akincan-kilic 0
  • Precision issues

    Precision issues

    If you choose Quadruple precision, the mantissa portion of the number gets approximated after a certain number of bits.

    As an example, the results for Quadruple precision from this converter (https://babbage.cs.qc.cuny.edu/IEEE-754/) are different from the one using the IEEE754 class.

    Digging a bit in the methods, the approximation is related to the fact that building the mantissa multiplying by 2 makes the error due to the initial approximation of the floating point number larger and larger, until it affects the result of the mantissa computation.

    I read that python and numpy do not handle particularly well high precision real numbers.

    I had to employ gmpy2 for my needs, maybe it's worth having a look at it for future modifications to the class.

    opened by francescotaurone 0
  • wrong result

    wrong result

    something is wrong with small numbers lile -0.5687.

    IEEE754(-0.5687) = 10111111110010001100101100101001 (= 0xBFC8CB29) while the resultt should be 0xbf119653.

    opened by raptorenpls 0
Releases(v_02)
Owner
Bora Canbula
Assoc. Prof. Dr. @ MCBU CENG. Studying theoretical nuclear physics and high performance computing.
Bora Canbula
Store Simulation

Almacenes Para clonar el Repositorio: Vaya a la terminal de Linux o Mac, o a la cmd en Windows y ejecute:

Johan Posada 1 Nov 12, 2021
bib2xml - A tool for getting Word formatted XML from Bibtex files

bib2xml - A tool for getting Word formatted XML from Bibtex files Processes Bibtex files (.bib), produces Word Bibliography XML (.xml) output Why not

Matheus Sartor 1 May 05, 2022
OCR-ID-Card VietNamese (new id-card)

OCR-ID-Card VietNamese (new id-card) run project: download 2 file weights and pu

12 Jun 15, 2022
A parallel branch-and-bound engine for Python.

pybnb A parallel branch-and-bound engine for Python. This software is copyright (c) by Gabriel A. Hackebeil (gabe.hacke

Gabriel Hackebeil 52 Nov 12, 2022
This is a backport of the BaseExceptionGroup and ExceptionGroup classes from Python 3.11.

This is a backport of the BaseExceptionGroup and ExceptionGroup classes from Python 3.11. It contains the following: The exceptiongroup.BaseExceptionG

Alex Grönholm 19 Dec 15, 2022
python for windows extensions

This is the readme for the Python for Win32 (pywin32) extensions source code. See CHANGES.txt for recent changes. 'setup.py' is a standard distutils

27 Dec 08, 2022
Paxos in Python, tested with Jepsen

Python implementation of Multi-Paxos with a stable leader and reconfiguration, roughly following "Paxos Made Moderately Complex". Run python3 paxos/st

A. Jesse Jiryu Davis 25 Dec 15, 2022
A script that will warn you, by opening a new browser tab, when there are new content in your favourite websites.

web check A script that will warn you, by opening a new browser tab, when there are new content in your favourite websites. What it does The script wi

Jaime Álvarez 52 Mar 15, 2022
Python - Aprendendo Python na ByLearn

PYTHON Identação Escopo Pai Escopo filho Escopo neto Variaveis

Italo Rafael 3 May 31, 2022
AminoAutoRegFxck/AutoReg For AminoApps.com

AminoAutoRegFxck AminoAutoRegFxck/AutoReg For AminoApps.com Termux apt update -y apt upgrade -y pkg install python git clone https://github.com/LilZev

3 Jan 18, 2022
Um sistema de llogin feito em uma interface grafica.

Interface-para-login Um sistema de login feito com JSON. Utilizando a biblioteca Tkinter, eu criei um sistema de login, onde guarda a informações de l

Mobben 1 Nov 28, 2021
An interactive tool with which to explore the possible imaging performance of candidate ngEHT architectures.

ngEHTexplorer An interactive tool with which to explore the possible imaging performance of candidate ngEHT architectures. Welcome! ngEHTexplorer is a

Avery Broderick 7 Jan 28, 2022
Cairo-math-64x61 - Fixed point 64.61 math library for Cairo / Starknet

Cairo Math 64x61 A fixed point 64.61 math library for Cairo & Starknet Signed 64

Influence 63 Dec 05, 2022
Daily knowledge pills to get better in Python.

Python daily pills Daily knowledge pills to get better Python code. Why Does your Python code suffers of any of this symptoms? Incorrect Indentation I

Jeferson Vaz dos Santos 35 Sep 19, 2022
Python Common things by Problem Fighter Library, (Exception, Debug Log, etc.)

In the name of God, the Most Gracious, the Most Merciful. PF-PY-Common Documentation Install and update using pip: pip install -U xxxx Please find the

Problem Fighter 3 Jan 15, 2022
World's best free and open source ERP.

World's best free and open source ERP.

Frappe 12.5k Jan 07, 2023
Backend/API for the Mumble.dev, an open source social media application.

Welcome to the Mumble Api Repository Getting Started If you are trying to use this project for the first time, you can get up and running by following

Dennis Ivy 189 Dec 27, 2022
A collection of Python library code for building Python applications.

Abseil Python Common Libraries This repository is a collection of Python library code for building Python applications. The code is collected from Goo

Abseil 2k Jan 07, 2023
MDAnalysis tool to calculate membrane curvature.

The MDAkit for membrane curvature analysis is part of the Google Summer of Code program and it is linked to a Code of Conduct.

MDAnalysis 19 Oct 20, 2022
A basic DIY-project made using Python and MySQL

Banking-Using-Python-MySQL This is a basic DIY-project made using Python and MySQL. Pre-Requisite needed:-- MySQL command Line:- creating a database

ABHISHEK 0 Jul 03, 2022