;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Units demo session ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (in-package :units) ;; ;; Creating quantities with base units ;; (kilogram meter second ampere mole kelvin candela) ;; UNITS 1 > (quantity 10 '(m (sec -1))) 10 m sec-1 UNITS 2 > (quantity 34 'kg) 34 kg ;; ;; Creating quantities with derived units ;; (e.g. hour, kilometer, pascal, newton, joule, ;; watt, atmosphere) ;; UNITS 3 > (quantity 2 'hour) 7200 sec UNITS 4 > (quantity 90 '(km (hour -1))) 25 m sec-1 UNITS 5 > (quantity 1.05 'atm) 106364.99999999985 kg m-1 sec-2 ;; ;; Defining new derived units ;; UNITS 6 > (define-unit inch 2.54 cm) INCH ;; ;; Now you can use INCH like any other unit ;; UNITS 7 > (quantity 12 'inch) 0.30480000000000007 m UNITS 8 > (quantity 10 '(gram (inch -2))) 15.500031000061995 kg m-2 ;; ;; Multiplying quantities ;; UNITS 9 > (* (quantity 10 '(m (sec -1))) (quantity 15 'sec)) 150 m UNITS 10 > (* (quantity 10 '(m (sec -1))) (quantity 2 'hour)) 72000 m ;; ;; You can't compare or add quantities if they don't have the ;; same base units. ;; UNITS 11 > (+ (quantity 10 '(m (sec -1))) (quantity 2 'hour)) Error: Unit mismatch between 10 m sec-1 and 7200 sec. 1 (abort) Return to level 0. 2 Return to top loop level 0. ;; ;; But you can add quantities with different derived units ;; that reduce to the same base units. ;; UNITS 12 > (+ (quantity 2 'hour) (quantity 5 'minute)) 7500 sec UNITS 13 > (let ((radius (quantity 5 'cm))) ;; Compute area of a circle with 5 cm radius. (* pi (expt radius 2))) 0.007853981633974483 m2 ;; ;; You can explicity check for correct units. ;; UNITS 14 > (check-units (quantity 10 'newton) 'newton) 10 kg m sec-2 UNITS 15 > (check-units (quantity 10 'newton) 'pascal) Error: The quantity 10 kg m sec-2 should have units PASCAL. 1 (abort) Return to level 0. 2 Return to top loop level 0. ;; ;; Defining physics constants and formulas. ;; UNITS 16 > (defconstant +gas-constant+ (quantity 8.315 '(joule (mole -1) (kelvin -1)))) +GAS-CONSTANT+ UNITS 17 > (defun gas-pressure (gas-density temperature molecular-weight) (check-units gas-density '(kg (m -3))) (check-units temperature 'kelvin) (check-units molecular-weight '(kg (mole -1))) (check-units (/ (* gas-density +gas-constant+ temperature) molecular-weight) 'pascal)) GAS-PRESSURE ;; ;; Using the gas-pressure formula. ;; UNITS 18 > (gas-pressure (quantity 2 '(kg (meter -3))) (quantity 295 'kelvin) (quantity 32 '(gram (mole -1)))) 153307.81249999997 kg m-1 sec-2 ;; ;; With wrong units, the same function results in a unit ;; mismatch error. ;; UNITS 19 > (gas-pressure (quantity 2 '(kg (meter -1))) (quantity 295 'kelvin) (quantity 32 '(gram (mole -1)))) Error: The quantity 2 kg m-1 should have units (KG (M -3)). 1 (abort) Return to level 0. 2 Return to top loop level 0.