From 79ee7306eb8a2b1e1657ca9dd169b860762175c4 Mon Sep 17 00:00:00 2001 From: Peter Gerwinski <peter.gerwinski@hs-bochum.de> Date: Mon, 17 Apr 2023 16:51:53 +0200 Subject: [PATCH] Beispiele 17.4.2023: Gleitkommazahlen --- 20230417/answer.c | 6 ++++++ 20230417/answer.h | 1 + 20230417/floats-01.c | 9 +++++++++ 20230417/floats-01.txt | 15 +++++++++++++++ 20230417/floats-02.c | 10 ++++++++++ 20230417/floats-02.txt | 24 ++++++++++++++++++++++++ 20230417/floats-03.c | 9 +++++++++ 20230417/floats-04.c | 9 +++++++++ 20230417/floats-05.c | 9 +++++++++ 20230417/floats-06.c | 9 +++++++++ 20230417/floats-07.c | 9 +++++++++ 20230417/philosophy.c | 8 ++++++++ 20230417/philosophy.txt | 26 ++++++++++++++++++++++++++ 20230417/sum-01.c | 15 +++++++++++++++ 20230417/sum-02.c | 15 +++++++++++++++ 20230417/sum-03.c | 15 +++++++++++++++ 20230417/sum-04.c | 15 +++++++++++++++ 20230417/sum-05.c | 14 ++++++++++++++ 20230417/sum-05.txt | 20 ++++++++++++++++++++ 20230417/sum-06.c | 15 +++++++++++++++ 20230417/sum-07.c | 8 ++++++++ 20230417/sum-08.c | 18 ++++++++++++++++++ 20230417/sum-09.c | 25 +++++++++++++++++++++++++ 23 files changed, 304 insertions(+) create mode 100644 20230417/answer.c create mode 100644 20230417/answer.h create mode 100644 20230417/floats-01.c create mode 100644 20230417/floats-01.txt create mode 100644 20230417/floats-02.c create mode 100644 20230417/floats-02.txt create mode 100644 20230417/floats-03.c create mode 100644 20230417/floats-04.c create mode 100644 20230417/floats-05.c create mode 100644 20230417/floats-06.c create mode 100644 20230417/floats-07.c create mode 100644 20230417/philosophy.c create mode 100644 20230417/philosophy.txt create mode 100644 20230417/sum-01.c create mode 100644 20230417/sum-02.c create mode 100644 20230417/sum-03.c create mode 100644 20230417/sum-04.c create mode 100644 20230417/sum-05.c create mode 100644 20230417/sum-05.txt create mode 100644 20230417/sum-06.c create mode 100644 20230417/sum-07.c create mode 100644 20230417/sum-08.c create mode 100644 20230417/sum-09.c diff --git a/20230417/answer.c b/20230417/answer.c new file mode 100644 index 0000000..65a1dc2 --- /dev/null +++ b/20230417/answer.c @@ -0,0 +1,6 @@ +#include "answer.h" + +int answer (void) +{ + return 23; +} diff --git a/20230417/answer.h b/20230417/answer.h new file mode 100644 index 0000000..b6777e8 --- /dev/null +++ b/20230417/answer.h @@ -0,0 +1 @@ +extern int answer (void); diff --git a/20230417/floats-01.c b/20230417/floats-01.c new file mode 100644 index 0000000..bb2266b --- /dev/null +++ b/20230417/floats-01.c @@ -0,0 +1,9 @@ +#include <stdio.h> +#include <math.h> + +int main (void) +{ + for (int i = 0; i < 100; i++) + printf ("%d %f", i, sin (i)); + return 0; +} diff --git a/20230417/floats-01.txt b/20230417/floats-01.txt new file mode 100644 index 0000000..15de348 --- /dev/null +++ b/20230417/floats-01.txt @@ -0,0 +1,15 @@ +#include <stdio.h> +#include <math.h> + +int main (void) +{ + for (int i = 0; i < 100; i++) + printf ("%d %f", i, sin (i)); + return 0; +} +cassini/home/peter/bo/2023ss/bs/20230417> gcc -Wall -O floats-01.c -o floats-01 +/usr/bin/ld: /tmp/ccmLktQT.o: in function `main': +floats-01.c:(.text+0x1b): undefined reference to `sin' +collect2: error: ld returned 1 exit status +cassini/home/peter/bo/2023ss/bs/20230417> gcc -Wall -O floats-01.c -lm -o floats-01 +cassini/home/peter/bo/2023ss/bs/20230417> diff --git a/20230417/floats-02.c b/20230417/floats-02.c new file mode 100644 index 0000000..8d76a26 --- /dev/null +++ b/20230417/floats-02.c @@ -0,0 +1,10 @@ +#include <stdio.h> + +extern double sin (double __x) __attribute__ ((__nothrow__ , __leaf__)); extern double __sin (double __x) __attribute__ ((__nothrow__ , __leaf__)); + +int main (void) +{ + for (int i = 0; i < 100; i++) + printf ("%d %f", i, sin (i)); + return 0; +} diff --git a/20230417/floats-02.txt b/20230417/floats-02.txt new file mode 100644 index 0000000..fc311fb --- /dev/null +++ b/20230417/floats-02.txt @@ -0,0 +1,24 @@ +#include <stdio.h> + +extern double sin (double __x) __attribute__ ((__nothrow__ , __leaf__)); extern double __sin (double __x) __attribute__ ((__nothrow__ , __leaf__)); + +int main (void) +{ + for (int i = 0; i < 100; i++) + printf ("%d %f", i, sin (i)); + return 0; +} +cassini/home/peter/bo/2023ss/bs/20230417> gcc -Wall -O floats-02.c +/usr/bin/ld: /tmp/ccpfDJJH.o: in function `main': +floats-02.c:(.text+0x1b): undefined reference to `sin' +collect2: error: ld returned 1 exit status +cassini/home/peter/bo/2023ss/bs/20230417> gcc -Wall -O floats-02.c -c +cassini/home/peter/bo/2023ss/bs/20230417> ls -l floats-02* +-rw-r--r-- 1 peter peter 267 Apr 17 16:03 floats-02.c +-rw-r--r-- 1 peter peter 1656 Apr 17 16:04 floats-02.o +cassini/home/peter/bo/2023ss/bs/20230417> gcc floats-02.o -o floats-02 +/usr/bin/ld: floats-02.o: in function `main': +floats-02.c:(.text+0x1b): undefined reference to `sin' +collect2: error: ld returned 1 exit status +cassini/home/peter/bo/2023ss/bs/20230417> gcc floats-02.o -lm -o floats-02 +cassini/home/peter/bo/2023ss/bs/20230417> diff --git a/20230417/floats-03.c b/20230417/floats-03.c new file mode 100644 index 0000000..69a9360 --- /dev/null +++ b/20230417/floats-03.c @@ -0,0 +1,9 @@ +#include <stdio.h> +#include <math.h> + +int main (void) +{ + for (int i = 0; i < 100; i++) + printf ("%d %f\n", i, sin (i)); + return 0; +} diff --git a/20230417/floats-04.c b/20230417/floats-04.c new file mode 100644 index 0000000..1cfd325 --- /dev/null +++ b/20230417/floats-04.c @@ -0,0 +1,9 @@ +#include <stdio.h> +#include <math.h> + +int main (void) +{ + for (float i = 0; i < 2 * M_PI; i += 0.1) + printf ("%10f%10f\n", i, sin (i)); + return 0; +} diff --git a/20230417/floats-05.c b/20230417/floats-05.c new file mode 100644 index 0000000..2f38d2b --- /dev/null +++ b/20230417/floats-05.c @@ -0,0 +1,9 @@ +#include <stdio.h> +#include <math.h> + +int main (void) +{ + for (float i = 0.0; i < 2.0 * M_PI; i += 0.1) + printf ("%10f%10f\n", i, sin (i)); + return 0; +} diff --git a/20230417/floats-06.c b/20230417/floats-06.c new file mode 100644 index 0000000..8453727 --- /dev/null +++ b/20230417/floats-06.c @@ -0,0 +1,9 @@ +#include <stdio.h> +#include <math.h> + +int main (void) +{ + printf ("2 / 3 = %f\n", 2 / 3); + printf ("2.0 / 3.0 = %f\n", 2.0 / 3.0); + return 0; +} diff --git a/20230417/floats-07.c b/20230417/floats-07.c new file mode 100644 index 0000000..d637df2 --- /dev/null +++ b/20230417/floats-07.c @@ -0,0 +1,9 @@ +#include <stdio.h> +#include <math.h> + +int main (void) +{ + for (float i = 0.0; i < 2.0 * M_PI; i += 0.001) + printf ("%10f%10f\n", i, sin (i)); + return 0; +} diff --git a/20230417/philosophy.c b/20230417/philosophy.c new file mode 100644 index 0000000..e9f508a --- /dev/null +++ b/20230417/philosophy.c @@ -0,0 +1,8 @@ +#include <stdio.h> +#include "answer.h" + +int main (void) +{ + printf ("The answer is %d.\n", answer ()); + return 0; +} diff --git a/20230417/philosophy.txt b/20230417/philosophy.txt new file mode 100644 index 0000000..045880a --- /dev/null +++ b/20230417/philosophy.txt @@ -0,0 +1,26 @@ +cassini/home/peter/bo/2023ss/bs/20230417> cat philosophy.c +#include <stdio.h> +#include "answer.h" + +int main (void) +{ + printf ("The answer is %d.\n", answer ()); + return 0; +} +cassini/home/peter/bo/2023ss/bs/20230417> cat answer.h +extern int answer (void); +cassini/home/peter/bo/2023ss/bs/20230417> gcc -Wall -O philosophy.c -o philosophy +/usr/bin/ld: /tmp/ccVKg5fk.o: in function `main': +philosophy.c:(.text+0x5): undefined reference to `answer' +collect2: error: ld returned 1 exit status +cassini/home/peter/bo/2023ss/bs/20230417> cat answer.c +#include "answer.h" + +int answer (void) +{ + return 23; +} +cassini/home/peter/bo/2023ss/bs/20230417> gcc -Wall -O philosophy.c answer.c -o philosophy +cassini/home/peter/bo/2023ss/bs/20230417> ./philosophy +The answer is 23. +cassini/home/peter/bo/2023ss/bs/20230417> diff --git a/20230417/sum-01.c b/20230417/sum-01.c new file mode 100644 index 0000000..0362b2f --- /dev/null +++ b/20230417/sum-01.c @@ -0,0 +1,15 @@ +#include <stdio.h> +#include <math.h> + +/* 1/1² + 1/2² + 1/3² + ... = pi²/6 */ + +int main (void) +{ + float pi = M_PI; + printf ("%0.9f\n", pi * pi / 6.0); + float S = 0.0; + for (float x = 1; x <= 100; x++) + S += 1.0 / (x * x); + printf ("%0.9f\n", S); + return 0; +} diff --git a/20230417/sum-02.c b/20230417/sum-02.c new file mode 100644 index 0000000..c0fabe8 --- /dev/null +++ b/20230417/sum-02.c @@ -0,0 +1,15 @@ +#include <stdio.h> +#include <math.h> + +/* 1/1² + 1/2² + 1/3² + ... = pi²/6 */ + +int main (void) +{ + float pi = M_PI; + printf ("%0.9f\n", pi * pi / 6.0); + float S = 0.0; + for (float x = 1; x <= 1000000; x++) + S += 1.0 / (x * x); + printf ("%0.9f\n", S); + return 0; +} diff --git a/20230417/sum-03.c b/20230417/sum-03.c new file mode 100644 index 0000000..472d8b7 --- /dev/null +++ b/20230417/sum-03.c @@ -0,0 +1,15 @@ +#include <stdio.h> +#include <math.h> + +/* 1/1² + 1/2² + 1/3² + ... = pi²/6 */ + +int main (void) +{ + float pi = M_PI; + printf ("%0.9f\n", pi * pi / 6.0); + float S = 0.0; + for (float x = 1; x <= 10000000; x++) /* ca. 1/20 s Rechenzeit */ + S += 1.0 / (x * x); + printf ("%0.9f\n", S); + return 0; +} diff --git a/20230417/sum-04.c b/20230417/sum-04.c new file mode 100644 index 0000000..c49d13a --- /dev/null +++ b/20230417/sum-04.c @@ -0,0 +1,15 @@ +#include <stdio.h> +#include <math.h> + +/* 1/1² + 1/2² + 1/3² + ... = pi²/6 */ + +int main (void) +{ + float pi = M_PI; + printf ("%0.9f\n", pi * pi / 6.0); + float S = 0.0; + for (float x = 1; x <= 100000000; x++) /* erwarte: ca. 1/2 s Rechenzeit */ + S += 1.0 / (x * x); + printf ("%0.9f\n", S); + return 0; +} diff --git a/20230417/sum-05.c b/20230417/sum-05.c new file mode 100644 index 0000000..8612524 --- /dev/null +++ b/20230417/sum-05.c @@ -0,0 +1,14 @@ +#include <stdio.h> + +int main (void) +{ + float x = 10000000; + printf ("%f\n", x); + x++; + printf ("%f\n", x); + x = 100000000; + printf ("%f\n", x); + x++; + printf ("%f\n", x); + return 0; +} diff --git a/20230417/sum-05.txt b/20230417/sum-05.txt new file mode 100644 index 0000000..eba8c94 --- /dev/null +++ b/20230417/sum-05.txt @@ -0,0 +1,20 @@ +#include <stdio.h> + +int main (void) +{ + float x = 10000000; + printf ("%f\n", x); + x++; + printf ("%f\n", x); + x = 100000000; + printf ("%f\n", x); + x++; + printf ("%f\n", x); + return 0; +} +cassini/home/peter/bo/2023ss/bs/20230417> gcc -Wall -O sum-05.c -o sum-05 +cassini/home/peter/bo/2023ss/bs/20230417> ./sum-05 +10000000.000000 +10000001.000000 +100000000.000000 +100000000.000000 diff --git a/20230417/sum-06.c b/20230417/sum-06.c new file mode 100644 index 0000000..477e087 --- /dev/null +++ b/20230417/sum-06.c @@ -0,0 +1,15 @@ +#include <stdio.h> +#include <math.h> + +/* 1/1² + 1/2² + 1/3² + ... = pi²/6 */ + +int main (void) +{ + float pi = M_PI; + printf ("%0.9f\n", pi * pi / 6.0); + float S = 0.0; + for (int i = 1; i <= 100000000; i++) /* erwarte: ca. 1/2 s Rechenzeit */ + S += 1.0 / (i * i); + printf ("%0.9f\n", S); + return 0; +} diff --git a/20230417/sum-07.c b/20230417/sum-07.c new file mode 100644 index 0000000..af48243 --- /dev/null +++ b/20230417/sum-07.c @@ -0,0 +1,8 @@ +#include <stdio.h> + +int main (void) +{ + int i = 65536; + printf ("%d\n", i * i); + return 0; +} diff --git a/20230417/sum-08.c b/20230417/sum-08.c new file mode 100644 index 0000000..2fec1bb --- /dev/null +++ b/20230417/sum-08.c @@ -0,0 +1,18 @@ +#include <stdio.h> +#include <math.h> + +/* 1/1² + 1/2² + 1/3² + ... = pi²/6 */ + +int main (void) +{ + float pi = M_PI; + printf ("%0.9f\n", pi * pi / 6.0); + float S = 0.0; + for (int i = 1; i <= 100000000; i++) /* zum Hochzählen: int (ginge nicht mit float) */ + { + float x = i; + S += 1.0 / (x * x); /* zum Quadrieren: float (ginge nicht mit int) */ + } + printf ("%0.9f\n", S); + return 0; +} diff --git a/20230417/sum-09.c b/20230417/sum-09.c new file mode 100644 index 0000000..e2c732f --- /dev/null +++ b/20230417/sum-09.c @@ -0,0 +1,25 @@ +#include <stdio.h> +#include <math.h> + +/* 1/1² + 1/2² + 1/3² + ... = pi²/6 */ + +int main (void) +{ + float pi = M_PI; + printf ("%0.9f\n", pi * pi / 6.0); + float S = 0.0; + for (int i = 1; i <= 100000000; i++) + { + float x = i; + S += 1.0 / (x * x); /* kleine Zahl zu großer addieren: verschwindet */ + } + printf ("%0.9f\n", S); + S = 0.0; + for (int i = 100000000; i >= 1; i--) /* Zuerst die kleinen Zahlen addieren, dann die großen. */ + { + float x = i; + S += 1.0 / (x * x); + } + printf ("%0.9f\n", S); + return 0; +} -- GitLab