On 2013-04-24, at 3:21 PM, Zhen Shen zhenshen10@outlook.com wrote:
Marc,
Sorry if my joke didn't come across as one. I am new to scheme.
If I understand you correctly, the calculations in this stmt will be performed with bignum arith?
(declare (flonum)) (if (> x 0.01) (* x a 3.0) (/ x a))
Certainly not bignum arithmetic, but possibly flonum arithmetic depending on your declarations. You can verify by using the -expansion option:
% cat flo.scm (declare (standard-bindings) (flonum) (not safe))
(if (> x 0.01) (* x a 3.0) (/ x a)) % gsc -c -expansion flo.scm Expansion:
(if ('#<procedure #2 ##fl>> x .01) ('#<procedure #3 ##fl*> x a 3.) ('#<procedure #4 ##fl/> x a))
So here all operations are done on flonums.
and to avoid that for max speed, i'd rewrite it as follows??
(declare (flonum)) (define x ..) (define a ..)
(define p (* x a 3.0)) (define q (/ x a)) (define r (> x 0.01)) (if r p q)
That will probably run slower because the test won't be inlined. I was talking about this case:
(let ((x (+ a b))) (if (< a 0.0) (* x x) (/ 1.0 x)))
Here the x will be boxed because it crosses a jump (the if).
Marc