Scientific notation in Scheme










0















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









share|improve this question

















  • 1





    Scientific notation likely mean that number is floating point number.

    – PetSerAl
    Nov 14 '18 at 7:51











  • You should try #e1e12 to see if that solves the difference.

    – Sylwester
    Nov 14 '18 at 12:01











  • Both of you are correct. Thanks a lot for the answers.

    – Orpheus
    Nov 14 '18 at 15:45











  • @PetSerAI Could you please copy your words to a formal answer? And then I can mark it and close this question.

    – Orpheus
    Nov 14 '18 at 15:47















0















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









share|improve this question

















  • 1





    Scientific notation likely mean that number is floating point number.

    – PetSerAl
    Nov 14 '18 at 7:51











  • You should try #e1e12 to see if that solves the difference.

    – Sylwester
    Nov 14 '18 at 12:01











  • Both of you are correct. Thanks a lot for the answers.

    – Orpheus
    Nov 14 '18 at 15:45











  • @PetSerAI Could you please copy your words to a formal answer? And then I can mark it and close this question.

    – Orpheus
    Nov 14 '18 at 15:47













0












0








0


0






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









share|improve this question














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






scheme scientific-notation sicp






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 7:43









OrpheusOrpheus

306




306







  • 1





    Scientific notation likely mean that number is floating point number.

    – PetSerAl
    Nov 14 '18 at 7:51











  • You should try #e1e12 to see if that solves the difference.

    – Sylwester
    Nov 14 '18 at 12:01











  • Both of you are correct. Thanks a lot for the answers.

    – Orpheus
    Nov 14 '18 at 15:45











  • @PetSerAI Could you please copy your words to a formal answer? And then I can mark it and close this question.

    – Orpheus
    Nov 14 '18 at 15:47












  • 1





    Scientific notation likely mean that number is floating point number.

    – PetSerAl
    Nov 14 '18 at 7:51











  • You should try #e1e12 to see if that solves the difference.

    – Sylwester
    Nov 14 '18 at 12:01











  • Both of you are correct. Thanks a lot for the answers.

    – Orpheus
    Nov 14 '18 at 15:45











  • @PetSerAI Could you please copy your words to a formal answer? And then I can mark it and close this question.

    – Orpheus
    Nov 14 '18 at 15:47







1




1





Scientific notation likely mean that number is floating point number.

– PetSerAl
Nov 14 '18 at 7:51





Scientific notation likely mean that number is floating point number.

– PetSerAl
Nov 14 '18 at 7:51













You should try #e1e12 to see if that solves the difference.

– Sylwester
Nov 14 '18 at 12:01





You should try #e1e12 to see if that solves the difference.

– Sylwester
Nov 14 '18 at 12:01













Both of you are correct. Thanks a lot for the answers.

– Orpheus
Nov 14 '18 at 15:45





Both of you are correct. Thanks a lot for the answers.

– Orpheus
Nov 14 '18 at 15:45













@PetSerAI Could you please copy your words to a formal answer? And then I can mark it and close this question.

– Orpheus
Nov 14 '18 at 15:47





@PetSerAI Could you please copy your words to a formal answer? And then I can mark it and close this question.

– Orpheus
Nov 14 '18 at 15:47












1 Answer
1






active

oldest

votes


















2














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.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295239%2fscientific-notation-in-scheme%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    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.






    share|improve this answer





























      2














      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.






      share|improve this answer



























        2












        2








        2







        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 22:39

























        answered Nov 14 '18 at 22:34









        SylwesterSylwester

        34.6k22955




        34.6k22955





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295239%2fscientific-notation-in-scheme%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3