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