From a748a17c7285910b32617850cb52d9a048bcbd46 Mon Sep 17 00:00:00 2001 From: Hong-Phuc Bui <hong-phuc.bui@htwsaar.de> Date: Sun, 21 Apr 2024 06:08:01 +0200 Subject: [PATCH] add bsp while --- python-grundlage/find-machinell-epsilon.py | 13 ++++++ python-grundlage/sqrt-newton.py | 10 +++++ python-grundlage/c/size_of_test.c | 2 README.md | 4 ++ python-grundlage/c/quake_iii_fisqrt.c | 32 ++++++++++++++++ 5 files changed, 60 insertions(+), 1 deletions(-) diff --git a/README.md b/README.md index 0e69485..6184510 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # Hier ist eine Sammlung von Beispiel-Projekts Sommersemester 2024 + +## python-grundlage + +Das notwendigste Wissen über Python diff --git a/python-grundlage/c/quake_iii_fisqrt.c b/python-grundlage/c/quake_iii_fisqrt.c new file mode 100644 index 0000000..b50c9b2 --- /dev/null +++ b/python-grundlage/c/quake_iii_fisqrt.c @@ -0,0 +1,32 @@ +/* explain https://www.youtube.com/watch?v=p8u_k2LIZyo */ + +#include <stdio.h> + +float Q_rsqrt(float number) +{ + long i; + float x2, y; + const float threehalfs = 1.5F; + + x2 = number*0.5F; + y = number; + i = *(long *) &y; + i = 0x5f3759df - (i >> 1); + printf("i = %lu\n", i); + y = *( float *) &i; + printf("y = %f\n", y); + y = y * (threehalfs - (x2*y*y)); + + return y; +} + +int main() +{ + float n = 5.0; + float f = Q_rsqrt(n); + printf("1/sqrt(%f) = %f", n, f); + return 0; +} + + + diff --git a/python-grundlage/c/size_of_test.c b/python-grundlage/c/size_of_test.c index 126d595..34bf883 100644 --- a/python-grundlage/c/size_of_test.c +++ b/python-grundlage/c/size_of_test.c @@ -5,7 +5,7 @@ const unsigned int max = 4294967295; const unsigned int max_as_negativ = -1; printf("%d\n", max); // expected output: -1 - printf("%u\n", max); + printf("%u\n", max); // expected output: 4294967295 (32 bits) bool same = max == max_as_negativ; if(same) { diff --git a/python-grundlage/find-machinell-epsilon.py b/python-grundlage/find-machinell-epsilon.py new file mode 100644 index 0000000..4fd47a5 --- /dev/null +++ b/python-grundlage/find-machinell-epsilon.py @@ -0,0 +1,13 @@ +# Credit: https://stackoverflow.com/a/57579379 + +import sys + +eps = 1.0 +while eps + 1 > 1: + eps /= 2 +eps *= 2 +sys_eps = sys.float_info.epsilon +print(f"The calculated epsilon: {eps}") +print(f" The system epsilon: {sys_eps}") + + diff --git a/python-grundlage/sqrt-newton.py b/python-grundlage/sqrt-newton.py new file mode 100644 index 0000000..00cfa07 --- /dev/null +++ b/python-grundlage/sqrt-newton.py @@ -0,0 +1,10 @@ +import sys + +n = float(sys.argv[1]) +EPSILON = sys.float_info.epsilon + +t = n +while abs(t - n/t) > (EPSILON*t): + t = (n / t + t) / 2 + +print(f"sqrt({n}) ~= {t}") -- Gitblit v1.10.0-SNAPSHOT