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