algorithm - Custom function failing to convert a decimal to single precision floating point -


here code attempting convert real number approximate single precision floating point representation, educational purposes demonstrate intermediate results:

function [ieee] = mydec2ieee (d) % accepts decimal number, d. % returns 1x32 bit array holding closest ieee representation.     s = getsign(d);                        [e, f] = getcharacteristic(d);    binarye = binstr2binarr(dec2bin(e));  % convert binary integer array.    if numel(binarye) < 8                 % extend exponent bits number.     binarye = [zeros(1, 8 - numel(binarye)), binarye];   end    binaryf = expandfractiontobinary(f);    ieee = [s , binarye, binaryf];  end  function [b] = binstr2binarr (s) % accepts binary character string, s. % returns binary integer array, b.    len = numel(s);   b = zeros(1, len);    = 1 : len     b(i) = s(i) - '0';     end    end    function [b] = expandfractiontobinary(f) % accepts has remained decimal number  % after calculation of exponent, i.e fractional part.   % returns 1x23 binary array approximation of  % fractional part represented sum of negative powers of 2, % (up 22 - nd power).    singleprecision = 22;   b = zeros(1, singleprecision); % binary string store fraction of ieee754.   = 1;                         % exponent of 2; (i+1) -index in binary array.     while f != 0 && <= singleprecision      ith = 1 / (2^(i));      if ith <= f        f = f - ith;        b(i) = 1;                  % include coefficient in sum.      end        = + 1;   end  end    function [e, f] = getcharacteristic (d) % accepts number base10, d. % returns exponent , fraction in d's ieee754 representation, in base10.  % write d in base-2 scientific  notation  % i.e. factor number in range [1, 2] , power of 2.   bias = 127;   = 1;   f = 0;    while ~(f >= 1 && f <= 2)       f = d / (2^(-i));     % pause; % if number > 1 denominator -> 0 , (faster f -> inf)     = + 1;      end      = - 1;  % last check done after incrementation.    e = bias - i;   f = f - 1; end    function [s] = getsign (d) % accepts number in base10, d. % returns sign bit of ieee754 representation.    if d >= 0      s = 0;     else       s = 1;   end   end   

input:

ieee = mydec2ieee(0.085) 

output:

columns 1 through 21:  0   0   1   1   1   1   0   1   1   0   1   0   1   1   1   0   0   0   0   1   0  columns 22 through 31:  1   0   0   0   1   1   1   1   0   1 

however, works decimal numbers in: 0 < d < 1.

questions

what doing wrong?

how should code modified correctly return ieee representations of numbers >= 1 , d <= 0?


note:

implementation based on relation d = (-1)sign * 2exponent - bias * (fraction + 1), fraction = sum (1/2^n), n = 0,...,22; bias = 127.

as confirmed here matlab uses ieee 754 single precision floats.

as such why not let matlab handle internals , this?:

s = dec2bin(typecast(single(0.085),'uint32'), 32) 

which gives:

00111101101011100001010001111011 

this matches required output (from independent check mention) , works values > 1.

if need numeric, rather string, result convert this:

x = s-'0' 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -