applied_numerics_uulm/w1/matlab/value.m

39 lines
728 B
Mathematica
Raw Normal View History

2022-05-15 22:14:40 +02:00
% EX3 a)
function x = value(b, d, v, t)
% b: basis
% d: mantissa coefficients
% v: coefficients of the exponent
% t: sign
if b < 2
disp('basis needs to be at least 2!')
return
elseif !(t == -1 || t == 1)
disp('t needs to be 1 or -1!')
return
end
exponent = (t * unConvert(v, b))
prefactor = b ^ exponent
mantissa = unConvertMantissa(d, b)
x = prefactor * mantissa
end
function val = unConvert(n, b)
nums = flip(n)
res = 0
for i = 1:length(n)
res += nums(i) * b^(i-1)
end
val = res
end
function val = unConvertMantissa(m, b)
res = 0
for i = 1:length(m)
res += m(i) * b^(-i)
end
val = res
end