schemechicken-scheme

Printing decimal numbers get rounded up in chicken-scheme


I have a scheme procedure that returns 0.24999999999999992 as a result. However, when I tried to print this result with chicken-scheme on my machine, it gets rounded to 0.25. How can I prevent rounding?

I tried running the same procedure on repl.it, and the print command here outputs the result without rounding.

If it helps, the code below:

(define (sum term a next b)
  (if
    (> a b)
    0
    (+ (term a) (sum term (next a) next b))
  )
)

(define (integral-simpson f a b n)
  (define h (/ (- b a) n))
  (define (inc x) (+ x 1))
  (define (term x)
    (cond
      ((or (= x 0) (= x n)) (f (+ a (* x h))))
      ((even? x) (* 2 (f (+ a (* x h)))))
      ((odd? x) (* 4 (f (+ a (* x h)))))
    )
  )

  (* (/ h 3)
    (sum
      term
      a
      inc
      n
    )
  )
)

(define (cube x)
  (* x x x)
)

(print (integral-simpson cube 0 1 100))

Solution

  • Try changing the print precision: (flonum-print-precision 17)