function v = longhex(u, digits) %LONGHEX Long hexadecimal format. % V=LONGHEX(U,N) takes a positive decimal fraction less than 1 and converts % it to a binary fraction of length digits. It then converts it to a IEEE % double precision number but possibly with a longer mantissa. U is a vector % of decimal digits in the range [0-9] with the decimal point to the 'left' % of U(1). N is the length in binary digits to save. V is a string of hexidecimal % digits of the IEEE double number, but with possibly longer mantiassa % % Algorithm: Knuth, Vol. 2, p. 319, Method 2a % By: Martin Knapp-Cordes % The MathWorks, Inc. % % Date: Apr 2001 - 09 u2 = zeros(size(u,1)+1,1); u2(2:end) = u; b = zeros(digits,1); for i=1:digits u2 = mult2(u2); b(i) = u2(1); u2(1) = 0; end % Always produce an least an IEEE double. o = find(b == 1); base = o(1); s = size(b,1); v = dec2hex(1023-base,3); n = max(ceil((s - base) / 4), 13); v2 = zeros(n * 4,1); v2(1:(s - base)) = b((base+1):end); for i=1:4:n*4 v = [ v dec2hex(bin2dec(sprintf('%d',v2(i:i+3))))]; end v = lower(v); %---------------------------------------------------------------- function u2 = mult2(u) % Multiply by 2 with carry. u2 = 2 .* u; c = (u2>=10); u2(1:end-1) = u2(1:end-1) + c(2:end) - c(1:end-1) .* 10; u2(end) = u2(end) - c(end) .* 10