-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.13.rkt
43 lines (30 loc) · 1.1 KB
/
2.13.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
31
32
33
34
35
36
37
38
39
40
41
42
43
#lang sicp
(define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))
(define (make-interval a b) (cons a b))
(define (upper-bound p)
(cdr p))
(define (lower-bound p)
(car p))
(define (make-center-width c w)
(make-interval (- c w) (+ c w)))
(define (center i)
(/ (+ (lower-bound i) (upper-bound i)) 2))
(define (width i)
(/ (- (lower-bound i) (upper-bound i)) 2))
(define (make-center-percent c p)
(make-interval (- c (* c (/ p 100.0))) (+ c (* c (/ p 100.0)))))
(define (percent i)
(* (/ (- (upper-bound i) (center i)) (center i)) 100.0))
(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 i1 (make-center-percent 100000 0.001))
(define i2 (make-center-percent 10 0.002))
(define test (mul-interval i1 i2))
(percent test) ; tolerance1 + tolerance 2