-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-10.rkt
30 lines (22 loc) · 927 Bytes
/
2-10.rkt
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
#lang racket
(define (make-interval a b) (cons a b))
(define (lower-bound interval) (car interval))
(define (upper-bound interval) (cdr 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 (spans-zero-interval? int)
(and (>= (upper-bound int) 0)
(<= (lower-bound int) 0)))
(define (div-interval x y)
(if (spans-zero-interval? y)
(error "Can't divide by interval that spans zero")
(mul-interval x
(make-interval (/ 1.0 (upper-bound y))
(/ 1.0 (lower-bound y))))))
(div-interval (make-interval 1 3) (make-interval 1 2))
(div-interval (make-interval 1 3) (make-interval -2 2))