b03901165Shih / Custom-floating-point-to-bit-conversion

Python and C++ codes of floating-point to bit-string conversion and bit-string to floating-point conversion

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom-floating-point-to-bit-conversion

This directory contains python and C++ codes of floating-point to bit-string conversion and bit-string to floating-point conversion functions.

Hardware designs calculating with floating-point representations may contain floating-point DSPs that has adjustable wordlength (exponent and fractional bits). Functions provided here can be useful tools for building testbenches or input patterns to these designs.

Usage (Python)

Number to bit-string in the specified exponent bit and fractional bit floating-point format:

>> float_to_bits(num, exp, sig)

Bit-string to number:

>> bits_to_num(string, exp, sig)

Example of running main.py:

=============(EXP, SIG) = (8, 23)(Single)===========
Original number: 3.14159265
Float to bits:   01000000010010010000111111011011
Bits to Float:   3.1415927410125732

=============(EXP, SIG) = (6, 17)(Custom)===========
Original number: 3.14159265
Float to bits:   010000010010010000111111
Bits to Float:   3.1415863037109375

=============(EXP, SIG) = (5, 11)(Half)===========
Original number: 3.14159265
Float to bits:   01000010010010001
Bits to Float:   3.1416015625

=============(EXP, SIG) = (4, 6)(Custom)===========
Original number: 3.14159265
Float to bits:   01000100101
Bits to Float:   3.15625

About

Python and C++ codes of floating-point to bit-string conversion and bit-string to floating-point conversion

License:MIT License


Languages

Language:Python 55.8%Language:C++ 44.2%