schemeracketnumerical

How to find the minimum positive number that added to 1.0 gives something larger?


While translating some Fortran to Scheme/Racket I have come across the function:

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

How do I find the number in Scheme?


Solution

  • #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))))