開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Scheme (プログラミング言語)
- Gauche (処理系)
計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の2(データによる抽象の構築)、2.1(データ抽象入門)、2.1.4(拡張問題: 区間算術演算)、問題 2.14.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 2.14.
コード(BBEdit, Emacs)
sample.scm
#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-
;; これまでに書いた手続き
(load "./procedures.scm")
;; 構成子
;; 区間の中央値とパーセント相対許容誤差
(define (make-center-percent c p)
((lambda (x)
(make-interval (- c x)
(+ c x)))
(* (abs c)
(/ p 100))))
;; 選択子
;; パーセント相対許容誤差
(define (percent i)
((lambda (c)
(abs (* 100
(/ (- (upper-bound i)
c)
c))))
(center i)))
(define (par1 r1 r2)
(div-interval (mul-interval r1 r2)
(add-interval r1 r2)))
(define (par2 r1 r2)
(let ((one (make-interval 1 1)))
(div-interval one
(mul-interval (div-interval one r1)
(div-interval one r2)))))
(define a (make-center-percent 10 0.00001))
(define b (make-center-percent 100 0.0001))
(define aa (div-interval a a))
(define ab (div-interval a b))
(for-each (lambda (x)
(print "中央値: " (center x))
(print "パーセント相対許容誤差: " (percent x)))
(list (par1 a b)
(par2 a b)
(par1 aa ab)
(par2 aa ab)
aa
ab
(add-interval aa ab)
(sub-interval aa ab)
(mul-interval aa ab)
(div-interval aa ab)))
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
$ ./sample.scm 中央値: 9.090909090926846 パーセント相対許容誤差: 2.0181818182468108e-4 中央値: 1000.0000000000999 パーセント相対許容誤差: 1.0999999999511411e-4 中央値: 0.09090909090924251 パーセント相対許容誤差: 1.5818181813399823e-4 中央値: 0.100000000000134 パーセント相対許容誤差: 1.299999999869107e-4 中央値: 1.00000000000002 パーセント相対許容誤差: 1.9999999989472483e-5 中央値: 0.10000000000011 パーセント相対許容誤差: 1.0999999999749101e-4 中央値: 1.1000000000001302 パーセント相対許容誤差: 2.8181818151841777e-5 中央値: 0.89999999999991 パーセント相対許容誤差: 3.4444444432485765e-5 中央値: 0.10000000000013401 パーセント相対許容誤差: 1.299999999730329e-4 中央値: 10.0000000000035 パーセント相対許容誤差: 1.299999999914803e-4 $
par1とpar2の計算法で異なる結果になることが確認できたので、Lenのいうことは正しい。
0 コメント:
コメントを投稿