summaryrefslogtreecommitdiffstats
path: root/net/ricketyspace/sicp/two/ten.scm
blob: 7553c07a220d08eb05d4c13e78cc568d3db6064a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
;;;; Under Creative Commons Attribution-ShareAlike 4.0
;;;; International. See
;;;; <https://creativecommons.org/licenses/by-sa/4.0/>.

(define-module (net ricketyspace sicp two ten)
  #:use-module (net ricketyspace sicp two seven)
  #:export (mul-interval div-interval))

(define (mul-interval x y)
  (let ((p1 (* (lower-bound x) (lower-bound y)))
        (p2 (* (lower-bound x) (upper-bound y)))
        (p3 (* (upper-bound x) (lower-bound y)))
        (p4 (* (upper-bound x) (upper-bound y))))
    (make-interval (min p1 p2 p3 p4)
                   (max p1 p2 p3 p4))))

(define (div-interval x y)
  (define (spans-zero y)
    (and (<= (lower-bound y) 0)
         (>= (upper-bound y) 0)))
  (cond ((spans-zero y) (error "Second argument cannot span zero!"))
        (else (mul-interval x
                            (make-interval (/ 1.0 (upper-bound y))
                                           (/ 1.0 (lower-bound y)))))))