68 lines
1.1 KiB
Mathematica
68 lines
1.1 KiB
Mathematica
|
% EX3 c)
|
||
|
function [d, v, t] = flp(b, m, n, x)
|
||
|
% GETS:
|
||
|
% b: basis
|
||
|
% m: mantissa length
|
||
|
% n: exponent length
|
||
|
% x: number to convert
|
||
|
|
||
|
% RETURNS:
|
||
|
% d: mantissa coefficients
|
||
|
% v: coefficients of the exponent
|
||
|
% t: sign
|
||
|
|
||
|
# calculate exponent and mantissa
|
||
|
exponent = 0
|
||
|
mantissa = x
|
||
|
while mantissa > 1
|
||
|
mantissa /= b
|
||
|
exponent += 1
|
||
|
end
|
||
|
while (mantissa * b) < 1
|
||
|
mantissa *= b
|
||
|
exponent -= 1
|
||
|
end
|
||
|
|
||
|
inverted = convert2basis(mantissa^-1, b)
|
||
|
inverted(end)=[]
|
||
|
val_man = flip(inverted)
|
||
|
val_exp = convert2basis(abs(exponent), b)
|
||
|
t = sign(exponent)
|
||
|
|
||
|
# pad result
|
||
|
end
|
||
|
|
||
|
function a = convert2basis(n, b)
|
||
|
if (b < 2)
|
||
|
disp('basis needs to be at least 2!')
|
||
|
return
|
||
|
end
|
||
|
|
||
|
v = [];
|
||
|
|
||
|
while n > 0
|
||
|
v = [mod(n, b), v];
|
||
|
n = floor(n / b);
|
||
|
end
|
||
|
|
||
|
a = v
|
||
|
end
|
||
|
|
||
|
function b = convertDecimal(n, b)
|
||
|
if (b < 2)
|
||
|
disp('basis needs to be at least 2!')
|
||
|
return
|
||
|
end
|
||
|
|
||
|
while n < 1
|
||
|
|
||
|
|
||
|
end
|
||
|
|
||
|
function res = padArr(arr, len)
|
||
|
for i = length(arr):len
|
||
|
arr(i+1) = 0
|
||
|
end
|
||
|
res = arr
|
||
|
end
|