Code covered by the BSD License  

Highlights from
Extended Brookshear Machine emulator and assembler

image thumbnail
from Extended Brookshear Machine emulator and assembler by David Young
Emulator and assembler for a simple computer, a teaching aid for computer science courses.

dble2f8(d)
function f = dble2f8(d)
%DBLE2F8 converts from double to Brookshear's 1-byte floating point
%   F = DBLE2F8(D) stores the value of d in floating point format in the
%   rightmost 8 bits of the result, which is actually a uint32

%   Copyright 2008 University of Sussex and David Young

[m, e] = log2(d);
if e < -4 || e > 3
    throw(MException('bmachine:dble2f8:outofrange', ...
        'Number out of range for 8-bit floating point'));
end
f = uint32(floor(16*abs(m)));      % m should be 0 or >= 0.5 and < 1
f = bitor(f, bitshift(e+4, 4));
if d < 0
    f = bitset(f, 8);
end

end

Contact us at files@mathworks.com