c-programming

Simple C programs
git clone git://git.laack.co/c-programming.git
Log | Files | Refs

commit 040fdaedb0dd8c830129964ee040fd239d0c106c
parent 87dbfe6155ceccbcb4a8d5aa0a3c9b43d9e0046c
Author: Andrew Laack <andrew@laack.co>
Date:   Sat, 17 Oct 2026 18:08:01 -0500

Refactoring

Diffstat:
Dperf-aware-programming/haversine/.gen.py.swo | 0
Dperf-aware-programming/haversine/.gen.py.swp | 0
Dperf-aware-programming/haversine/.main.c.swp | 0
Mperf-aware-programming/haversine/Makefile | 2+-
Mperf-aware-programming/haversine/gen.py | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
5 files changed, 65 insertions(+), 5 deletions(-)

diff --git a/perf-aware-programming/haversine/.gen.py.swo b/perf-aware-programming/haversine/.gen.py.swo Binary files differ. diff --git a/perf-aware-programming/haversine/.gen.py.swp b/perf-aware-programming/haversine/.gen.py.swp Binary files differ. diff --git a/perf-aware-programming/haversine/.main.c.swp b/perf-aware-programming/haversine/.main.c.swp Binary files differ. diff --git a/perf-aware-programming/haversine/Makefile b/perf-aware-programming/haversine/Makefile @@ -1,4 +1,4 @@ gen: - python3 gen.py out.json 1000 + python3 gen.py out/out.json 100 clean: rm -rf out.json diff --git a/perf-aware-programming/haversine/gen.py b/perf-aware-programming/haversine/gen.py @@ -1,17 +1,63 @@ import random +import hashlib +import math import argparse +def radians_from_degrees (deg): + return 0.01745329251994329577 * deg; + +def square(x): + return x**2 + +# copied almost directly out of the perf aware programming listing +def haversine(x0,x1,y0,y1, radius): + + lat1 = y0 + lat2 = y1 + lon1 = x0 + lon2 = x1 + + dLat = radians_from_degrees(lat2 - lat1); + dLon = radians_from_degrees(lon2 - lon1); + lat1 = radians_from_degrees(lat1); + lat2 = radians_from_degrees(lat2); + + a = square(math.sin(dLat/2.0)) + math.cos(lat1)*math.cos(lat2)*square(math.sin(dLon/2)); + c = 2.0*math.asin(math.sqrt(a)); + + res = radius * c; + return res; + + parser = argparse.ArgumentParser() parser.add_argument('filepath') parser.add_argument('num') args = parser.parse_args() +def rnd_y(): + return ((random.random() * (90*2)) - 90) + +def rnd_x(): + return ((random.random() * (180*2)) - 180) + +rndx = [] +rndy = [] +hav = [] + def genJson(): - x0 = random.random() * 100 - y0 = random.random() * 100 - x1 = random.random() * 100 - y1 = random.random() * 100 + x0 = rnd_x() + y0 = rnd_y() + x1 = rnd_x() + y1 = rnd_y() + + rndx.append(x0) + rndx.append(x1) + + rndy.append(y0) + rndy.append(y1) + st = f"{{\"x0\" : {x0}, \"y0\": {y0}, \"x1\" : {x1}, \"y1\": {y1}}}" + hav.append(haversine(x0,x1,y0,y1, 6372.8)) return st with open(args.filepath, 'w') as f: @@ -27,3 +73,17 @@ with open(args.filepath, 'w') as f: final = genJson() closing = "]}" f.writelines(lns) + +print("Avg x: " + str(sum(rndx) / len(rndx))) +print("Avg y: " + str(sum(rndy) / len(rndy))) +hvS = str(hav) + +with open('out/results.txt', 'w') as f: + f.writelines([hvS]) + + +h = hashlib.new('sha256') +h.update(hvS.encode()) +print(h.hexdigest()) +with open('out/sha.txt', 'w') as f: + f.writelines([str(h.hexdigest())])