How to find the minimum positive number added in 1.0 gives something more?

When translating some Fortran to Scheme / Racket, I came across Function:

; EPSILON(X) The least positive number that added ; to 1 returns a number that is greater than 1 

How to find the number in the diagram?

+6
source share
3 answers
 #lang racket/base ;; http://en.wikipedia.org/wiki/Machine_epsilon ;; approximates the machine epsilon (require racket/flonum) (define (compute-machine-epsilon) (let loop ([n 1.0]) (define next-n (fl/ n 2.0)) (if (fl= 1.0 (fl+ 1.0 next-n)) n (loop next-n)))) 
+7
source

Assuming you are using an IEEE-754 floating point (which may be wrong in the diagram, I don’t know), then the epsilon machine is well known: for double-precision arithmetic it is 1.11e-16 .

For other platforms or floating point implementations, Wikipedia shows a formula to calculate it as (in Haskell):

 main = print . last . map (subtract 1) . takeWhile (/= 1) . map (+ 1) . iterate (/2) $ 1 
+5
source

This is not a new answer - it just bothers me that the Danny code makes me wonder how hard it is to do it ... it could be simplified to

 (let loop ([n 1.0]) (if (= 1 (+ 1 (/ n 2))) n (loop (/ n 2)))) 
+5
source

Source: https://habr.com/ru/post/920061/


All Articles