Search code examples
schemesicpchicken-scheme

Strange output with pi calculating in Chicken Scheme


The version info: Version 4.9.0.1 (stability/4.9.0) (rev 8b3189b) macosx-unix-clang-x86-64

The code is actually for exercise 1.3.1 in SICP:

(define (product term a next b)
    (define (iter a result)
        (if (> a b)
            result
            (iter (next a) (* (term a) result))
        )
    )
    (iter a 1)
)

(define (get-pi n)
    (define (next x) (+ x 2))
    (define (term x) (* x x))
    (* 8 n (/ (product term 4 next n) (product term 3 next (+ n 1))))
)

The output:

#;102> (get-pi 165) 
3.13208714360219
#;103> (get-pi 167)
3.13220081034665
#;104> (get-pi 169)
3.13231179078142
#;105> (get-pi 170)
0.0

Why the result became 0.0?

Thank you!


Solution

  • Chicken doesn't implement the full numeric tower by default. You need to (use numbers).

    I don't have Chicken installed, but you might have to use (exact->inexact (get-pi 170)) to get the same results as before.