summaryrefslogtreecommitdiffstats
path: root/net/ricketyspace
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2018-08-25 15:03:05 +0000
committerrsiddharth <s@ricketyspace.net>2018-08-25 15:03:05 +0000
commite40d222c1be0693f44d3f15372a46598ee39c31b (patch)
tree73c02102d99a1e54bb0962b3033190dd2c2ab7d9 /net/ricketyspace
parent0bfe90e1fd4182d489a4933702a4ccaef3fd6eeb (diff)
Add (net ricketyspace sicp two twelve).
* net/ricketyspace/sicp/two/twelve.scm: New file.
Diffstat (limited to 'net/ricketyspace')
-rw-r--r--net/ricketyspace/sicp/two/twelve.scm40
1 files changed, 40 insertions, 0 deletions
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
+;;;; <https://creativecommons.org/licenses/by-sa/4.0/>.
+
+(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