From: Stephen Adams Date: Wed, 1 Nov 1995 16:51:08 +0000 (+0000) Subject: Extended the flonum efficiency tip. X-Git-Tag: 20090517-FFI~5797 X-Git-Url: https://birchwood-abbey.net/git?a=commitdiff_plain;h=5f36d00da2d27b69652c4d6826e4b0e4a4186330;p=mit-scheme.git Extended the flonum efficiency tip. --- diff --git a/v7/doc/user-manual/user.texinfo b/v7/doc/user-manual/user.texinfo index b240e0f90..6c306f62e 100644 --- a/v7/doc/user-manual/user.texinfo +++ b/v7/doc/user-manual/user.texinfo @@ -2,7 +2,7 @@ @iftex @finalout @end iftex -@comment $Id: user.texinfo,v 1.34 1995/10/31 02:21:50 adams Exp $ +@comment $Id: user.texinfo,v 1.35 1995/11/01 16:51:08 adams Exp $ @comment %**start of header (This is for running Texinfo on a region.) @setfilename user.info @settitle MIT Scheme User's Manual @@ -4276,6 +4276,30 @@ single arithmetic expression. If the expression contains conditionals or calls to procedures then the values tend to get boxed anyway. +@subsubheading A safer alternative + +An alternative to putting in @code{flo:} operations yourself is to let +the compiler try. +In the next definition of @code{distance}, the programmer explicitly +tests for flonum arguments and duplicates the expression. + +The compiler compiles the expressions differently: for the first +expression it knows from the conditional that @var{x} and @var{y} are +flonums and will replace both @code{*} and the @code{+} operators with +flonum operators. +It doesn't replace the @code{sqrt} operator, though, as it doesn't know +that sums of squares of reals are non-negative. + +This approach has the advantage of being completely safe, and +@code{distance} will still work for other kinds of number. + +@example +(define (distance x y) + (if (and (flo:flonum? x) (flo:flonum? y)) + (sqrt (+ (* x x) (* y y))) + (sqrt (+ (* x x) (* y y))))) +@end example + @subsubheading Flonum vectors Flonum vectors are vectors which contain only floating point values, in