summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--net/ricketyspace/sicp/two/ten.scm24
1 files changed, 24 insertions, 0 deletions
diff --git a/net/ricketyspace/sicp/two/ten.scm b/net/ricketyspace/sicp/two/ten.scm
new file mode 100644
index 0000000..d8e92df
--- /dev/null
+++ b/net/ricketyspace/sicp/two/ten.scm
@@ -0,0 +1,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 (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)))))))