Skip to content
Snippets Groups Projects
Commit e422383c authored by Sander Mathijs van Veen's avatar Sander Mathijs van Veen
Browse files

Merged report and makefile

parents 0ee657f5 db6a27c2
No related branches found
No related tags found
No related merge requests found
......@@ -13,3 +13,4 @@ extra_precision.tex
speed.tex
sum.tex
Makefile.tex
kahan
......@@ -4,14 +4,17 @@ SPEED_TYPES=float double LD
SUM_TYPES=float double
OPS=ADD DIV MULT SQRT
all: fp speed highlight report.pdf sum pr
all: fp speed highlight report.pdf sum kahan pr
highlight: floating_point.tex extra_precision.tex \
speed.tex sum.tex Makefile.tex
speed.tex sum.tex kahan_sum.tex Makefile.tex
s%.tex: s%.c
pygmentize -O style=colorful -o $@ $^
kahan_sum.tex: kahan_sum.c
pygmentize -O style=colorful -o $@ $^
Makefile.tex: Makefile
pygmentize -O style=colorful -o $@ $^
......@@ -44,11 +47,14 @@ fp: floating_point.o
sum: sum.c
for t in $(SUM_TYPES); do \
sed "s#{TYPE}#$$t#" $^ > sum.$$t.c; \
gcc $(FLAGS) -o sum.$$t sum.$$t.c; \
$(CC) $(FLAGS) -o sum.$$t sum.$$t.c; \
rm sum.$$t.c; \
done;
touch $@
kahan: kahan_sum.o
$(CC) $(FLAGS) -o $@ $^
%.o: %.c
$(CC) $(FLAGS) -o $@ -c $^
......@@ -57,4 +63,4 @@ sum: sum.c
clean:
rm -vf *.o *.i *.s fp pr fd* speed speed.*.* floating_point \
report.pdf *.aux *.log *.toc sum sum.float sum.double
report.pdf *.aux *.log *.toc sum sum.float sum.double kahan
#include <stdlib.h>
#include <stdio.h>
float kahan_sum(int N) {
float sum = 0.0, c = 0.0, t, y;
for( int i = 1; i <= N; i++ ) {
y = 1.0/i - c;
t = sum + y;
c = (t - sum) - y;
sum = t;
}
return sum;
}
int main(void) {
printf("N = 1e8: %f\n", kahan_sum(1e8));
printf("N = 2e8: %f\n", kahan_sum(2e8));
return 0;
}
......@@ -112,13 +112,13 @@ We've calculated $\sum_{i=1}^{N}\frac{1}{i}$ for $N = 10^8$ and $N = 2 \cdot
\texttt{float} and \texttt{double}. The results of this are in the table below.
\begin{table}[H]
\begin{tabular}{l|lll}
Type & N & Forward & Backward \\
\begin{tabular}{l|llll}
Type & N & Forward & Backward & Kahan\\
\hline
\texttt{float} & $10^8$ & $15.40368$ & $18.80792$ \\
\texttt{float} & $2 \cdot 10^8$ & $15.40368$ & $18.80792$ \\
\texttt{double} & $10^8$ & $18.99790$ & $18.99790$ \\
\texttt{double} & $2 \cdot 10^8$ & $19.69104$ & $19.69104$ \\
\texttt{float} & $10^8$ & $15.40368$ & $18.80792$ & $18.99790$ \\
\texttt{float} & $2 \cdot 10^8$ & $15.40368$ & $18.80792$ & $19.69104$ \\
\texttt{double} & $10^8$ & $18.99790$ & $18.99790$ & \\
\texttt{double} & $2 \cdot 10^8$ & $19.69104$ & $19.69104$ & \\
\end{tabular}
\caption{Results of various summation approaches on floats and doubles.}
\end{table}
......@@ -149,6 +149,8 @@ Type & N & Forward & Backward \\
to the same problem as described above: all numbers in $[\frac{1}{10^8},
\frac{1}{2 \cdot 10^8}]$ are also represented as zero and therefore not added
to the result.
\item To improve the precision of the \texttt{float} data type summation, we
implemented the Kahan summation algorithm.
\end{itemize}
% }}}
......@@ -193,6 +195,13 @@ last : 2.6910298253667153112189680541632696986198425292968750000
% }}}
\section{kahan\_sum.c} % {{{
\label{sec:kahan_sum.c}
\input{kahan_sum}
% }}}
\section{sum.c} % {{{
\label{sec:sum.c}
......
......@@ -23,12 +23,12 @@ int main(void) {
puts("Using type {TYPE}.");
puts("Forward summation:");
printf("N = 1e8: %e\n", sum_forward(1e8));
printf("N = 2e8: %e\n", sum_forward(2e8));
printf("N = 1e8: %f\n", sum_forward(1e8));
printf("N = 2e8: %f\n", sum_forward(2e8));
puts("Backward summation:");
printf("N = 1e8: %e\n", sum_backward(1e8));
printf("N = 2e8: %e\n", sum_backward(2e8));
printf("N = 1e8: %f\n", sum_backward(1e8));
printf("N = 2e8: %f\n", sum_backward(2e8));
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment