2
2
3
3
module Units
4
4
5
- import Base. show, Base . convert
5
+ import Base: * , / , convert, show
6
6
7
7
export SIPrefix,
8
8
Quantity,
@@ -289,15 +289,16 @@ end
289
289
immutable Unknown <: UnitBase end
290
290
291
291
# Length units
292
- immutable Meter <: UnitBase end
293
- immutable Angstrom <: UnitBase end
294
- immutable Inch <: UnitBase end
295
- immutable Foot <: UnitBase end
296
- immutable Yard <: UnitBase end
297
- immutable Mile <: UnitBase end
298
- immutable LightYear <: UnitBase end
299
- immutable Parsec <: UnitBase end
300
- immutable AstronomicalUnit <: UnitBase end
292
+ abstract UnitLength <: UnitBase
293
+ immutable Meter <: UnitLength end
294
+ immutable Angstrom <: UnitLength end
295
+ immutable Inch <: UnitLength end
296
+ immutable Foot <: UnitLength end
297
+ immutable Yard <: UnitLength end
298
+ immutable Mile <: UnitLength end
299
+ immutable LightYear <: UnitLength end
300
+ immutable Parsec <: UnitLength end
301
+ immutable AstronomicalUnit <: UnitLength end
301
302
let
302
303
# Unit RefUnit ToRef show pshow lshow fshow
303
304
const utable = {
@@ -317,13 +318,14 @@ _unit_gen_dict(utable[2:end])
317
318
end
318
319
319
320
# Time units
320
- immutable Second <: UnitBase end
321
- immutable Minute <: UnitBase end
322
- immutable Hour <: UnitBase end
323
- immutable Day <: UnitBase end
324
- immutable Week <: UnitBase end
325
- immutable YearJulian <: UnitBase end
326
- immutable PlanckTime <: UnitBase end
321
+ abstract UnitTime <: UnitBase
322
+ immutable Second <: UnitTime end
323
+ immutable Minute <: UnitTime end
324
+ immutable Hour <: UnitTime end
325
+ immutable Day <: UnitTime end
326
+ immutable Week <: UnitTime end
327
+ immutable YearJulian <: UnitTime end
328
+ immutable PlanckTime <: UnitTime end
327
329
let
328
330
# Unit RefUnit ToRef show pshow lshow fshow
329
331
const utable = {
@@ -352,10 +354,11 @@ end
352
354
353
355
# Mass units
354
356
# (note English units like pounds are technically weight, not mass)
355
- immutable Gram <: UnitBase end
356
- immutable AtomicMassUnit <: UnitBase end
357
- immutable Dalton <: UnitBase end
358
- immutable PlanckMass <: UnitBase end
357
+ abstract UnitMass <: UnitBase
358
+ immutable Gram <: UnitMass end
359
+ immutable AtomicMassUnit <: UnitMass end
360
+ immutable Dalton <: UnitMass end
361
+ immutable PlanckMass <: UnitMass end
359
362
let
360
363
# Unit RefUnit ToRef show pshow lshow fshow
361
364
const utable = {
@@ -454,4 +457,24 @@ function convert{Uin<:UnitBase, Uout<:UnitBase, Pin<:SIPrefix, Pout<:SIPrefix, T
454
457
return Quantity (Pout, Uout, from_reference (Uout)(to_reference (Uin)(q. value* to_reference (Pin))) / to_reference (Pout))
455
458
end
456
459
460
+
461
+ # Arithmetic with Quantities
462
+ (* ){TP,TU}(n:: Number , q:: Quantity{TP,TU} ) = Quantity (TP, TU, n* q. value)
463
+ (* ){TP,TU}(q:: Quantity{TP,TU} , n:: Number ) = Quantity (TP, TU, n* q. value)
464
+ (/ ){TP,TU}(q:: Quantity{TP,TU} , n:: Number ) = Quantity (TP, TU, q. value/ n)
465
+
466
+ (/ ){TP,TU<: UnitLength }(q1:: Quantity{TP,TU} , q2:: Quantity{TP,TU} ) = q1. value/ q2. value
467
+ (/ ){TP1,TP2,TU<: UnitLength }(q1:: Quantity{TP1,TU} , q2:: Quantity{TP2,TU} ) = q1. value/ q2. value* to_reference (TP1)/ to_reference (TP2)
468
+ (/ ){TP1,TP2,TU1<: UnitLength ,TU2<: UnitLength }(q1:: Quantity{TP1,TU1} , q2:: Quantity{TP2,TU2} ) = from_reference (TU2)(to_reference (TU1)(q1. value/ q2. value* to_reference (TP1)/ to_reference (TP2)))
469
+ (/ ){TP,TU<: UnitTime }(q1:: Quantity{TP,TU} , q2:: Quantity{TP,TU} ) = q1. value/ q2. value
470
+ (/ ){TP1,TP2,TU<: UnitTime }(q1:: Quantity{TP1,TU} , q2:: Quantity{TP2,TU} ) = q1. value/ q2. value* to_reference (TP1)/ to_reference (TP2)
471
+ (/ ){TP1,TP2,TU1<: UnitTime ,TU2<: UnitTime }(q1:: Quantity{TP1,TU1} , q2:: Quantity{TP2,TU2} ) = from_reference (TU2)(to_reference (TU1)(q1. value/ q2. value* to_reference (TP1)/ to_reference (TP2)))
472
+ (/ ){TP,TU<: UnitMass }(q1:: Quantity{TP,TU} , q2:: Quantity{TP,TU} ) = q1. value/ q2. value
473
+ (/ ){TP1,TP2,TU<: UnitMass }(q1:: Quantity{TP1,TU} , q2:: Quantity{TP2,TU} ) = q1. value/ q2. value* to_reference (TP1)/ to_reference (TP2)
474
+ (/ ){TP1,TP2,TU1<: UnitMass ,TU2<: UnitMass }(q1:: Quantity{TP1,TU1} , q2:: Quantity{TP2,TU2} ) = from_reference (TU2)(to_reference (TU1)(q1. value/ q2. value* to_reference (TP1)/ to_reference (TP2)))
475
+
476
+ (/ ){TP,TU}(q1:: Quantity{TP,TU} , q2:: Quantity{TP,TU} ) = q1. value/ q2. value
477
+ (/ ){TP1,TP2,TU}(q1:: Quantity{TP1,TU} , q2:: Quantity{TP2,TU} ) = q1. value/ q2. value* to_reference (TP1)/ to_reference (TP2)
478
+
479
+
457
480
end
0 commit comments