4.2 Machine Epsilon
Find the floating point number epsi that has the the following properties:
- 1.0+epsi is greater than 1.0 and
- Let m b e any number less than epsi. Then 1.0+m is equal to 1.0.
epsi is called machine epsilon. It is of great importance in understanding floating point numbers.
I have written the following program to try and find my machine epsilon value to a given search depth (10). Do you have any ideas how I could write this program better?
(defpackage :find-epsi (:use cl))
(in-package :find-epsi)
(defun smaller-scale (&OPTIONAL (epsi 1.0)) (if (> (+ 1.0 epsi) 1.0) (smaller-scale (/ epsi 10)) epsi))
(defun bigger (epsi inc-unit) (if (< (+ 1.0 epsi) 1.0) (bigger (+ epsi inc-unit) inc-unit) epsi))
(defun smaller (epsi dec-unit) (if (> (+ 1.0 epsi) 1.0) (smaller (+ epsi dec-unit) dec-unit) epsi))
(defun find-epsi (&OPTIONAL (search-depth 10) (epsi (smaller-scale)) (incdec-unit epsi))
(if (= search-depth 0) epsi (find-epsi (1- search-depth) (bigger (smaller epsi incdec-unit) incdec-unit) incdec-unit)))
(format t "epsi: ~a ~%" (find-epsi))
It seems that it should be much simpler to find epsilon than I originally thought. What do you think about the following program?
(defpackage :find-epsi (:use cl))
(in-package :find-epsi)
(defun find-epsi (&OPTIONAL (epsi 1.0))
(if (> (+ 1.0 epsi) 1.0) ; if the variable epsi is still significant
(find-epsi (/ epsi 2)) ; halve it and try again
epsi)) ; otherwise, we have found epsilon
(format t "epsi: ~a ~%" (find-epsi))