]> gitweb.factorcode.org Git - factor.git/blob - core/math/math-docs.factor
b15f09e49d44ecfd79b5365758326a242cb16e96
[factor.git] / core / math / math-docs.factor
1 USING: help.markup help.syntax kernel sequences quotations
2 math.private ;
3 IN: math
4
5 ARTICLE: "division-by-zero" "Division by zero"
6 "Floating point division never raises an error if the denominator is zero. This means that if at least one of the two inputs to " { $link / } ", " { $link /f } " or " { $link mod } " is a float, the result will be a floating point infinity or not a number value."
7 $nl
8 "The behavior of integer division is hardware specific. On x86 processors, " { $link /i } " and " { $link mod } " raise an error if both inputs are integers and the denominator is zero. On PowerPC, integer division by zero yields a result of zero."
9 $nl
10 "On the other hand, the " { $link / } " word, when given integer arguments, implements a much more expensive division algorithm which always yields an exact rational answer, and this word always tests for division by zero explicitly." ;
11
12 ARTICLE: "number-protocol" "Number protocol"
13 "Math operations obey certain numerical upgrade rules. If one of the inputs is a bignum and the other is a fixnum, the latter is first coerced to a bignum; if one of the inputs is a float, the other is coerced to a float."
14 $nl
15 "Two examples where you should note the types of the inputs and outputs:"
16 { $example "3 >fixnum 6 >bignum * class ." "bignum" }
17 { $example "1/2 2.0 + ." "4.5" }
18 "The following usual operations are supported by all numbers."
19 { $subsection + }
20 { $subsection - }
21 { $subsection * }
22 { $subsection / }
23 "Non-commutative operations take operands from the stack in the natural order; " { $snippet "6 2 /" } " divides 6 by 2."
24 { $subsection "division-by-zero" }
25 "Real numbers (but not complex numbers) can be ordered:"
26 { $subsection < }
27 { $subsection <= }
28 { $subsection > }
29 { $subsection >= } ;
30
31 ARTICLE: "modular-arithmetic" "Modular arithmetic"
32 { $subsection mod }
33 { $subsection rem }
34 { $subsection /mod }
35 { $subsection /i }
36 { $see-also "integer-functions" } ;
37
38 ARTICLE: "bitwise-arithmetic" "Bitwise arithmetic"
39 "There are two ways of looking at an integer -- as an abstract mathematical entity, or as a string of bits. The latter representation motivates " { $emphasis "bitwise operations" } "."
40 { $subsection bitand }
41 { $subsection bitor }
42 { $subsection bitxor }
43 { $subsection bitnot }
44 { $subsection shift }
45 { $subsection 2/ }
46 { $subsection 2^ }
47 { $subsection bit? }
48 { $see-also "conditionals" } ;
49
50 ARTICLE: "arithmetic" "Arithmetic"
51 "Factor attempts to preserve natural mathematical semantics for numbers. Multiplying two large integers never results in overflow, and dividing two integers yields an exact ratio. Floating point numbers are also supported, along with complex numbers."
52 $nl
53 "Math words are in the " { $vocab-link "math" } " vocabulary. Implementation details are in the " { $vocab-link "math.private" } " vocabulary."
54 { $subsection "number-protocol" }
55 { $subsection "modular-arithmetic" }
56 { $subsection "bitwise-arithmetic" }
57 { $see-also "integers" "rationals" "floats" "complex-numbers" } ;
58
59 ABOUT: "arithmetic"
60
61 HELP: number=
62 { $values { "x" number } { "y" number } { "?" "a boolean" } }
63 { $description "Tests if two numbers have the same numerical value. If either input is not a number, outputs " { $link f } "." }
64 { $notes "Do not call this word directly. Calling " { $link = } " has the same effect and is more concise." } ;
65
66 HELP: <
67 { $values { "x" real } { "y" real } { "?" "a boolean" } }
68 { $description "Tests if " { $snippet "x" } " is less than " { $snippet "y" } "." } ;
69
70 HELP: <=
71 { $values { "x" real } { "y" real } { "?" "a boolean" } }
72 { $description "Tests if " { $snippet "x" } " is less than or equal to " { $snippet "y" } "." } ;
73
74 HELP: >
75 { $values { "x" real } { "y" real } { "?" "a boolean" } }
76 { $description "Tests if " { $snippet "x" } " is greater than " { $snippet "y" } "." } ;
77
78 HELP: >=
79 { $values { "x" real } { "y" real } { "?" "a boolean" } }
80 { $description "Tests if " { $snippet "x" } " is greater than or equal to " { $snippet "y" } "." } ;
81
82
83 HELP: +
84 { $values { "x" number } { "y" number } { "z" number } }
85 { $description
86     "Adds two numbers."
87     { $list
88         "Addition of fixnums may overflow and convert the result to a bignum."
89         "Addition of bignums always yields a bignum."
90         "Addition of floats always yields a float."
91         "Addition of ratios and complex numbers proceeds using the relevant mathematical rules."
92     }
93 } ;
94
95 HELP: -
96 { $values { "x" number } { "y" number } { "z" number } }
97 { $description
98     "Subtracts " { $snippet "y" } " from " { $snippet "x" } "."
99     { $list
100         "Subtraction of fixnums may overflow and convert the result to a bignum."
101         "Subtraction of bignums always yields a bignum."
102         "Subtraction of floats always yields a float."
103         "Subtraction of ratios and complex numbers proceeds using the relevant mathematical rules."
104     }
105 } ;
106
107 HELP: *
108 { $values { "x" number } { "y" number } { "z" number } }
109 { $description
110     "Multiplies two numbers."
111     { $list
112         "Multiplication of fixnums may overflow and convert the result to a bignum."
113         "Multiplication of bignums always yields a bignum."
114         "Multiplication of floats always yields a float."
115         "Multiplication of ratios and complex numbers proceeds using the relevant mathematical rules."
116     }
117 } ;
118
119 HELP: /
120 { $values { "x" number } { "y" number } { "z" number } }
121 { $description
122     "Divides " { $snippet "x" } " by " { $snippet "y" } ", retaining as much precision as possible."
123     { $list
124         "Division of fixnums may yield a ratio, or overflow and yield a bignum."
125         "Division of bignums may yield a ratio."
126         "Division of floats always yields a float."
127         "Division of ratios and complex numbers proceeds using the relevant mathematical rules."
128     }
129 }
130 { $see-also "division-by-zero" } ;
131
132 HELP: /i
133 { $values { "x" real } { "y" real } { "z" real } }
134 { $description
135     "Divides " { $snippet "x" } " by " { $snippet "y" } ", truncating the result to an integer."
136     { $list
137         "Integer division of fixnums may overflow and yield a bignum."
138         "Integer division of bignums always yields a bignum."
139         "Integer division of floats always yields a float."
140         "Integer division of ratios and complex numbers proceeds using the relevant mathematical rules."
141     }
142 }
143 { $see-also "division-by-zero" } ;
144
145 HELP: /f
146 { $values { "x" real } { "y" real } { "z" real } }
147 { $description
148     "Divides " { $snippet "x" } " by " { $snippet "y" } ", representing the result as a floating point number."
149     { $list 
150         "Integer division of fixnums may overflow and yield a bignum."
151         "Integer division of bignums always yields a bignum."            
152         "Integer division of floats always yields a float."
153         "Integer division of ratios and complex numbers proceeds using the relevant mathematical rules."
154     }
155 }
156 { $see-also "division-by-zero" } ;
157
158 HELP: mod
159 { $values { "x" integer } { "y" integer } { "z" integer } }
160 { $description
161     "Computes the remainder of dividing " { $snippet "x" } " by " { $snippet "y" } ", with the remainder being negative if " { $snippet "x" } " is negative."
162     { $list 
163         "Modulus of fixnums always yields a fixnum."
164         "Modulus of bignums always yields a bignum."            
165     }
166 }
167 { $see-also "division-by-zero" rem } ;
168
169 HELP: /mod
170 { $values { "x" integer } { "y" integer } { "z" integer } { "w" integer } }
171 { $description
172     "Computes the quotient " { $snippet "z" } " and remainder " { $snippet "w" } " of dividing " { $snippet "x" } " by " { $snippet "y" } ", with the remainder being negative if " { $snippet "x" } " is negative."
173     { $list 
174         "The quotient of two fixnums may overflow and yield a bignum; the remainder is always a fixnum"
175         "The quotient and remainder of two bignums is always a bignum."            
176     }
177 }
178 { $see-also "division-by-zero" } ;
179
180 HELP: bitand
181 { $values { "x" integer } { "y" integer } { "z" integer } }
182 { $description "Outputs a new integer where each bit is set if and only if the corresponding bit is set in both inputs." }
183 { $examples
184     { $example "USING: math prettyprint ;" "BIN: 101 BIN: 10 bitand .b" "0" }
185     { $example "USING: math prettyprint ;" "BIN: 110 BIN: 10 bitand .b" "10" }
186 }
187 { $notes "This word implements bitwise and, so applying it to booleans will throw an error. Boolean and is the " { $link and } " word." } ;
188
189 HELP: bitor
190 { $values { "x" integer } { "y" integer } { "z" integer } }
191 { $description "Outputs a new integer where each bit is set if and only if the corresponding bit is set in at least one of the inputs." }
192 { $examples
193     { $example "USING: math prettyprint ;" "BIN: 101 BIN: 10 bitor .b" "111" }
194     { $example "USING: math prettyprint ;" "BIN: 110 BIN: 10 bitor .b" "110" }
195 }
196 { $notes "This word implements bitwise inclusive or, so applying it to booleans will throw an error. Boolean inclusive or is the " { $link and } " word." } ;
197
198 HELP: bitxor
199 { $values { "x" integer } { "y" integer } { "z" integer } }
200 { $description "Outputs a new integer where each bit is set if and only if the corresponding bit is set in exactly one of the inputs." }
201 { $examples
202     { $example "USING: math prettyprint ;" "BIN: 101 BIN: 10 bitxor .b" "111" }
203     { $example "USING: math prettyprint ;" "BIN: 110 BIN: 10 bitxor .b" "100" }
204 }
205 { $notes "This word implements bitwise exclusive or, so applying it to booleans will throw an error. Boolean exclusive or is the " { $link xor } " word." } ;
206
207 HELP: shift
208 { $values { "x" integer } { "n" integer } { "y" integer } }
209 { $description "Shifts " { $snippet "x" } " to the left by " { $snippet "n" } " bits if " { $snippet "n" } " is positive, or " { $snippet "-n" } " bits to the right if " { $snippet "n" } " is negative. A left shift of a fixnum may overflow, yielding a bignum. A right shift may result in bits ``falling off'' the right hand side and being discarded." }
210 { $examples { $example "USING: math prettyprint ;" "BIN: 101 5 shift .b" "10100000" } { $example "USING: math prettyprint ;" "BIN: 11111 -2 shift .b" "111" } } ;
211
212 HELP: bitnot
213 { $values { "x" integer } { "y" integer } }
214 { $description "Computes the bitwise complement of the input; that is, each bit in the input number is flipped." }
215 { $notes "This word implements bitwise not, so applying it to booleans will throw an error. Boolean not is the " { $link not } " word."
216 $nl
217 "Due to the two's complement representation of signed integers, the following two lines are equivalent:" { $code "bitnot" "neg 1-" } } ;
218
219 HELP: bit?
220 { $values { "x" integer } { "n" integer } { "?" "a boolean" } }
221 { $description "Tests if the " { $snippet "n" } "th bit of " { $snippet "x" } " is set." }
222 { $examples { $example "USING: math prettyprint ;" "BIN: 101 2 bit? ." "t" } } ;
223
224 HELP: log2
225 { $values { "x" "a positive integer" } { "n" integer } }
226 { $description "Outputs the largest integer " { $snippet "n" } " such that " { $snippet "2^n" } " is less than " { $snippet "x" } "." }
227 { $errors "Throws an error if " { $snippet "x" } " is zero or negative." } ;
228
229 HELP: 1+
230 { $values { "x" number } { "y" number } }
231 { $description
232     "Increments a number by 1. The following two lines are equivalent, but the first is more efficient:"
233     { $code "1+" "1 +" }
234 } ;
235
236 HELP: 1-
237 { $values { "x" number } { "y" number } }
238 { $description
239     "Decrements a number by 1. The following two lines are equivalent, but the first is more efficient:"
240     { $code "1-" "1 -" }
241 } ;
242
243 HELP: sq
244 { $values { "x" number } { "y" number } }
245 { $description "Multiplies a number by itself." } ;
246
247 HELP: neg
248 { $values { "x" number } { "-x" number } }
249 { $description "Computes a number's additive inverse." } ;
250
251 HELP: recip
252 { $values { "x" number } { "y" number } }
253 { $description "Computes a number's multiplicative inverse." }
254 { $errors "Throws an error if " { $snippet "x" } " is the integer 0." } ;
255
256 HELP: rem
257 { $values { "x" integer } { "y" integer } { "z" integer } }
258 { $description
259     "Computes the remainder of dividing " { $snippet "x" } " by " { $snippet "y" } ", with the remainder always positive."
260     { $list 
261         "Modulus of fixnums always yields a fixnum."
262         "Modulus of bignums always yields a bignum."            
263     }
264 }
265 { $see-also "division-by-zero" mod } ;
266
267 HELP: sgn
268 { $values { "x" real } { "n" "-1, 0 or 1" } }
269 { $description
270     "Outputs one of the following:"
271     { $list
272         "-1 if " { $snippet "x" } " is negative"
273         "0 if " { $snippet "x" } " is equal to 0"
274         "1 if " { $snippet "x" } " is positive"
275     }
276 } ;
277
278 HELP: 2/
279 { $values { "x" integer } { "y" integer } }
280 { $description "Shifts " { $snippet "x" } " to the right by one bit." }
281 { $examples
282     { $example "USING: math prettyprint ;" "14 2/ ." "7" }
283     { $example "USING: math prettyprint ;" "17 2/ ." "8" }
284     { $example "USING: math prettyprint ;" "-17 2/ ." "-9" }
285 }
286 { $notes "This word is not equivalent to " { $snippet "2 /" } " or " { $snippet "2 /i" } "; the name is historic and originates from the Forth programming language." } ;
287
288 HELP: 2^
289 { $values { "n" "a positive integer" } { "2^n" "a positive integer" } }
290 { $description "Computes two to the power of " { $snippet "n" } ". This word will only give correct results if " { $snippet "n" } " is greater than zero; for the general case, use " { $snippet  "2 swap ^" } "." } ;
291
292 HELP: zero?
293 { $values { "x" number } { "?" "a boolean" } }
294 { $description "Tests if the number is equal to zero." } ;
295
296 HELP: times
297 { $values { "n" integer } { "quot" quotation } }
298 { $description "Calls the quotation " { $snippet "n" } " times." }
299 { $notes "If you need to pass the current index to the quotation, use " { $link each } "." } ;
300
301 HELP: fp-nan?
302 { $values { "x" real } { "?" "a boolean" } }
303 { $description "Tests if " { $snippet "x" } " is an IEEE Not-a-Number value. While " { $snippet "x" } " can be any real number, this word will only ever yield true if " { $snippet "x" } " is a " { $link float } "." } ;
304
305 HELP: real-part ( z -- x )
306 { $values { "z" number } { "x" real } }
307 { $description "Outputs the real part of a complex number. This acts as the identity on real numbers." } ;
308
309 HELP: imaginary-part ( z -- y )
310 { $values { "z" number } { "y" real } }
311 { $description "Outputs the imaginary part of a complex number. This outputs zero for real numbers." } ;
312
313 HELP: real
314 { $class-description "The class of real numbers, which is a disjoint union of rationals and floats." } ;
315
316 HELP: number
317 { $class-description "The class of numbers." } ;
318
319 HELP: next-power-of-2
320 { $values { "m" "a non-negative integer" } { "n" "an integer" } }
321 { $description "Outputs the smallest power of 2 greater than " { $snippet "m" } ". The output value is always at least 1." } ;
322
323 HELP: power-of-2?
324 { $values { "n" integer } { "?" "a boolean" } }
325 { $description "Tests if " { $snippet "n" } " is a power of 2." } ;
326
327 HELP: each-integer
328 { $values { "n" integer } { "quot" "a quotation with stack effect " { $snippet "( i -- )" } } }
329 { $description "Applies the quotation to each integer from 0 up to " { $snippet "n" } ", excluding " { $snippet "n" } "." }
330 { $notes "This word is used to implement " { $link each } "." } ;
331
332 HELP: all-integers?
333 { $values { "n" integer } { "quot" "a quotation with stack effect " { $snippet "( i -- ? )" } } { "?" "a boolean" } }
334 { $description "Applies the quotation to each integer from 0 up to " { $snippet "n" } ", excluding " { $snippet "n" } ". Iterationi stops when the quotation outputs " { $link f } " or the end is reached. If the quotation yields a false value for some integer, this word outputs " { $link f } ". Otherwise, this word outputs " { $link t } "." }
335 { $notes "This word is used to implement " { $link all? } "." } ;
336
337 HELP: find-integer
338 { $values { "n" integer } { "quot" "a quotation with stack effect " { $snippet "( i -- ? )" } } { "i" "an integer or " { $link f } } }
339 { $description "Applies the quotation to each integer from 0 up to " { $snippet "n" } ", excluding " { $snippet "n" } ". Iterationi stops when the quotation outputs a true value or the end is reached. If the quotation yields a true value for some integer, this word outputs that integer. Otherwise, this word outputs " { $link f } "." }
340 { $notes "This word is used to implement " { $link find } "." } ;
341
342 HELP: find-last-integer
343 { $values { "n" integer } { "quot" "a quotation with stack effect " { $snippet "( i -- ? )" } } { "i" "an integer or " { $link f } } }
344 { $description "Applies the quotation to each integer from " { $snippet "n" } " down to 0, inclusive. Iteration stops when the quotation outputs a true value or 0 is reached. If the quotation yields a true value for some integer, the word outputs that integer. Otherwise, the word outputs " { $link f } "." }
345 { $notes "This word is used to implement " { $link find-last } "." } ;