From e40d222c1be0693f44d3f15372a46598ee39c31b Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 25 Aug 2018 15:03:05 +0000 Subject: Add (net ricketyspace sicp two twelve). * net/ricketyspace/sicp/two/twelve.scm: New file. --- net/ricketyspace/sicp/two/twelve.scm | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 net/ricketyspace/sicp/two/twelve.scm (limited to 'net') diff --git a/net/ricketyspace/sicp/two/twelve.scm b/net/ricketyspace/sicp/two/twelve.scm new file mode 100644 index 0000000..c15614b --- /dev/null +++ b/net/ricketyspace/sicp/two/twelve.scm @@ -0,0 +1,40 @@ +;;;; Under Creative Commons Attribution-ShareAlike 4.0 +;;;; International. See +;;;; . + +(define-module (net ricketyspace sicp two twelve) + #:use-module (net ricketyspace sicp two seven) + #:export (make-center-width + make-center-percent + center + width + percent)) + +(define (make-center-width c w) + (make-interval (- c w) (+ c w))) + +(define (make-center-percent c p) + (define (pof p c) (* c (/ p 100.0))) + (make-interval (- c (pof p c)) + (+ c (pof p c)))) + +(define (center i) + (/ (+ (lower-bound i) (upper-bound i)) 2)) +(define (width i) + (/ (- (upper-bound i) (lower-bound i)) 2)) + +(define (center i) + (/ (+ (lower-bound i) (upper-bound i)) 2)) + +(define (width i) + (/ (- (upper-bound i) (lower-bound i)) 2)) + +(define (percent i) + (* (/ (width i) (center i)) 100)) + +;;; Guile REPL +;;; +;;; scheme@(guile-user)> (percent (make-center-percent 45 15)) +;;; $16 = 15.0 +;;; scheme@(guile-user)> (percent (make-center-percent 40 16)) +;;; $17 = 15.999999999999998 -- cgit v1.2.3