From 5e85a662d21081bc27adc1142759659cca59cd62 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Wed, 27 Sep 2023 09:38:55 -0600 Subject: [PATCH] fix compute-maceps arguments --- cl/src/approx/maceps.lisp | 14 +++--- cl/tests/maceps.lisp | 10 +++-- homeworks/hw-2.org | 93 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 101 insertions(+), 16 deletions(-) diff --git a/cl/src/approx/maceps.lisp b/cl/src/approx/maceps.lisp index cd24482..ad234e8 100644 --- a/cl/src/approx/maceps.lisp +++ b/cl/src/approx/maceps.lisp @@ -1,13 +1,11 @@ (in-package :lizfcm.approx) -(defun compute-maceps (val f) - (let* ((h val) - (a val) - (err val)) +(defun compute-maceps (f a init) + (let ((h init) + (err init)) (loop while (> err 0) do - (progn - (setf h (/ h 2) - err (abs (- (funcall f (+ a h)) - (funcall f a))))) + (setf h (/ h 2) + err (abs (- (funcall f (+ a h)) + (funcall f a)))) collect (list a h err)))) diff --git a/cl/tests/maceps.lisp b/cl/tests/maceps.lisp index 9d61772..cd5ced9 100644 --- a/cl/tests/maceps.lisp +++ b/cl/tests/maceps.lisp @@ -13,10 +13,12 @@ (test maceps :description "double precision provides precision about (mac eps of single precision) squared" - (let* ((maceps-computation-double (compute-maceps 1.0d0 - (lambda (x) x))) - (maceps-computation-single (compute-maceps 1.0 - (lambda (x) x))) + (let* ((maceps-computation-double (compute-maceps (lambda (x) x) + 1.0d0 + 1.0d0)) + (maceps-computation-single (compute-maceps (lambda (x) x) + 1.0 + 1.0)) (last-double-h (cadar (last maceps-computation-double))) (last-single-h (cadar (last maceps-computation-single)))) (is (within-range-p diff --git a/homeworks/hw-2.org b/homeworks/hw-2.org index c570b65..90807b6 100644 --- a/homeworks/hw-2.org +++ b/homeworks/hw-2.org @@ -12,13 +12,41 @@ Computing $\epsilon_{\text{mac}}$ for single precision numbers (load "../cl/lizfcm.asd") (ql:quickload :lizfcm) - (let ((domain-values (lizfcm.approx:compute-maceps 1.0 - (lambda (x) x)))) + (let ((domain-values (lizfcm.approx:compute-maceps (lambda (x) x) + 1.0 + 1.0))) (lizfcm.utils:table (:headers '("a" "h" "err") :domain-order (a h err) :domain-values domain-values))) #+END_SRC +#+RESULTS: +| a | h | err | +| 1.0 | 0.5 | 0.5 | +| 1.0 | 0.25 | 0.25 | +| 1.0 | 0.125 | 0.125 | +| 1.0 | 0.0625 | 0.0625 | +| 1.0 | 0.03125 | 0.03125 | +| 1.0 | 0.015625 | 0.015625 | +| 1.0 | 0.0078125 | 0.0078125 | +| 1.0 | 0.00390625 | 0.00390625 | +| 1.0 | 0.001953125 | 0.001953125 | +| 1.0 | 0.0009765625 | 0.0009765625 | +| 1.0 | 0.00048828125 | 0.00048828125 | +| 1.0 | 0.00024414063 | 0.00024414063 | +| 1.0 | 0.00012207031 | 0.00012207031 | +| 1.0 | 6.1035156e-05 | 6.1035156e-05 | +| 1.0 | 3.0517578e-05 | 3.0517578e-05 | +| 1.0 | 1.5258789e-05 | 1.5258789e-05 | +| 1.0 | 7.6293945e-06 | 7.6293945e-06 | +| 1.0 | 3.8146973e-06 | 3.8146973e-06 | +| 1.0 | 1.9073486e-06 | 1.9073486e-06 | +| 1.0 | 9.536743e-07 | 9.536743e-07 | +| 1.0 | 4.7683716e-07 | 4.7683716e-07 | +| 1.0 | 2.3841858e-07 | 2.3841858e-07 | +| 1.0 | 1.1920929e-07 | 1.1920929e-07 | +| 1.0 | 5.9604645e-08 | 0.0 | + (with many rows truncated) | a | h | err | @@ -39,13 +67,70 @@ $\epsilon_{\text{mac}}$ \approx 5.9604 \cdot 10^{-8} Computing $\epsilon_{\text{mac}}$ for double precision numbers: #+BEGIN_SRC lisp :session t :results table - (let ((domain-values (lizfcm.approx:compute-maceps 1.0d0 - (lambda (x) x)))) + (let ((domain-values (lizfcm.approx:compute-maceps (lambda (x) x) + 1.0d0 + 1.0d0))) (lizfcm.utils:table (:headers '("a" "h" "err") :domain-order (a h err) :domain-values domain-values))) #+END_SRC +#+RESULTS: +| a | h | err | +| 1.0d0 | 0.5d0 | 0.5d0 | +| 1.0d0 | 0.25d0 | 0.25d0 | +| 1.0d0 | 0.125d0 | 0.125d0 | +| 1.0d0 | 0.0625d0 | 0.0625d0 | +| 1.0d0 | 0.03125d0 | 0.03125d0 | +| 1.0d0 | 0.015625d0 | 0.015625d0 | +| 1.0d0 | 0.0078125d0 | 0.0078125d0 | +| 1.0d0 | 0.00390625d0 | 0.00390625d0 | +| 1.0d0 | 0.001953125d0 | 0.001953125d0 | +| 1.0d0 | 9.765625d-4 | 9.765625d-4 | +| 1.0d0 | 4.8828125d-4 | 4.8828125d-4 | +| 1.0d0 | 2.44140625d-4 | 2.44140625d-4 | +| 1.0d0 | 1.220703125d-4 | 1.220703125d-4 | +| 1.0d0 | 6.103515625d-5 | 6.103515625d-5 | +| 1.0d0 | 3.0517578125d-5 | 3.0517578125d-5 | +| 1.0d0 | 1.52587890625d-5 | 1.52587890625d-5 | +| 1.0d0 | 7.62939453125d-6 | 7.62939453125d-6 | +| 1.0d0 | 3.814697265625d-6 | 3.814697265625d-6 | +| 1.0d0 | 1.9073486328125d-6 | 1.9073486328125d-6 | +| 1.0d0 | 9.5367431640625d-7 | 9.5367431640625d-7 | +| 1.0d0 | 4.76837158203125d-7 | 4.76837158203125d-7 | +| 1.0d0 | 2.384185791015625d-7 | 2.384185791015625d-7 | +| 1.0d0 | 1.1920928955078125d-7 | 1.1920928955078125d-7 | +| 1.0d0 | 5.960464477539063d-8 | 5.960464477539063d-8 | +| 1.0d0 | 2.9802322387695313d-8 | 2.9802322387695313d-8 | +| 1.0d0 | 1.4901161193847656d-8 | 1.4901161193847656d-8 | +| 1.0d0 | 7.450580596923828d-9 | 7.450580596923828d-9 | +| 1.0d0 | 3.725290298461914d-9 | 3.725290298461914d-9 | +| 1.0d0 | 1.862645149230957d-9 | 1.862645149230957d-9 | +| 1.0d0 | 9.313225746154785d-10 | 9.313225746154785d-10 | +| 1.0d0 | 4.656612873077393d-10 | 4.656612873077393d-10 | +| 1.0d0 | 2.3283064365386963d-10 | 2.3283064365386963d-10 | +| 1.0d0 | 1.1641532182693481d-10 | 1.1641532182693481d-10 | +| 1.0d0 | 5.820766091346741d-11 | 5.820766091346741d-11 | +| 1.0d0 | 2.9103830456733704d-11 | 2.9103830456733704d-11 | +| 1.0d0 | 1.4551915228366852d-11 | 1.4551915228366852d-11 | +| 1.0d0 | 7.275957614183426d-12 | 7.275957614183426d-12 | +| 1.0d0 | 3.637978807091713d-12 | 3.637978807091713d-12 | +| 1.0d0 | 1.8189894035458565d-12 | 1.8189894035458565d-12 | +| 1.0d0 | 9.094947017729282d-13 | 9.094947017729282d-13 | +| 1.0d0 | 4.547473508864641d-13 | 4.547473508864641d-13 | +| 1.0d0 | 2.2737367544323206d-13 | 2.2737367544323206d-13 | +| 1.0d0 | 1.1368683772161603d-13 | 1.1368683772161603d-13 | +| 1.0d0 | 5.684341886080802d-14 | 5.684341886080802d-14 | +| 1.0d0 | 2.842170943040401d-14 | 2.842170943040401d-14 | +| 1.0d0 | 1.4210854715202004d-14 | 1.4210854715202004d-14 | +| 1.0d0 | 7.105427357601002d-15 | 7.105427357601002d-15 | +| 1.0d0 | 3.552713678800501d-15 | 3.552713678800501d-15 | +| 1.0d0 | 1.7763568394002505d-15 | 1.7763568394002505d-15 | +| 1.0d0 | 8.881784197001252d-16 | 8.881784197001252d-16 | +| 1.0d0 | 4.440892098500626d-16 | 4.440892098500626d-16 | +| 1.0d0 | 2.220446049250313d-16 | 2.220446049250313d-16 | +| 1.0d0 | 1.1102230246251565d-16 | 0.0d0 | + (with many rows truncated) | a | h | err | | 1.0d0 | 0.5d0 | 0.5d0 |