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
Gives you more advanced math in python.

AdvancedPythonMath Gives you more advanced math in python. Functions .simplex(args: {number}) .circ(args: {raidus}) .pytha(args: {leg_a + leg_2}) .slo

Voidy Devleoper 1 Dec 25, 2021
PyLaboratory 0 Feb 7, 2022
Stopmagic gives you the power of creating amazing Stop Motion animations faster and easier than ever before.

Stopmagic gives you the power of creating amazing Stop Motion animations faster and easier than ever before. This project is maintained by Aldrin Mathew.

Aldrin's Art Factory 67 Dec 31, 2022
A NetBox Plugin that gives a UI for generating, comparing and deploying configurations to devices.

netbox_config_plugin - A plugin to generate, compare and deploy configurations This plugin allows you to execute your code to generate a config for a

Jo 11 Dec 21, 2022
Gives criticality score for an open source project

Open Source Project Criticality Score (Beta) This project is maintained by members of the Securing Critical Projects WG. Goals Generate a criticality

Open Source Security Foundation (OpenSSF) 1.1k Dec 23, 2022
OnTime is a small python that you set a time and on that time, app will send you notification and also play an alarm.

OnTime Always be OnTime! What is OnTime? OnTime is a small python that you set a time and on that time, app will send you notification and also play a

AmirHossein Mohammadi 11 Jan 16, 2022
Blender 3.1 Alpha (and later) PLY importer that correctly loads point clouds (and all PLY models as point clouds)

import-ply-as-verts Blender 3.1 Alpha (and later) PLY importer that correctly loads point clouds (and all PLY models as point clouds) Latest News Mand

Michael Prostka 82 Dec 20, 2022
IEEE ITU bunyesinde komitelere verilen Python3 egitiminin dokumanlastirilmis versiyonlari bu repository altinda tutulmaktadir.

IEEE ITU Python Egitimi Nasil Faydalanmaliyim? Dersleri izledikten sonra dokumanlardaki kodlari yorum satirlari isaretlerini kaldirarak deneyebilirsin

İTÜ IEEE Student Branch 47 Sep 4, 2022
Islam - This is a simple python script.In this script I have written all the suras of Al Quran. As a result, by using this script, you can know the number of any sura at the moment.

Introduction: If you want to know sura number of al quran by just typing the name of sura than you can use this script. Usage in termux: $ pkg install

Fazle Rabbi 1 Jan 2, 2022
A web application which you can search, buy or sell shares with current prices which provided by IEX.

CS50 - Stock Exchange A web application which you can search, buy or sell shares with current prices which provided by IEX. Table of Contents Setup St

null 1 May 28, 2022