math: faster recip for integers and ratios.

db4
John Benediktsson 2012-09-02 09:47:31 -07:00
parent b39547a866
commit 009fb4249e
2 changed files with 14 additions and 1 deletions

View File

@ -33,6 +33,16 @@ M: integer /
2dup fast-gcd [ /i ] curry bi@ fraction>
] if-zero ;
M: integer recip
1 swap [
division-by-zero
] [
dup 0 < [ [ neg ] bi@ ] when fraction>
] if-zero ;
M: ratio recip
>fraction swap fraction> ;
M: ratio hashcode*
nip >fraction [ hashcode ] bi@ bitxor ;

View File

@ -69,7 +69,6 @@ ERROR: log2-expects-positive x ;
: 2/ ( x -- y ) -1 shift ; inline
: sq ( x -- y ) dup * ; inline
: neg ( x -- -x ) -1 * ; inline
: recip ( x -- y ) 1 swap / ; inline
: sgn ( x -- n ) dup 0 < [ drop -1 ] [ 0 > 1 0 ? ] if ; inline
: ?1+ ( x -- y ) [ 1 + ] [ 0 ] if* ; inline
: rem ( x y -- z ) abs [ mod ] [ + ] [ mod ] tri ; foldable
@ -100,6 +99,10 @@ TUPLE: complex { real real read-only } { imaginary real read-only } ;
UNION: number real complex ;
GENERIC: recip ( x -- y )
M: number recip 1 swap / ; inline
: fp-bitwise= ( x y -- ? ) [ double>bits ] same? ; inline
GENERIC: fp-special? ( x -- ? )