Search code examples
schemescientific-notationsicp

Scientific notation in Scheme


I am working on the exercises of SICP.

In Ex1.22 I've got a question on the performance of scientific notation in Scheme.

This exercise is to find a specified count of prime numbers larger than a specified value.

; code to check whether a number is prime  
(define (smallest-divisor n)
  (find-divisor n 2))
(define (find-divisor n test-divisor)
  (cond ((> (square test-divisor) n) n)
        ((divides? test-divisor n) test-divisor)
        (else (find-divisor n (1+ test-divisor)))))
(define (divides? a b)
  (= (remainder b a) 0))
(define (prime? n)
  (= n (smallest-divisor n)))

; code to find prime numbers
; (search-for-primes 10 3) means find 3 prime numbers larger than 10
; the prime numbers and the time taken will be printed
(define (search-for-primes start count)
  (define (iter n c)
    (cond ((= c 0) (newline) (display "Done"))
          (else (iter (+ n 2) (- c (timed-prime-test n))))))
  (iter (if (even? start) (1+ start) start) 
        count))
(define (timed-prime-test n)
  (newline)
  (display n)
  (start-prime-test n (runtime)))
(define (start-prime-test n start-time)
  (cond ((prime? n)
         (report-prime (- (runtime) start-time))
         1)
        (else 0)))
(define (report-prime elapsed-time)
  (display " *** ")
  (display elapsed-time))

My question is the performance difference of below two calls:

1 ]=> (search-for-primes 1000000000000 3)

1000000000039 *** 2.319999999999993
1000000000061 *** 2.3799999999999955
1000000000063 *** 2.3599999999999994

1 ]=> (search-for-primes 1e12 3)

1000000000039. *** 4.990000000000009
1000000000061. *** 4.960000000000008
1000000000063. *** 4.959999999999994

Clearly scientific notation takes much more time. Why does this happen?

My code is running on the latest version of MIT-Scheme:

MIT/GNU Scheme running under GNU/Linux
Type `^C' (control-C) followed by `H' to obtain information about interrupts.

Copyright (C) 2018 Massachusetts Institute of Technology
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Image saved on Wednesday October 31, 2018 at 7:14:37 PM
  Release 10.1.2 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/i386 4.118

Solution

  • While the literal 1000000000000 is read in Scheme as an exact integer, 1e12 is not understood as exact and will become a floating point number. To use scientific notation for exact numbers you should use #e prefix or use inexact->exact:

    (eqv? 1000000000000 1e12)                  ; ==> #f (not the same value)
    (eqv? 1000000000000 #e1e12)                ; ==> #t (the same value)
    (eqv? 1000000000000 (inexact->exact 1e12)) ; ==> #t (the same value)
    

    Also when the number is not a whole numbers it becomes a rational number:

    #e0.5 ; ==> 1/2
    

    For completeness, you can do the opposite too. Eg. #i1000000000000 makes the equivalent to 1e12 and so does (exact->inexact 1000000000000).

    limitations

    Before R6RS there were no requirement to have a full numeric tower. The report even mentions that a Scheme with only floating point numbers might be useful. For R5RS and earlier you should consult the implementations documentation to see if it supports a full numeric tower or not. MIT Scheme states in their documentation that they implement the full numeric tower.