-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-10.scm
45 lines (37 loc) · 1.18 KB
/
2-10.scm
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
#lang sicp
(define (make-interval a b) (cons a b))
(define (lower-bound i) (car i))
(define (upper-bound i) (cdr i))
(define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))
(define (sub-interval x y)
(make-interval (- (lower-bound x) (upper-bound y))
(- (upper-bound x) (lower-bound y))))
(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 (crosses-zero? i)
(let ((a (lower-bound i))
(b (upper-bound i)))
(if
(or
(= a 0)
(= b 0)
(and (< a 0) (> b 0))
(and (> a 0) (< b 0)))
#t #f)))
(define (div-interval x y)
(if (crosses-zero? y) (error "denominator includes zero"))
(mul-interval
x
(make-interval (/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y)))))
(define x (make-interval 1 2))
(define y (make-interval 0 1))
(div-interval x x)
(div-interval x y)