]> gitweb.factorcode.org Git - factor.git/blob - core/math/math.factor
io.streams.tee: more tests
[factor.git] / core / math / math.factor
1 ! Copyright (C) 2003, 2009 Slava Pestov, Joe Groff.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: kernel kernel.private ;
4 IN: math
5
6 BUILTIN: fixnum ;
7 BUILTIN: bignum ;
8 BUILTIN: float ;
9
10 PRIMITIVE: bits>double ( n -- x )
11 PRIMITIVE: bits>float ( n -- x )
12 PRIMITIVE: double>bits ( x -- n )
13 PRIMITIVE: float>bits ( x -- n )
14
15 <PRIVATE
16 PRIMITIVE: bignum* ( x y -- z )
17 PRIMITIVE: bignum+ ( x y -- z )
18 PRIMITIVE: bignum- ( x y -- z )
19 PRIMITIVE: bignum-bit? ( x n -- ? )
20 PRIMITIVE: bignum-bitand ( x y -- z )
21 PRIMITIVE: bignum-bitnot ( x -- y )
22 PRIMITIVE: bignum-bitor ( x y -- z )
23 PRIMITIVE: bignum-bitxor ( x y -- z )
24 PRIMITIVE: bignum-gcd ( x y -- z )
25 PRIMITIVE: bignum-log2 ( x -- n )
26 PRIMITIVE: bignum-mod ( x y -- z )
27 PRIMITIVE: bignum-shift ( x y -- z )
28 PRIMITIVE: bignum/i ( x y -- z )
29 PRIMITIVE: bignum/mod ( x y -- z w )
30 PRIMITIVE: bignum< ( x y -- ? )
31 PRIMITIVE: bignum<= ( x y -- ? )
32 PRIMITIVE: bignum= ( x y -- ? )
33 PRIMITIVE: bignum> ( x y -- ? )
34 PRIMITIVE: bignum>= ( x y -- ? )
35 PRIMITIVE: bignum>fixnum ( x -- y )
36 PRIMITIVE: bignum>fixnum-strict ( x -- y )
37 PRIMITIVE: both-fixnums? ( x y -- ? )
38 PRIMITIVE: fixnum* ( x y -- z )
39 PRIMITIVE: fixnum*fast ( x y -- z )
40 PRIMITIVE: fixnum+ ( x y -- z )
41 PRIMITIVE: fixnum+fast ( x y -- z )
42 PRIMITIVE: fixnum- ( x y -- z )
43 PRIMITIVE: fixnum-bitand ( x y -- z )
44 PRIMITIVE: fixnum-bitnot ( x -- y )
45 PRIMITIVE: fixnum-bitor ( x y -- z )
46 PRIMITIVE: fixnum-bitxor ( x y -- z )
47 PRIMITIVE: fixnum-fast ( x y -- z )
48 PRIMITIVE: fixnum-mod ( x y -- z )
49 PRIMITIVE: fixnum-shift ( x y -- z )
50 PRIMITIVE: fixnum-shift-fast ( x y -- z )
51 PRIMITIVE: fixnum/i ( x y -- z )
52 PRIMITIVE: fixnum/i-fast ( x y -- z )
53 PRIMITIVE: fixnum/mod ( x y -- z w )
54 PRIMITIVE: fixnum/mod-fast ( x y -- z w )
55 PRIMITIVE: fixnum< ( x y -- ? )
56 PRIMITIVE: fixnum<= ( x y -- z )
57 PRIMITIVE: fixnum> ( x y -- ? )
58 PRIMITIVE: fixnum>= ( x y -- ? )
59 PRIMITIVE: fixnum>bignum ( x -- y )
60 PRIMITIVE: fixnum>float ( x -- y )
61 PRIMITIVE: float* ( x y -- z )
62 PRIMITIVE: float+ ( x y -- z )
63 PRIMITIVE: float- ( x y -- z )
64 PRIMITIVE: float-u< ( x y -- ? )
65 PRIMITIVE: float-u<= ( x y -- ? )
66 PRIMITIVE: float-u> ( x y -- ? )
67 PRIMITIVE: float-u>= ( x y -- ? )
68 PRIMITIVE: float/f ( x y -- z )
69 PRIMITIVE: float< ( x y -- ? )
70 PRIMITIVE: float<= ( x y -- ? )
71 PRIMITIVE: float= ( x y -- ? )
72 PRIMITIVE: float> ( x y -- ? )
73 PRIMITIVE: float>= ( x y -- ? )
74 PRIMITIVE: float>bignum ( x -- y )
75 PRIMITIVE: float>fixnum ( x -- y )
76 PRIVATE>
77
78 GENERIC: >fixnum ( x -- n ) foldable
79 GENERIC: >bignum ( x -- n ) foldable
80 GENERIC: >integer ( x -- n ) foldable
81 GENERIC: >float ( x -- y ) foldable
82 GENERIC: integer>fixnum ( x -- y ) foldable
83 GENERIC: integer>fixnum-strict ( x -- y ) foldable
84
85 GENERIC: numerator ( a/b -- a )
86 GENERIC: denominator ( a/b -- b )
87 GENERIC: >fraction ( a/b -- a b )
88
89 GENERIC: real-part ( z -- x )
90 GENERIC: imaginary-part ( z -- y )
91
92 MATH: number= ( x y -- ? ) foldable
93
94 M: object number= 2drop f ;
95
96 MATH: <  ( x y -- ? ) foldable
97 MATH: <= ( x y -- ? ) foldable
98 MATH: >  ( x y -- ? ) foldable
99 MATH: >= ( x y -- ? ) foldable
100
101 MATH: unordered? ( x y -- ? ) foldable
102 MATH: u<  ( x y -- ? ) foldable
103 MATH: u<= ( x y -- ? ) foldable
104 MATH: u>  ( x y -- ? ) foldable
105 MATH: u>= ( x y -- ? ) foldable
106
107 M: object unordered? 2drop f ;
108
109 MATH: +   ( x y -- z ) foldable
110 MATH: -   ( x y -- z ) foldable
111 MATH: *   ( x y -- z ) foldable
112 MATH: /   ( x y -- z ) foldable
113 MATH: /f  ( x y -- z ) foldable
114 MATH: /i  ( x y -- z ) foldable
115 MATH: mod ( x y -- z ) foldable
116
117 MATH: /mod ( x y -- z w ) foldable
118
119 MATH: bitand ( x y -- z ) foldable
120 MATH: bitor  ( x y -- z ) foldable
121 MATH: bitxor ( x y -- z ) foldable
122 GENERIC#: shift 1 ( x n -- y ) foldable
123 GENERIC: bitnot ( x -- y ) foldable
124 GENERIC#: bit? 1 ( x n -- ? ) foldable
125
126 GENERIC: abs ( x -- y ) foldable
127
128 <PRIVATE
129
130 GENERIC: (log2) ( x -- n ) foldable
131
132 PRIVATE>
133
134 ERROR: non-negative-number-expected n ;
135
136 : assert-non-negative ( n -- n )
137     dup 0 < [ non-negative-number-expected ] when ; inline
138
139 ERROR: positive-number-expected n ;
140
141 : assert-positive ( n -- n )
142     dup 0 > [ positive-number-expected ] unless ; inline
143
144 ERROR: negative-number-expected n ;
145
146 : assert-negative ( n -- n )
147     dup 0 < [ negative-number-expected ] unless ; inline
148
149 : recursive-hashcode ( n obj quot -- code )
150     pick 0 <= [ 3drop 0 ] [ [ 1 - ] 2dip call ] if ; inline
151
152 : log2 ( x -- n ) assert-positive (log2) ; inline
153 : zero? ( x -- ? ) 0 number= ; inline
154 : 2/ ( x -- y ) -1 shift ; inline
155 : sq ( x -- y ) dup * ; inline
156 : neg ( x -- -x ) -1 * ; inline
157 : sgn ( x -- n ) dup 0 < [ drop -1 ] [ 0 > 1 0 ? ] if ; inline
158 : ?1+ ( x -- y ) [ 1 + ] [ 0 ] if* ; inline
159 : rem ( x y -- z ) abs [ mod ] [ + ] [ mod ] tri ; foldable
160 : 2^ ( n -- 2^n ) 1 swap shift ; inline
161 : even? ( n -- ? ) 1 bitand zero? ; inline
162 : odd? ( n -- ? ) 1 bitand 1 number= ; inline
163
164 : bit-length ( x -- n )
165     assert-non-negative dup 1 > [ log2 1 + ] when ;
166
167 GENERIC: neg? ( x -- ? )
168
169 : if-zero ( ..a n quot1: ( ..a -- ..b ) quot2: ( ..a n -- ..b ) -- ..b )
170     [ dup zero? ] [ [ drop ] prepose ] [ ] tri* if ; inline
171
172 : when-zero ( ... n quot: ( ... -- ... x ) -- ... x ) [ ] if-zero ; inline
173
174 : unless-zero ( ... n quot: ( ... n -- ... ) -- ... ) [ ] swap if-zero ; inline
175
176 : until-zero ( ... n quot: ( ... x -- ... y ) -- ... ) [ dup zero? ] swap until drop ; inline
177
178 UNION: integer fixnum bignum ;
179
180 TUPLE: ratio
181     { numerator integer read-only }
182     { denominator integer read-only } ;
183
184 UNION: rational integer ratio ;
185
186 M: rational neg? 0 < ; inline
187
188 UNION: real rational float ;
189
190 TUPLE: complex
191     { real real read-only }
192     { imaginary real read-only } ;
193
194 UNION: number real complex ;
195
196 GENERIC: recip ( x -- y )
197
198 M: number recip 1 swap / ; inline
199
200 : rect> ( x y -- z )
201     ! Note: an imaginary 0.0 should still create a complex
202     dup 0 = [ drop ] [ complex boa ] if ; inline
203
204 GENERIC: >rect ( z -- x y )
205
206 M: real >rect 0 ; inline
207
208 M: complex >rect [ real-part ] [ imaginary-part ] bi ; inline
209
210 <PRIVATE
211
212 : (gcd) ( b a x y -- a d )
213     swap [
214         nip
215     ] [
216         [ /mod [ over * swapd - ] dip ] keep (gcd)
217     ] if-zero ; inline recursive
218
219 PRIVATE>
220
221 : gcd ( x y -- a d )
222     [ 0 1 ] 2dip (gcd) dup 0 < [ neg ] when ; inline
223
224 MATH: simple-gcd ( x y -- d ) foldable
225
226 <PRIVATE
227
228 : fixnum-gcd ( x y -- d ) { fixnum fixnum } declare gcd nip ;
229
230 PRIVATE>
231
232 M: fixnum simple-gcd fixnum-gcd ; inline
233
234 M: bignum simple-gcd bignum-gcd ; inline
235
236 M: real simple-gcd gcd nip ; inline
237
238 : lcm ( a b -- c )
239     [ * dup zero? ] 2keep '[ _ _ simple-gcd / ] unless ; foldable
240
241 : fp-bitwise= ( x y -- ? ) [ double>bits ] same? ; inline
242
243 GENERIC: fp-special? ( x -- ? )
244 GENERIC: fp-nan? ( x -- ? )
245 GENERIC: fp-qnan? ( x -- ? )
246 GENERIC: fp-snan? ( x -- ? )
247 GENERIC: fp-infinity? ( x -- ? )
248 GENERIC: fp-nan-payload ( x -- bits )
249 GENERIC: fp-sign ( x -- ? )
250
251 M: object fp-special? drop f ; inline
252 M: object fp-nan? drop f ; inline
253 M: object fp-qnan? drop f ; inline
254 M: object fp-snan? drop f ; inline
255 M: object fp-infinity? drop f ; inline
256
257 : <fp-nan> ( payload -- nan )
258     0x7ff0000000000000 bitor bits>double ; inline
259
260 GENERIC: next-float ( m -- n )
261 GENERIC: prev-float ( m -- n )
262
263 : next-power-of-2 ( m -- n )
264     dup 2 <= [ drop 2 ] [ 1 - log2 1 + 2^ ] if ; inline
265
266 : power-of-2? ( n -- ? )
267     dup 0 <= [ drop f ] [ dup 1 - bitand zero? ] if ; foldable
268
269 : align ( m w -- n )
270     1 - [ + ] keep bitnot bitand ; inline
271
272 : each-integer-from ( ... from to quot: ( ... i -- ... ) -- ... )
273     2over < [
274         [ nip call ] 3keep
275         [ 1 + ] 2dip each-integer-from
276     ] [
277         3drop
278     ] if ; inline recursive
279
280 : each-integer ( ... n quot: ( ... i -- ... ) -- ... )
281     [ 0 ] 2dip each-integer-from ; inline
282
283 : times ( ... n quot: ( ... -- ... ) -- ... )
284     [ drop ] prepose each-integer ; inline
285
286 : find-integer-from ( ... i n quot: ( ... i -- ... ? ) -- ... i/f )
287     2over < [
288         [ nip call ] 3keep roll
289         [ 2drop ]
290         [ [ 1 + ] 2dip find-integer-from ] if
291     ] [
292         3drop f
293     ] if ; inline recursive
294
295 : find-integer ( ... n quot: ( ... i -- ... ? ) -- ... i/f )
296     [ 0 ] 2dip find-integer-from ; inline
297
298 : find-last-integer ( ... n quot: ( ... i -- ... ? ) -- ... i/f )
299     over 0 < [
300         2drop f
301     ] [
302         [ call ] 2keep rot [
303             drop
304         ] [
305             [ 1 - ] dip find-last-integer
306         ] if
307     ] if ; inline recursive
308
309 : all-integers-from? ( ... from to quot: ( ... i -- ... ? ) -- ... ? )
310     2over < [
311         [ nip call ] 3keep roll
312         [ [ 1 + ] 2dip all-integers-from? ]
313         [ 3drop f ] if
314     ] [
315         3drop t
316     ] if ; inline recursive
317
318 : all-integers? ( ... n quot: ( ... i -- ... ? ) -- ... ? )
319     [ 0 ] 2dip all-integers-from? ; inline