-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 19-May-07, at 11:28 PM, Bradley Lucier wrote:
Marc:
The compiler can expand (sqrt x) as
(define (sqrt x) (cond ((##fixnum? x) (if (and (##fixnum.<= 0 x) (or (##not (##fixnum? 4294967296)) ; 32-bit fixnums (##fixnum.<= x 4503599627370496))) (##flonum->fixnum (##flonum.sqrt (##flonum.<-fixnum x))) (##sqrt x))) ((and (##flonum? x) (##flonum.<= 0. x)) (##flonum.sqrt x)) (else (##sqrt x))))
It's very worthwhile for some programs (and some micro-benchmarks ;-).
This definition is incorrect because:
(sqrt 17)
4
You need something like this:
(define (sqrt x) (cond ((##fixnum? x) (if (and (##not (##fixnum.negative? x)) (or (##not (##fixnum? 4294967296)) ;; 32-bit fixnums (##fixnum.<= x 4503599627370496))) (let* ((f (##flonum.sqrt (##flonum.<-fixnum x))) (i (##flonum->fixnum s))) (if (##fixnum.= x (##fixnum.* i i)) i (##sqrt x))) (##sqrt x))) ((and (##flonum? x) (##not (##flonum.negative? x))) (##flonum.sqrt x)) (else (##sqrt x))))
But that's a lot of code! Can you give good examples of where this pays off?
Marc