]> gitweb.factorcode.org Git - factor.git/blob - core/math/math-docs.factor
a863715d33257e9049c5fd06f3b77a7e404363d0
[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, but the first is more efficient:"
170     { $code "1+" "1 +" }
171 } ;
172
173 HELP: 1-
174 { $values { "x" number } { "y" number } }
175 { $description
176     "Decrements a number by 1. The following two lines are equivalent, but the first is more efficient:"
177     { $code "1-" "1 -" }
178 } ;
179
180 HELP: ?1+
181 { $description "If the input is not " { $link f } ", adds one. Otherwise, outputs a " { $snippet "0" } "." } ;
182
183 HELP: sq
184 { $values { "x" number } { "y" number } }
185 { $description "Multiplies a number by itself." } ;
186
187 HELP: neg
188 { $values { "x" number } { "-x" number } }
189 { $description "Computes a number's additive inverse." } ;
190
191 HELP: recip
192 { $values { "x" number } { "y" number } }
193 { $description "Computes a number's multiplicative inverse." }
194 { $errors "Throws an error if " { $snippet "x" } " is the integer 0." } ;
195
196 HELP: rem
197 { $values { "x" rational } { "y" rational } { "z" rational } }
198 { $description
199     "Computes the remainder of dividing " { $snippet "x" } " by " { $snippet "y" } ", with the remainder always positive."
200     { $list 
201         "Given fixnums, always yields a fixnum."
202         "Given bignums, always yields a bignum."
203         "Given rationals, always yields a rational."    
204     }
205 }
206 { $see-also "division-by-zero" mod } ;
207
208 HELP: sgn
209 { $values { "x" real } { "n" "-1, 0 or 1" } }
210 { $description
211     "Outputs one of the following:"
212     { $list
213         "-1 if " { $snippet "x" } " is negative"
214         "0 if " { $snippet "x" } " is equal to 0"
215         "1 if " { $snippet "x" } " is positive"
216     }
217 } ;
218
219 HELP: 2/
220 { $values { "x" integer } { "y" integer } }
221 { $description "Shifts " { $snippet "x" } " to the right by one bit." }
222 { $examples
223     { $example "USING: math prettyprint ;" "14 2/ ." "7" }
224     { $example "USING: math prettyprint ;" "17 2/ ." "8" }
225     { $example "USING: math prettyprint ;" "-17 2/ ." "-9" }
226 }
227 { $notes "This word is not equivalent to " { $snippet "2 /" } " or " { $snippet "2 /i" } "; the name is historic and originates from the Forth programming language." } ;
228
229 HELP: 2^
230 { $values { "n" "a positive integer" } { "2^n" "a positive integer" } }
231 { $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 ^" } "." } ;
232
233 HELP: zero?
234 { $values { "x" number } { "?" "a boolean" } }
235 { $description "Tests if the number is equal to zero." } ;
236
237 HELP: times
238 { $values { "n" integer } { "quot" quotation } }
239 { $description "Calls the quotation " { $snippet "n" } " times." }
240 { $notes "If you need to pass the current index to the quotation, use " { $link each } "." }
241 { $examples
242     { $example "USING: io math ;" "3 [ \"Hi\" print ] times" "Hi\nHi\nHi" }
243 } ;
244
245 HELP: fp-nan?
246 { $values { "x" real } { "?" "a boolean" } }
247 { $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 } "." } ;
248
249 HELP: fp-infinity?
250 { $values { "x" real } { "?" "a boolean" } }
251 { $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 } "." }
252 { $examples
253     { $example "USING: math prettyprint ;" "1/0. fp-infinity? ." "t" }
254     { $example "USING: io kernel math ;" "-1/0. [ fp-infinity? ] [ 0 < ] bi [ \"negative infinity\" print ] when" "negative infinity" }
255 } ;
256
257 { fp-nan? fp-infinity? } related-words
258
259 HELP: real-part
260 { $values { "z" number } { "x" real } }
261 { $description "Outputs the real part of a complex number. This acts as the identity on real numbers." }
262 { $examples { $example "USING: math prettyprint ; C{ 1 2 } real-part ." "1" } } ;
263
264 HELP: imaginary-part
265 { $values { "z" number } { "y" real } }
266 { $description "Outputs the imaginary part of a complex number. This outputs zero for real numbers." }
267 { $examples
268     { $example "USING: math prettyprint ; C{ 1 2 } imaginary-part ." "2" }
269     { $example "USING: math prettyprint ; 3 imaginary-part ." "0" }
270 } ;
271
272 HELP: real
273 { $class-description "The class of real numbers, which is a disjoint union of rationals and floats." } ;
274
275 HELP: number
276 { $class-description "The class of numbers." } ;
277
278 HELP: next-power-of-2
279 { $values { "m" "a non-negative integer" } { "n" "an integer" } }
280 { $description "Outputs the smallest power of 2 greater than " { $snippet "m" } ". The output value is always at least 1." } ;
281
282 HELP: power-of-2?
283 { $values { "n" integer } { "?" "a boolean" } }
284 { $description "Tests if " { $snippet "n" } " is a power of 2." } ;
285
286 HELP: each-integer
287 { $values { "n" integer } { "quot" "a quotation with stack effect " { $snippet "( i -- )" } } }
288 { $description "Applies the quotation to each integer from 0 up to " { $snippet "n" } ", excluding " { $snippet "n" } "." }
289 { $notes "This word is used to implement " { $link each } "." } ;
290
291 HELP: all-integers?
292 { $values { "n" integer } { "quot" "a quotation with stack effect " { $snippet "( i -- ? )" } } { "?" "a boolean" } }
293 { $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 } "." }
294 { $notes "This word is used to implement " { $link all? } "." } ;
295
296 HELP: find-integer
297 { $values { "n" integer } { "quot" "a quotation with stack effect " { $snippet "( i -- ? )" } } { "i" "an integer or " { $link f } } }
298 { $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 } "." }
299 { $notes "This word is used to implement " { $link find } "." } ;
300
301 HELP: find-last-integer
302 { $values { "n" integer } { "quot" "a quotation with stack effect " { $snippet "( i -- ? )" } } { "i" "an integer or " { $link f } } }
303 { $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 } "." }
304 { $notes "This word is used to implement " { $link find-last } "." } ;
305
306 ARTICLE: "division-by-zero" "Division by zero"
307 "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."
308 $nl
309 "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."
310 $nl
311 "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." ;
312
313 ARTICLE: "number-protocol" "Number protocol"
314 "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."
315 $nl
316 "Two examples where you should note the types of the inputs and outputs:"
317 { $example "3 >fixnum 6 >bignum * class ." "bignum" }
318 { $example "1/2 2.0 + ." "4.5" }
319 "The following usual operations are supported by all numbers."
320 { $subsection + }
321 { $subsection - }
322 { $subsection * }
323 { $subsection / }
324 "Non-commutative operations take operands from the stack in the natural order; " { $snippet "6 2 /" } " divides 6 by 2."
325 { $subsection "division-by-zero" }
326 "Real numbers (but not complex numbers) can be ordered:"
327 { $subsection < }
328 { $subsection <= }
329 { $subsection > }
330 { $subsection >= }
331 "Numbers can be compared for equality using " { $link = } ", or a less precise test which disregards types:"
332 { $subsection number= } ;
333
334 ARTICLE: "modular-arithmetic" "Modular arithmetic"
335 { $subsection mod }
336 { $subsection rem }
337 { $subsection /mod }
338 { $subsection /i }
339 { $see-also "integer-functions" } ;
340
341 ARTICLE: "bitwise-arithmetic" "Bitwise arithmetic"
342 "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" } "."
343 { $subsection bitand }
344 { $subsection bitor }
345 { $subsection bitxor }
346 { $subsection bitnot }
347 { $subsection shift }
348 { $subsection 2/ }
349 { $subsection 2^ }
350 { $subsection bit? }
351 { $see-also "conditionals" } ;
352
353 ARTICLE: "arithmetic" "Arithmetic"
354 "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."
355 $nl
356 "Math words are in the " { $vocab-link "math" } " vocabulary. Implementation details are in the " { $vocab-link "math.private" } " vocabulary."
357 { $subsection "number-protocol" }
358 { $subsection "modular-arithmetic" }
359 { $subsection "bitwise-arithmetic" }
360 { $see-also "integers" "rationals" "floats" "complex-numbers" } ;
361
362 ABOUT: "arithmetic"
363