]> gitweb.factorcode.org Git - factor.git/blob - core/math/math.factor
compiler: Fix bitand on ratios, floats. Fix shift on ratios, floats. Add integer...
[factor.git] / core / math / math.factor
1 ! Copyright (C) 2003, 2009 Slava Pestov, Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel ;
4 IN: math
5
6 GENERIC: >fixnum ( x -- n ) foldable
7 GENERIC: >bignum ( x -- n ) foldable
8 GENERIC: >integer ( x -- n ) foldable
9 GENERIC: >float ( x -- y ) foldable
10 GENERIC: integer>fixnum ( x -- y ) foldable
11
12 GENERIC: numerator ( a/b -- a )
13 GENERIC: denominator ( a/b -- b )
14
15 GENERIC: real-part ( z -- x )
16 GENERIC: imaginary-part ( z -- y )
17
18 MATH: number= ( x y -- ? ) foldable
19
20 M: object number= 2drop f ;
21
22 MATH: <  ( x y -- ? ) foldable
23 MATH: <= ( x y -- ? ) foldable
24 MATH: >  ( x y -- ? ) foldable
25 MATH: >= ( x y -- ? ) foldable
26
27 MATH: unordered? ( x y -- ? ) foldable
28 MATH: u<  ( x y -- ? ) foldable
29 MATH: u<= ( x y -- ? ) foldable
30 MATH: u>  ( x y -- ? ) foldable
31 MATH: u>= ( x y -- ? ) foldable
32
33 M: object unordered? 2drop f ;
34
35 MATH: +   ( x y -- z ) foldable
36 MATH: -   ( x y -- z ) foldable
37 MATH: *   ( x y -- z ) foldable
38 MATH: /   ( x y -- z ) foldable
39 MATH: /f  ( x y -- z ) foldable
40 MATH: /i  ( x y -- z ) foldable
41 MATH: mod ( x y -- z ) foldable
42
43 MATH: /mod ( x y -- z w ) foldable
44
45 MATH: bitand ( x y -- z ) foldable
46 MATH: bitor  ( x y -- z ) foldable
47 MATH: bitxor ( x y -- z ) foldable
48 GENERIC# shift 1 ( x n -- y ) foldable
49 GENERIC: bitnot ( x -- y ) foldable
50 GENERIC# bit? 1 ( x n -- ? ) foldable
51
52 GENERIC: abs ( x -- y ) foldable
53
54 <PRIVATE
55
56 GENERIC: (log2) ( x -- n ) foldable
57
58 PRIVATE>
59
60 ERROR: log2-expects-positive x ;
61
62 : log2 ( x -- n )
63     dup 0 <= [ log2-expects-positive ] [ (log2) ] if ; inline
64
65 : zero? ( x -- ? ) 0 number= ; inline
66 : 2/ ( x -- y ) -1 shift ; inline
67 : sq ( x -- y ) dup * ; inline
68 : neg ( x -- -x ) -1 * ; inline
69 : recip ( x -- y ) 1 swap / ; inline
70 : sgn ( x -- n ) dup 0 < [ drop -1 ] [ 0 > 1 0 ? ] if ; inline
71 : ?1+ ( x -- y ) [ 1 + ] [ 0 ] if* ; inline
72 : rem ( x y -- z ) abs [ mod ] [ + ] [ mod ] tri ; foldable
73 : 2^ ( n -- 2^n ) 1 swap shift ; inline
74 : even? ( n -- ? ) 1 bitand zero? ; inline
75 : odd? ( n -- ? ) 1 bitand 1 number= ; inline
76
77 GENERIC: neg? ( x -- -x )
78
79 : if-zero ( ..a n quot1: ( ..a -- ..b ) quot2: ( ..a n -- ..b ) -- ..b )
80     [ dup zero? ] [ [ drop ] prepose ] [ ] tri* if ; inline
81
82 : when-zero ( ..a n quot: ( ..a -- ..b ) -- ..b ) [ ] if-zero ; inline
83
84 : unless-zero ( ..a n quot: ( ..a -- ..b ) -- ..b ) [ ] swap if-zero ; inline
85
86 UNION: integer fixnum bignum ;
87
88 TUPLE: ratio { numerator integer read-only } { denominator integer read-only } ;
89
90 UNION: rational integer ratio ;
91
92 M: rational neg? 0 < ; inline
93
94 UNION: real rational float ;
95
96 TUPLE: complex { real real read-only } { imaginary real read-only } ;
97
98 UNION: number real complex ;
99
100 : fp-bitwise= ( x y -- ? ) [ double>bits ] same? ; inline
101
102 GENERIC: fp-special? ( x -- ? )
103 GENERIC: fp-nan? ( x -- ? )
104 GENERIC: fp-qnan? ( x -- ? )
105 GENERIC: fp-snan? ( x -- ? )
106 GENERIC: fp-infinity? ( x -- ? )
107 GENERIC: fp-nan-payload ( x -- bits )
108 GENERIC: fp-sign ( x -- ? )
109
110 M: object fp-special? drop f ; inline
111 M: object fp-nan? drop f ; inline
112 M: object fp-qnan? drop f ; inline
113 M: object fp-snan? drop f ; inline
114 M: object fp-infinity? drop f ; inline
115
116 : <fp-nan> ( payload -- nan )
117     0x7ff0000000000000 bitor bits>double ; inline
118
119 GENERIC: next-float ( m -- n )
120 GENERIC: prev-float ( m -- n )
121
122 : next-power-of-2 ( m -- n )
123     dup 2 <= [ drop 2 ] [ 1 - log2 1 + 2^ ] if ; inline
124
125 : power-of-2? ( n -- ? )
126     dup 0 <= [ drop f ] [ dup 1 - bitand zero? ] if ; foldable
127
128 : align ( m w -- n )
129     1 - [ + ] keep bitnot bitand ; inline
130
131 <PRIVATE
132
133 : iterate-prep ( n quot -- i n quot ) [ 0 ] 2dip ; inline
134
135 : if-iterate? ( i n true false -- ) [ 2over < ] 2dip if ; inline
136
137 : iterate-step ( i n quot -- i n quot )
138     #! Apply quot to i, keep i and quot, hide n.
139     [ nip call ] 3keep ; inline
140
141 : iterate-next ( i n quot -- i' n quot ) [ 1 + ] 2dip ; inline
142
143 PRIVATE>
144
145 : (each-integer) ( ... i n quot: ( ... i -- ... ) -- ... )
146     [ iterate-step iterate-next (each-integer) ]
147     [ 3drop ] if-iterate? ; inline recursive
148
149 : (find-integer) ( ... i n quot: ( ... i -- ... ? ) -- ... i )
150     [
151         iterate-step
152         [ [ ] ] 2dip
153         [ iterate-next (find-integer) ] 2curry bi-curry if
154     ] [ 3drop f ] if-iterate? ; inline recursive
155
156 : (all-integers?) ( ... i n quot: ( ... i -- ... ? ) -- ... ? )
157     [
158         iterate-step
159         [ iterate-next (all-integers?) ] 3curry
160         [ f ] if
161     ] [ 3drop t ] if-iterate? ; inline recursive
162
163 : each-integer ( ... n quot: ( ... i -- ... ) -- ... )
164     iterate-prep (each-integer) ; inline
165
166 : times ( ... n quot: ( ... -- ... ) -- ... )
167     [ drop ] prepose each-integer ; inline
168
169 : find-integer ( ... n quot: ( ... i -- ... ? ) -- ... i )
170     iterate-prep (find-integer) ; inline
171
172 : all-integers? ( ... n quot: ( ... i -- ... ? ) -- ... ? )
173     iterate-prep (all-integers?) ; inline
174
175 : find-last-integer ( ... n quot: ( ... i -- ... ? ) -- ... i )
176     over 0 < [
177         2drop f
178     ] [
179         [ call ] 2keep rot [
180             drop
181         ] [
182             [ 1 - ] dip find-last-integer
183         ] if
184     ] if ; inline recursive