Skip to content

Commit 5e8ddd7

Browse files
authored
Merge branch 'main' into configure_nimalyzer
2 parents 7361b28 + 4a59ed8 commit 5e8ddd7

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

.github/workflows/build_directory_md.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ jobs:
1919
- uses: actions/checkout@v4
2020

2121
- uses: jiro4989/setup-nim-action@v2
22+
with:
23+
parent-nim-install-directory: ${{ runner.temp }}
2224

2325
- name: Build file
2426
run: |
25-
git clean -f -x -d
27+
git clean --force -x -d
2628
2729
# Compile the script first
2830
nim c -o:directory_script .scripts/directory.nim

maths/haversine_distance.nim

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Haversine formula
2+
3+
import std/math
4+
5+
func haversineDistance(latitudeA, longitudeA, latitudeB,
6+
longitudeB: float): float =
7+
## returns the length of the shortest path connecting the input points on an unit sphere.
8+
## The input points are represented by their spherical/geographical coordinates.
9+
## The inputs are expected to be in radians.
10+
let
11+
dLatitude = latitudeB - latitudeA
12+
dLongitude = longitudeB - longitudeA
13+
a = sin(dLatitude / 2.0)^2 + cos(latitudeA) * cos(latitudeB) * sin(
14+
dLongitude / 2.0)^2
15+
2.0 * arcsin(sqrt(a))
16+
17+
when isMainModule:
18+
import std/[unittest, sequtils, strformat]
19+
suite "haversineDistance":
20+
const testCases = [
21+
(0.0, 0.0, 0.0, 0.0, 0.0),
22+
(0.0, 0.0, PI / 2.0, 0.0, PI / 2.0),
23+
(-PI / 2.0, 0.0, PI / 2.0, 0.0, PI),
24+
(0.0, 0.0, 0.0, PI / 2.0, PI / 2.0),
25+
(0.0, -PI / 2.0, 0.0, PI / 2.0, PI),
26+
(1.0, -PI / 2.0, -1.0, PI / 2.0, PI),
27+
(2.0, -PI / 2.0, -2.0, PI / 2.0, PI),
28+
(3.0, -PI / 2.0, -3.0, PI / 2.0, PI),
29+
(3.0, -PI / 2.0 + 0.5, -3.0, PI / 2.0 + 0.5, PI),
30+
(0.0, 0.0, 0.0, PI, PI),
31+
(PI / 2.0, 1.0, PI / 2.0, 2.0, 0.0),
32+
(-PI / 2.0, 1.0, -PI / 2.0, 2.0, 0.0),
33+
(0.0, 0.0, -PI / 4.0, 0.0, PI / 4.0),
34+
(0.0, 1.0, PI / 4.0, 1.0, PI / 4.0),
35+
(-PI / 2.0, 0.0, -PI / 4.0, 0.0, PI / 4.0),
36+
(-PI / 2.0, 0.0, -PI / 4.0, 0.6, PI / 4.0),
37+
(-PI / 2.0, 3.0, -PI / 4.0, 0.2, PI / 4.0),
38+
].mapIt:
39+
(id: fmt"posA=({it[0]}, {it[1]}), posB=({it[2]}, {it[3]})",
40+
latitudeA: it[0], longitudeA: it[1],
41+
latitudeB: it[2], longitudeB: it[3],
42+
expected: it[4])
43+
44+
func isClose(a, b: float): bool =
45+
return abs(a-b) < 0.0000001
46+
47+
for tc in testCases:
48+
test tc.id:
49+
checkpoint("returns expected result")
50+
check isClose(haversineDistance(tc.latitudeA, tc.longitudeA,
51+
tc.latitudeB, tc.longitudeB), tc.expected)
52+
check isClose(haversineDistance(tc.latitudeB, tc.longitudeB,
53+
tc.latitudeA, tc.longitudeA), tc.expected)

0 commit comments

Comments
 (0)