Scheme 图式中的科学符号

Scheme 图式中的科学符号,scheme,scientific-notation,sicp,Scheme,Scientific Notation,Sicp,我正在做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) ((

我正在做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

虽然文本
10000000000
在Scheme中被读取为一个精确整数,
1e12
不被理解为精确的,它将成为一个浮点数。要对精确数字使用科学记数法,应使用
#e
前缀或
不精确->精确

(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
为了完整性,您也可以做相反的事情。例如,
#i10000000000
使其等同于
1e12
(精确->不精确10000000000)

局限性
在R6RS之前,没有要求有一个完整的数字塔。报告甚至提到只有浮点数的方案可能有用。对于R5R和更早版本,您应该参考实现文档,看看它是否支持完整的数字塔。MIT Scheme在其文档中声明,他们实现了完整的数字塔。

虽然文本
10000000000
在Scheme中被读取为精确整数,
1e12
不被理解为精确数,将成为浮点数。要对精确数字使用科学记数法,应使用
#e
前缀或
不精确->精确

(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
为了完整性,您也可以做相反的事情。例如,
#i10000000000
使其等同于
1e12
(精确->不精确10000000000)

局限性
在R6RS之前,没有要求有一个完整的数字塔。报告甚至提到只有浮点数的方案可能有用。对于R5R和更早版本,您应该参考实现文档,看看它是否支持完整的数字塔。MIT Scheme在他们的文档中指出,他们实现了完整的数字塔。

科学记数法可能意味着数字是浮点数。你应该试试
#e12
,看看这是否解决了差异。你们都是正确的。非常感谢你的回答。@PetSerAI你能把你的话复制成正式的回答吗?然后我可以标记它并结束这个问题。科学记数法可能意味着数字是浮点数。你应该试试
#e12
,看看这是否解决了差异。你们都是正确的。非常感谢你的回答。@PetSerAI你能把你的话复制成正式的回答吗?然后我可以标记它并结束这个问题。