;; Unicalc test cases (load "a02.scm") ;; Function close-enough is meant to test whether two inexact quantites ;; have a relative difference that is within the specified tolerance. ;; This is used in cases where answers might not match exactly. (define tolerance 1e-6) (define (relative-error x y) (cond ((= x y) 0) ((not (= x 0)) (/ (abs (- x y)) x)) (else (/ (abs (- x y) y))))) (define (close-enough Quantity1 Quantity2) (and (equal? (rest Quantity1) (rest Quantity2)) (< (relative-error (first Quantity1) (first Quantity2) ) tolerance))) (test (normalize-unit 'second) '(1 (second)())) (test (close-enough (normalize-unit 'gram) '(0.001 (kg) ())) #t) (test (normalize-unit 'kilometer) '(1000 (meter) ())) (test (normalize-unit 'day) '(86400 (second) ())) (test (normalize-unit 'newton) '(1 (kg meter) (second second))) (test (close-enough (normalize '(1 (mile)(hour))) '(0.447031923888 (meter) (second))) #t) (test (close-enough (normalize '(1e6 (pound)(mile mile))) '(0.1751393211439157 (kg) (meter meter))) #t) (test (normalize '(1 (newton meter)(second))) '(1 (kg meter meter) (second second second))) (test (normalize '(1 (volt)())) '(1 (kg meter meter) (ampere second second second))) (test (close-enough (multiply (normalize '(1 (foot) ())) (normalize '(1 (acre) ()))) '(1233.414987438996 (meter meter meter) ())) #t) (test (multiply '(1 () ()) '(1 (kg meter)(second))) '(1 (kg meter)(second))) (test (multiply '(1 (chicken) (meter meter)) '(100 (meter meter) ())) '(100 (chicken) ())) (test (close-enough (multiply (normalize '(100 (tadpole) (gallon))) '(1 (meter meter meter) ())) '(26418.63702726775 (tadpole) ())) #t) (test (close-enough (divide (normalize '(1 (yard)())) (normalize '(1 (foot)()))) '(3.0 ()())) #t) (test (close-enough (divide (normalize '(1 (mile)())) (normalize '(1 (foot)()))) '(5280.0 () ())) #t) (test (close-enough (divide (normalize '(1 (torr) ())) (normalize '(1 (atmosphere)()))) '(0.0013157894736842105 () ())) #t) (tester 'show)