]> gitweb.factorcode.org Git - factor.git/blob - core/math/math.factor
math: remove out-of-fixnum-range error.
[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: log2-expects-positive x ;
66
67 : log2 ( x -- n )
68     dup 0 <= [ log2-expects-positive ] [ (log2) ] if ; inline
69
70 : zero? ( x -- ? ) 0 number= ; inline
71 : 2/ ( x -- y ) -1 shift ; inline
72 : sq ( x -- y ) dup * ; inline
73 : neg ( x -- -x ) -1 * ; inline
74 : sgn ( x -- n ) dup 0 < [ drop -1 ] [ 0 > 1 0 ? ] if ; inline
75 : ?1+ ( x -- y ) [ 1 + ] [ 0 ] if* ; inline
76 : rem ( x y -- z ) abs [ mod ] [ + ] [ mod ] tri ; foldable
77 : 2^ ( n -- 2^n ) 1 swap shift ; inline
78 : even? ( n -- ? ) 1 bitand zero? ; inline
79 : odd? ( n -- ? ) 1 bitand 1 number= ; inline
80
81 GENERIC: neg? ( x -- -x )
82
83 : if-zero ( ..a n quot1: ( ..a -- ..b ) quot2: ( ..a n -- ..b ) -- ..b )
84     [ dup zero? ] [ [ drop ] prepose ] [ ] tri* if ; inline
85
86 : when-zero ( ..a n quot: ( ..a -- ..b ) -- ..b ) [ ] if-zero ; inline
87
88 : unless-zero ( ..a n quot: ( ..a n -- ..b ) -- ..b ) [ ] swap if-zero ; inline
89
90 UNION: integer fixnum bignum ;
91
92 TUPLE: ratio { numerator integer read-only } { denominator integer read-only } ;
93
94 UNION: rational integer ratio ;
95
96 M: rational neg? 0 < ; inline
97
98 UNION: real rational float ;
99
100 TUPLE: complex { real real read-only } { imaginary real read-only } ;
101
102 UNION: number real complex ;
103
104 GENERIC: recip ( x -- y )
105
106 M: number recip 1 swap / ; inline
107
108 : fp-bitwise= ( x y -- ? ) [ double>bits ] same? ; inline
109
110 GENERIC: fp-special? ( x -- ? )
111 GENERIC: fp-nan? ( x -- ? )
112 GENERIC: fp-qnan? ( x -- ? )
113 GENERIC: fp-snan? ( x -- ? )
114 GENERIC: fp-infinity? ( x -- ? )
115 GENERIC: fp-nan-payload ( x -- bits )
116 GENERIC: fp-sign ( x -- ? )
117
118 M: object fp-special? drop f ; inline
119 M: object fp-nan? drop f ; inline
120 M: object fp-qnan? drop f ; inline
121 M: object fp-snan? drop f ; inline
122 M: object fp-infinity? drop f ; inline
123
124 : <fp-nan> ( payload -- nan )
125     0x7ff0000000000000 bitor bits>double ; inline
126
127 GENERIC: next-float ( m -- n )
128 GENERIC: prev-float ( m -- n )
129
130 : next-power-of-2 ( m -- n )
131     dup 2 <= [ drop 2 ] [ 1 - log2 1 + 2^ ] if ; inline
132
133 : power-of-2? ( n -- ? )
134     dup 0 <= [ drop f ] [ dup 1 - bitand zero? ] if ; foldable
135
136 : align ( m w -- n )
137     1 - [ + ] keep bitnot bitand ; inline
138
139 <PRIVATE
140
141 : iterate-prep ( n quot -- i n quot ) [ 0 ] 2dip ; inline
142
143 : if-iterate? ( i n true false -- ) [ 2over < ] 2dip if ; inline
144
145 : iterate-step ( i n quot -- i n quot )
146     #! Apply quot to i, keep i and quot, hide n.
147     [ nip call ] 3keep ; inline
148
149 : iterate-rot ( ? i n quot -- i n quot ? )
150     [ rot ] dip swap ; inline
151
152 : iterate-next ( i n quot -- i' n quot ) [ 1 + ] 2dip ; inline
153
154 PRIVATE>
155
156 : (each-integer) ( ... i n quot: ( ... i -- ... ) -- ... )
157     [ iterate-step iterate-next (each-integer) ]
158     [ 3drop ] if-iterate? ; inline recursive
159
160 : (find-integer) ( ... i n quot: ( ... i -- ... ? ) -- ... i/f )
161     [
162         iterate-step iterate-rot
163         [ 2drop ] [ iterate-next (find-integer) ] if
164     ] [ 3drop f ] if-iterate? ; inline recursive
165
166 : (all-integers?) ( ... i n quot: ( ... i -- ... ? ) -- ... ? )
167     [
168         iterate-step iterate-rot
169         [ iterate-next (all-integers?) ] [ 3drop f ] if
170     ] [ 3drop t ] if-iterate? ; inline recursive
171
172 : each-integer ( ... n quot: ( ... i -- ... ) -- ... )
173     iterate-prep (each-integer) ; inline
174
175 : times ( ... n quot: ( ... -- ... ) -- ... )
176     [ drop ] prepose each-integer ; inline
177
178 : find-integer ( ... n quot: ( ... i -- ... ? ) -- ... i )
179     iterate-prep (find-integer) ; inline
180
181 : all-integers? ( ... n quot: ( ... i -- ... ? ) -- ... ? )
182     iterate-prep (all-integers?) ; inline
183
184 : find-last-integer ( ... n quot: ( ... i -- ... ? ) -- ... i )
185     over 0 < [
186         2drop f
187     ] [
188         [ call ] 2keep rot [
189             drop
190         ] [
191             [ 1 - ] dip find-last-integer
192         ] if
193     ] if ; inline recursive