forked from dbmcclain/vmath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunits.lisp
104 lines (86 loc) · 2.28 KB
/
units.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
(defun defunits-chaining (u units prev)
(if (member u prev)
(error "~{ ~a~^ depends on~}"
(cons u prev)))
(let ((spec (find u units :key #'car)))
(if (null spec)
(error "Unknown unit ~a" u)
(let ((chain (cadr spec)))
(if (listp chain)
(* (car chain)
(defunits-chaining
(cadr chain)
units
(cons u prev)))
chain)))))
(um:defmacro! defunits (quantity base-unit &rest units)
`(defmacro ,(um:symb 'unit-of- quantity)
(,g!val ,g!un)
`(* ,,g!val
,(case ,g!un
((,base-unit) 1)
,@(mapcar (lambda (x)
`((,(car x))
,(defunits-chaining
(car x)
(cons
`(,base-unit 1)
(um:group units 2))
nil)))
(um:group units 2))))))
;; -------------------------------------------
;; units of distance
(defunits length m
km 1000
cm 1/100
mm (1/10 cm)
um (1/1000 mm)
nm (1/1000 um)
angstrom (1/10 nm)
yard 9144/10000 ; Defined in 1956
foot (1/3 yard)
inch (1/12 foot)
mile (1760 yard)
furlong (1/8 mile)
fathom (2 yard) ; Defined in 1929
nautical-mile 1852
cable (1/10 nautical-mile)
old-brit-nautical-mile ; Dropped in 1970
(6080/3 yard)
old-brit-cable
(1/10 old-brit-nautical-mile)
old-brit-fathom
(1/100 old-brit-cable)
r-earth (6371 km)
r-sun 6.955d8
au (149.6d6 km)
ly (9.461d12 km)
pc (3.26156 ly)
)
(defunits mass kg
g 1/1000
m-earth (5.9736d24 kg)
m-sun (1.9891d30 kg)
)
(defunits time s
ms (1/1000 s)
us (1/1000 ms)
ns (1/1000 us)
ps (1/1000 ns)
m (60 s)
h (60 m)
d (24 h)
sid-day (86164.090530833 s) ;; J2000(?)
)
(defmacro [L] (val unit)
`(unit-of-length ,val ,unit))
(defmacro [M] (val unit)
`(unit-of-mass ,val ,unit))
(defmacro [T] (val unit)
`(unit-of-time ,val ,unit))
(defunits frequency hz
khz 1000
mhz (10 khz)
ghz (1000 mhz)
thz (1000 ghz)
)