function b=double2bin(n) %DOUBLE2BIN Convert double precision floating point to binary string. % DOUBLE2BIN(N) returns the binary representation of the double precision % floating point number N in the form [sign][exponent][mantissa]. % % The binary representation of F is computed, not converted from its % hexadecimal representation as in NUM2BIN. % % See also NUM2BIN, DEC2BIN, BIN2DEC, HEX2DEC, BASE2DEC. % Francisco J. Beron-Vera % $Revision: 1.3 $ $Date: 2006/08/30 16:19:35 $ % Convert integer part to binary string. i=abs(fix(n)); ib=[]; while i~=0 ib=[ib num2str(rem(i,2))]; i=fix(i/2); end if isempty(ib) ib=['0']; end ib=fliplr(ib); % Convert fractional part to binary string. f=abs(n-fix(n)); fb=[]; for j=1:53 fb=[fb num2str(fix(f*2))]; f=f*2-fix(f*2); end % Output as a string of the form [sign][exponent][mantissa]. if n>0 s='0'; else s='1'; end if length(ib)==1 if str2num(ib)==0 e=[dec2bin(0) dec2bin(1022)]; m=fb(2:end); else e=[dec2bin(0) dec2bin(1023)]; m=fb; end else e=dec2bin(length(ib)+1022); m=[ib(2:end) fb]; end b=['[' s '][' e '][' m(1:52) ']'];