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