File Exchange

image thumbnail

Floating Point Number Conversion

version 1.0.0.0 (3.78 KB) by Eric Verner
Converts floating point numbers between decimal and IEEE 754 binary representation

12 Downloads

Updated 20 Nov 2012

View License

This package is designed to convert floating point point numbers from their decimal to their binary formats, according to the IEEE 754 standard. This is useful when calculations at the limits of MATLAB precision are performed or when the binary strings are of interest, such as in genetic algorithms. The package consists of the following four scripts.

float2bin: converts decimal floating point numbers to IEEE 754 binary string

bin2float: converts binary string to floating point number

bitstr2vec: converts binary string to binary vector of 0s and 1s

bitvec2str: converts binary vector to binary string

The floating point format that Matlab utilizes and some functions relating to floating point precision and limits are described in this post:

http://matlabgeeks.com/tips-tutorials/floating-point-numbers-in-matlab-2/

The float2bin function included in this package is described in this post:

http://matlabgeeks.com/tips-tutorials/conversion-of-floating-point-numbers-in-matlab/

Comments and Ratings (8)

float2bin can be fixed by transposing nums in line 28.

old:
bins = dec2bin(nums, 4); %Convert to array of binary number strings
new:
bins = dec2bin(nums', 4); %Convert to array of binary number strings

Bin2float is broken. Additionally, float2bin does not produce the correct output. Suppose you have an array a=[a1,a2] where a1 and a2 both have the most significant hex word equal to 5 (arbitrary). The result of b=float2bin(a)= '01010101...' instead of '0101 ... 0101 ...'.

it's amazing , thanks a lot for your contribution .

balcksil

Marcus Hill

Undefined function or variable 'float2bin'. It's showing this

Very useful thank you for this post.

Ludovic

Hi, very great and usefull post. I had to do floating point to Binary conversion for my thesis and it very usefull.
But also, if would be better if you add a method for conversion from binary floating point vector to decimal directly, since handling strings cost more time on computer. One will have to take it into consideration if the conversion have to be repeated over a large number of matrix elements

MATLAB Release Compatibility
Created with R14
Compatible with any release
Platform Compatibility
Windows macOS Linux