feat: added w1 scripts
This commit is contained in:
parent
b17c8b97d4
commit
abeb814912
17
w1/matlab/convert2basis.m
Normal file
17
w1/matlab/convert2basis.m
Normal file
@ -0,0 +1,17 @@
|
||||
% EX2 a)
|
||||
|
||||
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
|
67
w1/matlab/flp.m
Normal file
67
w1/matlab/flp.m
Normal file
@ -0,0 +1,67 @@
|
||||
% 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
|
21
w1/matlab/interval.practise.m
Executable file
21
w1/matlab/interval.practise.m
Executable file
@ -0,0 +1,21 @@
|
||||
function interval(x, a, b)
|
||||
if (nargin < 2)
|
||||
a = 7;
|
||||
b = 12;
|
||||
end
|
||||
if (a > b)
|
||||
bNew = a
|
||||
a = b
|
||||
b = bNew
|
||||
end
|
||||
|
||||
if (a <= x && x <= b)
|
||||
disp('x ist im Intervall')
|
||||
elseif (x < a)
|
||||
disp('x ist unterhalb des Intervals')
|
||||
elseif (x > b)
|
||||
disp('x ist überhalb des Intervals')
|
||||
else
|
||||
disp('x ist keine Nummer')
|
||||
end
|
||||
end
|
91
w1/matlab/testConvert2basis.m
Normal file
91
w1/matlab/testConvert2basis.m
Normal file
@ -0,0 +1,91 @@
|
||||
% Angewandte Numerik 1, SoSe 2022
|
||||
% Uebungsblatt 01, Aufgabe 02: Darstellung natuerlicher Zahlen
|
||||
%
|
||||
% Testprogramm fuer die Funktion a = convert2basis(n, b)
|
||||
%
|
||||
% Letzte Aenderung: 22.04.2022
|
||||
|
||||
|
||||
|
||||
%% Initialisierung
|
||||
|
||||
clearvars;
|
||||
close all;
|
||||
clc;
|
||||
|
||||
fprintf('\n');
|
||||
fprintf('Angewandte Numerik 1, Sommersemester 2022\n');
|
||||
fprintf('Uebungsblatt 1, Aufgabe 02: Darstellung natuerlicher Zahlen\n');
|
||||
fprintf('\n');
|
||||
|
||||
|
||||
|
||||
%% Definition und Durchfuehrung der Testfaelle
|
||||
|
||||
testfall = 0;
|
||||
while true % alle Testfaelle untersuchen
|
||||
testfall = testfall + 1; % naechster Testfall
|
||||
|
||||
|
||||
%% alle Testfaelle definieren
|
||||
|
||||
switch testfall
|
||||
case 1 % Testfall 1: b = 2, n = 30
|
||||
b = 2;
|
||||
n = 30;
|
||||
a = [1 1 1 1 0];
|
||||
case 2 % Testfall 2: b = 2, n = 31
|
||||
b = 2;
|
||||
n = 31;
|
||||
a = [1 1 1 1 1];
|
||||
case 3 % Testfall 3: b = 2, n = 32
|
||||
b = 2;
|
||||
n = 32;
|
||||
a = [1 0 0 0 0 0];
|
||||
case 4 % Testfall 4: b = 2, n = 33
|
||||
b = 2;
|
||||
n = 33;
|
||||
a = [1 0 0 0 0 1];
|
||||
case 5 % Testfall 5: b = 2, n = 42
|
||||
b = 2;
|
||||
n = 42;
|
||||
a = [1 0 1 0 1 0];
|
||||
case 6 % Testfall 6: b = 2, n = 134110
|
||||
b = 2;
|
||||
n = 134110;
|
||||
a = [1 0 0 0 0 0 1 0 1 1 1 1 0 1 1 1 1 0];
|
||||
case 7 % Testfall 7: b = 8, n = 27
|
||||
b = 8;
|
||||
n = 27;
|
||||
a = [3 3];
|
||||
case 8 % Testfall 8: b = 8, n = 3652
|
||||
b = 8;
|
||||
n = 3652;
|
||||
a = [7 1 0 4];
|
||||
case 9 % Testfall 9: b = 8, n = 46807
|
||||
b = 8;
|
||||
n = 46807;
|
||||
a = [1 3 3 3 2 7];
|
||||
case 10 % Testfall 10: b = 10, n = 3121
|
||||
b = 10;
|
||||
n = 3121;
|
||||
a = [3 1 2 1];
|
||||
case 11 % Testfall 11: b = 10, n = 192310030133
|
||||
b = 10;
|
||||
n = 192310030133;
|
||||
a = [1 9 2 3 1 0 0 3 0 1 3 3];
|
||||
otherwise
|
||||
break; % keine Testfaelle mehr vorhanden
|
||||
end
|
||||
|
||||
|
||||
%% Testfall durchfuehren und Ergebnis ausgeben
|
||||
|
||||
spezString = 'Testfall %2d (b = %2d, n = %12d): %s.\n';
|
||||
if max(abs(a - convert2basis(n, b))) == 0
|
||||
fprintf(spezString, testfall, b, n, 'Bestanden');
|
||||
else
|
||||
fprintf(2, spezString, testfall, b, n, 'Fehlgeschlagen')
|
||||
end
|
||||
|
||||
end
|
105
w1/matlab/testFlp.m
Normal file
105
w1/matlab/testFlp.m
Normal file
@ -0,0 +1,105 @@
|
||||
% Angewandte Numerik 1, SoSe 2022
|
||||
% Uebungsblatt 01, Aufgabe 03: Wert einer Gleitpunktdarstellung
|
||||
%
|
||||
% Testprogramm fuer die Funktion [d, v, t] = flp(b, m, n, x)
|
||||
%
|
||||
% Letzte Aenderung: 22.04.2022
|
||||
|
||||
|
||||
|
||||
%% Initialisierung
|
||||
|
||||
clearvars;
|
||||
close all;
|
||||
clc;
|
||||
|
||||
fprintf('\n');
|
||||
fprintf('Angewandte Numerik 1, Sommersemester 2022\n');
|
||||
fprintf('Uebungsblatt 1, Aufgabe 3d: Gleitpunkt-Darstellung\n');
|
||||
fprintf('\n');
|
||||
|
||||
|
||||
|
||||
%% Definition und Durchfuehrung der Testfaelle
|
||||
|
||||
testfall = 0;
|
||||
while true % alle Testfaelle untersuchen
|
||||
testfall = testfall + 1; % naechster Testfall
|
||||
|
||||
|
||||
%% alle Testfaelle definieren
|
||||
|
||||
switch testfall
|
||||
case 1 % Testfall 1
|
||||
b = 2; % Basis
|
||||
m = 8; % Laenge der Mantisse
|
||||
n = 3; % Laenge des Exponenten
|
||||
x = 27.375; % zu konvertierende Zahl
|
||||
dSoll = [1 1 0 1 1 0 1 1]; % Mantisse
|
||||
vSoll = [1 0 1]; % Exponent
|
||||
tSoll = 1; % Vorzeichen Exponent
|
||||
case 2 % Testfall 2
|
||||
b = 8; % Basis
|
||||
m = 8; % Laenge der Mantisse
|
||||
n = 3; % Laenge des Exponenten
|
||||
x = 27.375; % zu konvertierende Zahl
|
||||
dSoll = [3 3 3 0 0 0 0 0]; % Mantisse
|
||||
vSoll = [0 0 2]; % Exponent
|
||||
tSoll = 1; % Vorzeichen Exponent
|
||||
case 3 % Testfall 3
|
||||
b = 2; % Basis
|
||||
m = 10; % Laenge der Mantisse
|
||||
n = 3; % Laenge des Exponenten
|
||||
x = 9.140625; % zu konvertierende Zahl
|
||||
dSoll = [1 0 0 1 0 0 1 0 0 1]; % Mantisse
|
||||
vSoll = [1 0 0]; % Exponent
|
||||
tSoll = 1; % Vorzeichen Exponent
|
||||
case 4 % Testfall 4
|
||||
b = 8; % Basis
|
||||
m = 5; % Laenge der Mantisse
|
||||
n = 3; % Laenge des Exponenten
|
||||
x = 9.140625; % zu konvertierende Zahl
|
||||
dSoll = [1 1 1 1 0]; % Mantisse
|
||||
vSoll = [0 0 2]; % Exponent
|
||||
tSoll = 1; % Vorzeichen Exponent
|
||||
case 5 % Testfall 5
|
||||
b = 2; % Basis
|
||||
m = 3; % Laenge der Mantisse
|
||||
n = 2; % Laenge des Exponenten
|
||||
x = 0.375; % zu konvertierende Zahl
|
||||
dSoll = [1 1 0]; % Mantisse
|
||||
vSoll = [0 1]; % Exponent
|
||||
tSoll = -1; % Vorzeichen Exponent
|
||||
case 6 % Testfall 6
|
||||
b = 8; % Basis
|
||||
m = 5; % Laenge der Mantisse
|
||||
n = 2; % Laenge des Exponenten
|
||||
x = 0.0157470703125; % zu konvertierende Zahl
|
||||
dSoll = [1 0 0 4 0]; % Mantisse
|
||||
vSoll = [0 1]; % Exponent
|
||||
tSoll = -1; % Vorzeichen Exponent
|
||||
case 7 % Testfall 7
|
||||
b = 2; % Basis
|
||||
m = 3; % Laenge der Mantisse
|
||||
n = 3; % Laenge des Exponenten
|
||||
x = 0.0625; % zu konvertierende Zahl
|
||||
dSoll = [1 0 0]; % Mantisse
|
||||
vSoll = [0 1 1]; % Exponent
|
||||
tSoll = -1; % Vorzeichen Exponent
|
||||
otherwise
|
||||
break; % keine Testfaelle mehr vorhanden
|
||||
end
|
||||
|
||||
|
||||
%% Testfall durchfuehren und Ergebnis ausgeben
|
||||
|
||||
[d, v, t] = flp(b, m, n, x);
|
||||
|
||||
spezString = 'Testfall %d: %s.\n';
|
||||
if max(abs([d - dSoll, v - vSoll, t - tSoll])) == 0
|
||||
fprintf(spezString, testfall, 'Bestanden');
|
||||
else
|
||||
fprintf(2, spezString, testfall, 'Fehlgeschlagen');
|
||||
end
|
||||
|
||||
end
|
67
w1/matlab/testValue.m
Normal file
67
w1/matlab/testValue.m
Normal file
@ -0,0 +1,67 @@
|
||||
% Angewandte Numerik 1, SoSe 2022
|
||||
% Uebungsblatt 01, Aufgabe 03: Wert einer Gleitpunktdarstellung
|
||||
%
|
||||
% Testprogramm fuer die Funktion x = value(b, d, v, t)
|
||||
%
|
||||
% Letzte Aenderung: 22.04.2022
|
||||
|
||||
|
||||
|
||||
%% Initialisierung
|
||||
|
||||
clearvars;
|
||||
close all;
|
||||
clc;
|
||||
|
||||
tol = 1e-14; % Geforderte Genauigkeit der Berechnungen
|
||||
|
||||
fprintf('\n');
|
||||
fprintf('Angewandte Numerik 1, Sommersemester 2022\n');
|
||||
fprintf('Uebungsblatt 1, Aufgabe 3b: Gleitpunkt-Darstellung\n');
|
||||
fprintf('\n');
|
||||
|
||||
|
||||
|
||||
%% Definition und Durchfuehrung der Testfaelle
|
||||
|
||||
testfall = 0;
|
||||
while true % alle Testfaelle untersuchen
|
||||
testfall = testfall + 1; % naechster Testfall
|
||||
|
||||
|
||||
%% alle Testfaelle definieren
|
||||
|
||||
switch testfall
|
||||
case 1 % Testfall 1
|
||||
b = 2; % Basis
|
||||
d = [1 0 1 0 0 1 0 0]; % Mantisse
|
||||
v = [0 0 1 0 0]; % Exponent
|
||||
t = 1; % Vorzeichen Exponent
|
||||
x = 10.25; % Wert der Zahl
|
||||
case 2 % Testfall 2
|
||||
b = 2; % Basis
|
||||
d = [1 0 1]; % Mantisse
|
||||
v = [0 0 1]; % Exponent
|
||||
t = -1; % Vorzeichen Exponent
|
||||
x = 0.3125; % Wert der Zahl
|
||||
case 3 % Testfall 3
|
||||
b = 8; % Basis
|
||||
d = [7 5 0]; % Mantisse
|
||||
v = [0 0 1]; % Exponent
|
||||
t = -1; % Vorzeichen Exponent
|
||||
x = 0.119140625; % Wert der Zahl
|
||||
otherwise
|
||||
break; % keine Testfaelle mehr vorhanden
|
||||
end
|
||||
|
||||
|
||||
%% Testfall durchfuehren und Ergebnis ausgeben
|
||||
|
||||
spezString = 'Testfall %d: %s.\n';
|
||||
if abs(x - value(b,d,v,t)) < tol
|
||||
fprintf(spezString, testfall, 'Bestanden');
|
||||
else
|
||||
fprintf(2, spezString, testfall, 'Fehlgeschlagen');
|
||||
end
|
||||
|
||||
end
|
38
w1/matlab/value.m
Normal file
38
w1/matlab/value.m
Normal file
@ -0,0 +1,38 @@
|
||||
% 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
|
Loading…
Reference in New Issue
Block a user