## Binary floating point Representation in Matlab

### Johan Johan (view profile)

on 12 Sep 2019
Latest activity Edited by Bruno Luong

### Bruno Luong (view profile)

on 16 Sep 2019 at 10:20

### James Tursa (view profile)

Floating-point numbers are represented as
X=(-1)^s*m*2^c.
s: the sign
m:matissa
c:exponent
For example
The value 1.9844 as Binary floating point would be REF
0 01111111 11111100000000011010010
^ Sign bit
^^^^^^^^ Exponent
^^^^^^^^^^^^^^^^^^^^^^^ Fraction
But in matlab ,how doing the same work in this vector ?
A=[ 0.1900 -0.0300 -0.1300 0 0.1500 -0.0700 0.0500 0.1600 -0.2500 -0.1900];

### James Tursa (view profile)

on 12 Sep 2019

Did you try it?
>> A=[ 0.1900 -0.0300 -0.1300 0 0.1500 -0.0700 0.0500 0.1600 -0.2500 -0.1900];
>> dec2bin(typecast(A,'uint64'),64)
ans =
0011111111001000010100011110101110000101000111101011100000000000
1011111110011110101110000101000111101011100001010010000000000000
1011111111000000101000111101011100001010001111010111000000000000
0000000000000000000000000000000000000000000000000000000000000000
0011111111000011001100110011001100110011001100110011010000000000
1011111110110001111010111000010100011110101110000101000000000000
0011111110101001100110011001100110011001100110011001101000000000
0011111111000100011110101110000101000111101011100001010000000000
1011111111010000000000000000000000000000000000000000000000000000
1011111111001000010100011110101110000101000111101011100000000000

Johan Johan

### Johan Johan (view profile)

on 12 Sep 2019
Thank you, but i'm confused about the results, it is difference if i applied the function float2bin [in this reference], gave me this results
0011111111001000010100011110101110000101000111101011100001010010
1011111110011110101110000101000111101011100001010001111010111000
1011111111000000101000111101011100001010001111010111000010100100
0000000000000000000000000000000000000000000000000000000000000000
0011111111000011001100110011001100110011001100110011001100110011
1011111110110001111010111000010100011110101110000101000111101100
0011111111000100011110101110000101000111101011100001010001111011
1011111111010000000000000000000000000000000000000000000000000000
why there is difference?
James Tursa

### James Tursa (view profile)

on 12 Sep 2019
Well, the short answer turns out to be that dec2bin( ) is wrong (at least in some versions of MATLAB) ... it can't convert numbers greater than flintmax properly (see note at bottom of doc page). And, in general, lots of double numbers, when typecast to uint64, will violate this restriction. (IMO this restriction should probably be spelled out in BOLD LETTERS in the data types section, not as a note at the bottom of the page)
So you are going to be left with other methods, such as your float2bin function (which I have not used and cannot vouch for), or splitting up the MATLAB conversions to work on 32-bit chunks (or less) instead of 64-bit chunks.
E.g., for that first value of 0.1900
>> num2hex(0.1900)
ans =
3fc851eb851eb852 <-- We should expect the last 4 binary bits to be 0010 (i.e., 2)
>> dec2bin(typecast(0.1900, 'uint64'), 64)
ans =
0011111111001000010100011110101110000101000111101011100000000000 <-- Nope!
>> reshape(dec2bin(sscanf(num2hex(0.1900),'%1x'),4)',1,[])
ans =
0011111111001000010100011110101110000101000111101011100001010010 <-- Yes!
The dec2bin( ) call on the whole value got the trailing bits wrong. But converting each hex digit individually we get the expected result.

### Bruno Luong (view profile)

on 12 Sep 2019
Edited by Bruno Luong

### Bruno Luong (view profile)

on 16 Sep 2019 at 10:20

To overcome the limitation of dec2bin at 52 bits
A=[ 0.1900 -0.0300 -0.1300 0 0.1500 -0.0700 0.0500 0.1600 -0.2500 -0.1900]
b = reshape(dec2bin(flipud(reshape(typecast(A,'uint8'),8,[])),8)',64,[])'
ans =
10×64 char array
'0011111111001000010100011110101110000101000111101011100001010010'
'1011111110011110101110000101000111101011100001010001111010111000'
'1011111111000000101000111101011100001010001111010111000010100100'
'0000000000000000000000000000000000000000000000000000000000000000'
'0011111111000011001100110011001100110011001100110011001100110011'
'1011111110110001111010111000010100011110101110000101000111101100'
'0011111110101001100110011001100110011001100110011001100110011010'
'0011111111000100011110101110000101000111101011100001010001111011'
'1011111111010000000000000000000000000000000000000000000000000000'
'1011111111001000010100011110101110000101000111101011100001010010'