1 ! Copyright (C) 2003, 2007 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+ [ 1+ ] [ 0 ] if* ; inline
69 : rem ( x y -- z ) 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 M: number equal? number= ;
87 M: real hashcode* nip >fixnum ;
89 GENERIC: fp-nan? ( x -- ? )
95 double>bits -51 shift BIN: 111111111111 [ bitand ] keep
98 : (next-power-of-2) ( i n -- n )
102 >r 1 shift r> (next-power-of-2)
105 : next-power-of-2 ( m -- n ) 2 swap (next-power-of-2) ; foldable
107 : power-of-2? ( n -- ? )
108 dup 0 <= [ drop f ] [ dup 1- bitand zero? ] if ; foldable
111 1- [ + ] keep bitnot bitand ; inline
115 : iterate-prep 0 -rot ; inline
117 : if-iterate? >r >r 2over < r> r> if ; inline
119 : iterate-step ( i n quot -- i n quot )
120 #! Apply quot to i, keep i and quot, hide n.
121 swap >r 2dup 2slip r> swap ; inline
123 : iterate-next >r >r 1+ r> r> ; inline
127 : (each-integer) ( i n quot -- )
128 [ iterate-step iterate-next (each-integer) ]
129 [ 3drop ] if-iterate? ; inline
131 : (find-integer) ( i n quot -- i )
134 [ 2drop ] [ iterate-next (find-integer) ] if
135 ] [ 3drop f ] if-iterate? ; inline
137 : (all-integers?) ( i n quot -- ? )
140 [ iterate-next (all-integers?) ] [ 3drop f ] if
141 ] [ 3drop t ] if-iterate? ; inline
143 : each-integer ( n quot -- )
144 iterate-prep (each-integer) ; inline
146 : times ( n quot -- )
147 [ drop ] prepose each-integer ; inline
149 : find-integer ( n quot -- i )
150 iterate-prep (find-integer) ; inline
152 : all-integers? ( n quot -- ? )
153 iterate-prep (all-integers?) ; inline
155 : find-last-integer ( n quot -- i )
162 >r 1- r> find-last-integer