PasteRack.org
Paste # 39298
2015-05-04 14:58:17

Fork as a new paste.

Paste viewed 1263 times.


Embed:

If 1+x is 1, how much is 1-x?

  1. #lang racket
  2.  
  3. (define (almost-one bits extra one)
  4.   (for ([i (in-range (add1 (expt 2 extra)))])
  5.     (define x (/ i (expt 2 (+ bits extra))))
  6.     (displayln (format "~a/2^~a ~a ~a ~a"
  7.                        (~r (* x (expt 2 bits)) #:precision '(= 4))
  8.                        bits
  9.                        (= (+ one x) one)
  10.                        (= (- one x) one)
  11.                        (~r x #:notation 'exponential)))))
  12.  
  13. (displayln "Double Precision (32 bits)")
  14. (almost-one 52 4 1.0)
  15. (newline)
  16. (displayln "Single Precision (16 bits)")
  17. (almost-one 23 4 1.f0)

=>

Double Precision (32 bits)

0.0000/2^52 #t #t 0e+00

0.0625/2^52 #t #t 1.387779e-17

0.1250/2^52 #t #t 2.775558e-17

0.1875/2^52 #t #t 4.163336e-17

0.2500/2^52 #t #t 5.551115e-17

0.3125/2^52 #t #f 6.938894e-17

0.3750/2^52 #t #f 8.326673e-17

0.4375/2^52 #t #f 9.714451e-17

0.5000/2^52 #t #f 1.110223e-16

0.5625/2^52 #f #f 1.249001e-16

0.6250/2^52 #f #f 1.387779e-16

0.6875/2^52 #f #f 1.526557e-16

0.7500/2^52 #f #f 1.665335e-16

0.8125/2^52 #f #f 1.804112e-16

0.8750/2^52 #f #f 1.94289e-16

0.9375/2^52 #f #f 2.081668e-16

1.0000/2^52 #f #f 2.220446e-16

Single Precision (16 bits)

0.0000/2^23 #t #t 0e+00

0.0625/2^23 #t #t 7.450581e-09

0.1250/2^23 #t #t 1.490116e-08

0.1875/2^23 #t #t 2.235174e-08

0.2500/2^23 #t #t 2.980232e-08

0.3125/2^23 #t #f 3.72529e-08

0.3750/2^23 #t #f 4.470348e-08

0.4375/2^23 #t #f 5.215406e-08

0.5000/2^23 #t #f 5.960464e-08

0.5625/2^23 #f #f 6.705523e-08

0.6250/2^23 #f #f 7.450581e-08

0.6875/2^23 #f #f 8.195639e-08

0.7500/2^23 #f #f 8.940697e-08

0.8125/2^23 #f #f 9.685755e-08

0.8750/2^23 #f #f 1.043081e-07

0.9375/2^23 #f #f 1.117587e-07

1.0000/2^23 #f #f 1.192093e-07