1 ! Copyright (C) 2003, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel math.private ;
6 GENERIC: >fixnum ( x -- n ) foldable
7 GENERIC: >bignum ( x -- n ) foldable
8 GENERIC: >integer ( x -- n ) foldable
9 GENERIC: >float ( x -- y ) foldable
11 GENERIC: numerator ( a/b -- a )
12 GENERIC: denominator ( a/b -- b )
14 GENERIC: real-part ( z -- x )
15 GENERIC: imaginary-part ( z -- y )
17 MATH: number= ( x y -- ? ) foldable
19 M: object number= 2drop f ;
21 MATH: < ( x y -- ? ) foldable
22 MATH: <= ( x y -- ? ) foldable
23 MATH: > ( x y -- ? ) foldable
24 MATH: >= ( x y -- ? ) foldable
26 MATH: + ( x y -- z ) foldable
27 MATH: - ( x y -- z ) foldable
28 MATH: * ( x y -- z ) foldable
29 MATH: / ( x y -- z ) foldable
30 MATH: /f ( x y -- z ) foldable
31 MATH: /i ( x y -- z ) foldable
32 MATH: mod ( x y -- z ) foldable
34 MATH: /mod ( x y -- z w ) foldable
36 MATH: bitand ( x y -- z ) foldable
37 MATH: bitor ( x y -- z ) foldable
38 MATH: bitxor ( x y -- z ) foldable
39 GENERIC# shift 1 ( x n -- y ) foldable
40 GENERIC: bitnot ( x -- y ) foldable
41 GENERIC# bit? 1 ( x n -- ? ) foldable
43 GENERIC: abs ( x -- y ) foldable
47 GENERIC: (log2) ( x -- n ) foldable
53 "log2 expects positive inputs" throw
58 : zero? ( x -- ? ) 0 number= ; inline
59 : 1+ ( x -- y ) 1 + ; inline
60 : 1- ( x -- y ) 1 - ; inline
61 : 2/ ( x -- y ) -1 shift ; inline
62 : sq ( x -- y ) dup * ; inline
63 : neg ( x -- -x ) 0 swap - ; inline
64 : recip ( x -- y ) 1 swap / ; inline
65 : sgn ( x -- n ) dup 0 < [ drop -1 ] [ 0 > 1 0 ? ] if ; inline
67 : ?1+ ( x -- y ) [ 1+ ] [ 0 ] if* ; inline
69 : rem ( x y -- z ) abs tuck mod over + swap mod ; foldable
71 : 2^ ( n -- 2^n ) 1 swap shift ; inline
73 : even? ( n -- ? ) 1 bitand zero? ;
75 : odd? ( n -- ? ) 1 bitand 1 number= ;
77 UNION: integer fixnum bignum ;
79 UNION: rational integer ratio ;
81 UNION: real rational float ;
83 UNION: number real complex ;
85 GENERIC: fp-nan? ( x -- ? )
91 double>bits -51 shift HEX: fff [ bitand ] keep = ;
93 GENERIC: fp-infinity? ( x -- ? )
95 M: object fp-infinity?
98 M: float fp-infinity? ( float -- ? )
100 dup -52 shift HEX: 7ff [ bitand ] keep = [
101 HEX: fffffffffffff bitand 0 =
106 : next-power-of-2 ( m -- n )
107 dup 2 <= [ drop 2 ] [ 1- log2 1+ 2^ ] if ; inline
109 : power-of-2? ( n -- ? )
110 dup 0 <= [ drop f ] [ dup 1- bitand zero? ] if ; foldable
113 1- [ + ] keep bitnot bitand ; inline
117 : iterate-prep ( n quot -- i n quot ) [ 0 ] 2dip ; inline
119 : if-iterate? ( i n true false -- ) [ 2over < ] 2dip if ; inline
121 : iterate-step ( i n quot -- i n quot )
122 #! Apply quot to i, keep i and quot, hide n.
123 swap [ 2dup 2slip ] dip swap ; inline
125 : iterate-next ( i n quot -- i' n quot ) [ 1+ ] 2dip ; inline
129 : (each-integer) ( i n quot: ( i -- ) -- )
130 [ iterate-step iterate-next (each-integer) ]
131 [ 3drop ] if-iterate? ; inline recursive
133 : (find-integer) ( i n quot: ( i -- ? ) -- i )
136 [ 2drop ] [ iterate-next (find-integer) ] if
137 ] [ 3drop f ] if-iterate? ; inline recursive
139 : (all-integers?) ( i n quot: ( i -- ? ) -- ? )
142 [ iterate-next (all-integers?) ] [ 3drop f ] if
143 ] [ 3drop t ] if-iterate? ; inline recursive
145 : each-integer ( n quot -- )
146 iterate-prep (each-integer) ; inline
148 : times ( n quot -- )
149 [ drop ] prepose each-integer ; inline
151 : find-integer ( n quot -- i )
152 iterate-prep (find-integer) ; inline
154 : all-integers? ( n quot -- ? )
155 iterate-prep (all-integers?) ; inline
157 : find-last-integer ( n quot: ( i -- ? ) -- i )
164 [ 1- ] dip find-last-integer
166 ] if ; inline recursive