diff --git a/basis/math/ratios/ratios.factor b/basis/math/ratios/ratios.factor index 1f95732a18..bf572ac352 100644 --- a/basis/math/ratios/ratios.factor +++ b/basis/math/ratios/ratios.factor @@ -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 ; diff --git a/core/math/math.factor b/core/math/math.factor index b86cc9565c..20593bd48a 100644 --- a/core/math/math.factor +++ b/core/math/math.factor @@ -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 -- ? )