I am writing a script to convert decimal to floating point binary IEEE754. However, my answer returned number binary values (e.g. 1009999900).
11 views (last 30 days)
Show older comments
As being new to matlab, Im trying to create a program to convert decimal to floating binary from first principle. However when specify minimum output string of 32 characters, they did not return binary values (e.g. 0 or 1) but instead containing 9s. Could this be due to rounding off because of the floating form (decimal) by the program?
code
%%Converting input of base 10
format long g
prompt = 'Input value of x: ';
x=input(prompt);
%Converting of mantissa (x_1) to between 1 and 2 of base-2 scientific notation
n_exponent=0;
x_1=x;
if abs(x)<1
while abs(x_1)<1
n_exponent=n_exponent-1;
x_1=x/(2.^n_exponent);
end
else
while abs(x_1)>=2
n_exponent=n_exponent+1;
x_1=x/(2.^n_exponent);
end
end
%Exponent to 8-bit binary (single precision 127)
exp_127=n_exponent+127;
bit8=0;
order=1;
for n=1:8
bit8=rem(exp_127,2)*order+bit8;
exp_127=fix(exp_127/2);
order=order*10;
end
% Converting fraction to 23-bit mantissa (leading 1 hidden)
fraction=x_1-1;
bit23=0;
order=1;
for n=1:23
fraction=fraction*2;
bit23=order*fix(fraction)+bit23;
fraction=rem(fraction,1);
order=order*10;
end
%Determining sign of x
sign=0;
if x<0
sign=1;
end
%Full IEEE 754 binary floating point
output=sign*(10.^31)+bit8*(10.^23)+bit23;
sprintf('%032.f',output)
ans = 00111101101011109990000000000000
1 Comment
SHAHMUSTAFA MUJAWAR
on 10 Apr 2018
hi, Kelvin this code is working fine for the whole number but for fraction values like (-0.001) it is showing some altered value in mantissa part and its reverse conversion in online 'single precision to decimal' calculator is (-0.0014), can you help me out from this problem
Accepted Answer
Guillaume
on 6 Feb 2017
Edited: Guillaume
on 6 Feb 2017
You cannot acurately store a decimal number greater than flintmax (~9e15) as double. Any integer above that will be rounded to the nearest decimal representable as double. So neither your bit23 (max.value > 1e23) and certainly not your output (max.value > 1e64) can be acurately stored as double.
Even as uint64, the maximum value that can be stored ( intmax('uint64'))is about 1e19, so still not enough.
My suggestion, store your bits as a vector of 0 and 1 or as a char vector of '0' and '1', e.g.:
bit23 = zeros(1, 23); %bit8 = zeros(1, 8)
for n = 1:23
bit23(n) = fix(fraction); %bit8(n) = rem(exp_127,2);
%...
fprintf('%d', [sign, fliplr(bit8), fliplr(bit23)])
3 Comments
Rajashree Annapillai
on 25 Aug 2018
Hello sir, I want to know the concept of this code you have mentioned, especially the exponent and the mantissa part.As this method(for mantissa and exponent) mentioned in the code differs from the normal calculation,I can't able to understand the concept of this code.Can you please help me.
Aik Meng SOH
on 26 Aug 2020
Tried incorporating Guillaume's recommendation into the code (like below). +ve whole numbers run great. But when it comes to decimals like 2.4, there is a slight discrepancy with the last digiits.
From code: 01000000000110011001100110011001.
From online converters: 01000000000110011001100110011010.
Another question is regarding -ve numbers.
From code: 110000110-5-1-100000000000000000000
From online converters:11000011011100000000000000000000
What's wrong with the quote for the 23 bit portion?
format long g
prompt = 'Input value of x: ';
x=input(prompt);
%Converting of mantissa (x_1) to between 1 and 2 of base-2 scientific notation
n_exponent=0;
x_1=x;
if abs(x)<1
while abs(x_1)<1
n_exponent=n_exponent-1;
x_1=x/(2.^n_exponent);
end
else
while abs(x_1)>=2
n_exponent=n_exponent+1;
x_1=x/(2.^n_exponent);
end
end
%Exponent to 8-bit binary (single precision 127)
exp_127=n_exponent+127;
bit8 = zeros(1, 8);
order=1;
for n=1:8
bit8(n) = rem(exp_127,2);
exp_127=fix(exp_127/2);
order=order*10;
end
% Converting fraction to 23-bit mantissa (leading 1 hidden)
fraction=x_1-1;
bit23 = zeros(1, 23);
order=1;
for n=1:23
fraction=fraction*2;
bit23(n) = fix(fraction);
fraction=rem(fraction,1);
order=order*10;
end
%Determining sign of x
sign=0;
if x<0
sign=1;
end
%Full IEEE 754 binary floating point
fprintf('%d', [sign, fliplr(bit8), (bit23)])
More Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!