summaryrefslogtreecommitdiff
path: root/coding-exercises/2/11.rkt
diff options
context:
space:
mode:
authorMike Vink <mike1994vink@gmail.com>2023-03-07 15:18:30 +0100
committerMike Vink <mike1994vink@gmail.com>2023-03-07 15:18:30 +0100
commitbd5f50be83a10363fdfd4f73a377325cf48b5903 (patch)
tree050f562d4b815cc4a8467facb935c0cba98b8bea /coding-exercises/2/11.rkt
parent592ef89cb282ab33d6b10cacae711a4a8e6b1212 (diff)
fixup
Diffstat (limited to 'coding-exercises/2/11.rkt')
-rw-r--r--coding-exercises/2/11.rkt90
1 files changed, 90 insertions, 0 deletions
diff --git a/coding-exercises/2/11.rkt b/coding-exercises/2/11.rkt
new file mode 100644
index 0000000..d81d33a
--- /dev/null
+++ b/coding-exercises/2/11.rkt
@@ -0,0 +1,90 @@
+#lang racket
+(require "../../shared/intervals.rkt")
+
+;; Could be written to use less comparisons with nested cond, but this is more readable.
+(define (pos? x)
+ (> x 0))
+(define (neg? x)
+ (< x 0))
+(define (mul-interval x y)
+ (cond
+ ;; 1. all positive
+ ((and
+ (pos? (lower-bound x))
+ (pos? (lower-bound y))
+ (pos? (upper-bound x))
+ (pos? (upper-bound y)))
+ (make-interval (* (lower-bound x) (lower-bound y))
+ (* (upper-bound x) (upper-bound y))))
+ ;; 2. one lower-bound neg
+ ((and
+ (neg? (lower-bound x))
+ (pos? (lower-bound y))
+ (pos? (upper-bound x))
+ (pos? (upper-bound y)))
+ (make-interval (* (lower-bound x) (upper-bound y))
+ (* (upper-bound x) (upper-bound y))))
+ ;; 3. one lower-bound neg
+ ((and
+ (pos? (lower-bound x))
+ (neg? (lower-bound y))
+ (pos? (upper-bound x))
+ (pos? (upper-bound y)))
+ (make-interval (* (upper-bound x) (lower-bound y))
+ (* (upper-bound x) (upper-bound y))))
+ ;; 4. one interval neg
+ ((and
+ (neg? (lower-bound x))
+ (pos? (lower-bound y))
+ (neg? (upper-bound x))
+ (pos? (upper-bound y)))
+ (make-interval (* (upper-bound x) (upper-bound y))
+ (* (lower-bound x) (lower-bound y))))
+ ;; 5. one interval neg
+ ((and
+ (pos? (lower-bound x))
+ (neg? (lower-bound y))
+ (pos? (upper-bound x))
+ (neg? (upper-bound y)))
+ (make-interval (* (upper-bound x) (upper-bound y))
+ (* (lower-bound x) (lower-bound y))))
+ ;; 6. one interval neg, one interval crossing zero
+ ((and
+ (neg? (lower-bound x))
+ (neg? (lower-bound y))
+ (neg? (upper-bound x))
+ (pos? (upper-bound y)))
+ (make-interval (* (upper-bound x) (upper-bound y))
+ (* (upper-bound x) (lower-bound y))))
+ ;; 7. one interval neg, one interval crossing zero
+ ((and
+ (neg? (lower-bound x))
+ (neg? (lower-bound y))
+ (pos? (upper-bound x))
+ (neg? (upper-bound y)))
+ (make-interval (* (upper-bound x) (upper-bound y))
+ (* (upper-bound x) (lower-bound y))))
+ ;; 8. all neg
+ ((and
+ (neg? (lower-bound x))
+ (neg? (lower-bound y))
+ (neg? (upper-bound x))
+ (neg? (upper-bound y)))
+ (make-interval (* (lower-bound x) (lower-bound y))
+ (* (upper-bound x) (upper-bound y))))
+ ;; 9. both crossing zero
+ ((and
+ (neg? (lower-bound x))
+ (neg? (lower-bound y))
+ (pos? (upper-bound x))
+ (pos? (upper-bound y)))
+ (make-interval ((lambda (a b) (if (< a b) a b))
+ (* (lower-bound x) (upper-bound y))
+ (* (upper-bound x) (lower-bound y)))
+ ((lambda (a b) (if (> a b) a b))
+ (* (upper-bound x) (upper-bound y))
+ (* (lower-bound x) (lower-bound y)))))))
+
+(mul-interval
+ (make-interval -3 3)
+ (make-interval -4 4))