我正在从事SICP的练习。
在Ex1.22中,我对Scheme中科学计数法的性能提出了疑问。
此练习是查找大于指定值的指定质数计数。
; 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))
我的问题是以下两个呼叫的性能差异:
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
显然,科学记数法需要花费更多时间。为什么会这样?
我的代码正在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
答案 0 :(得分:2)
虽然在Scheme中将文字1000000000000
读为一个精确整数,但是1e12
不能理解为精确整数,并且将成为浮点数。要将科学符号用于确切的数字,您应该使用#e
前缀或使用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)
当数字不是整数时,它也变成有理数:
#e0.5 ; ==> 1/2
为完整起见,您也可以相反。例如。 #i1000000000000
等效于1e12
,(exact->inexact 1000000000000)
也等效。
在R6RS之前,不需要具有完整的数字塔。该报告甚至提到只有浮点数的Scheme可能会有用。对于R5RS及更早版本,您应查阅实现文档以查看其是否支持完整的数字塔。 MIT计划在其文档中指出,他们实现了完整的数字塔。