]> gitweb.factorcode.org Git - factor.git/blob - core/math/math-docs.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / core / math / math-docs.factor
1 USING: help.markup help.syntax kernel sequences quotations
2 math.private byte-arrays io.binary ;
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 } { "?" boolean } }
16 { $description "Tests if " { $snippet "x" } " is less than " { $snippet "y" } "." } ;
17
18 HELP: <=
19 { $values { "x" real } { "y" real } { "?" boolean } }
20 { $description "Tests if " { $snippet "x" } " is less than or equal to " { $snippet "y" } "." } ;
21
22 HELP: >
23 { $values { "x" real } { "y" real } { "?" boolean } }
24 { $description "Tests if " { $snippet "x" } " is greater than " { $snippet "y" } "." } ;
25
26 HELP: >=
27 { $values { "x" real } { "y" real } { "?" 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 { $values { "x" { $maybe number } } { "y" number } }
184 { $description "If the input is not " { $link f } ", adds one. Otherwise, outputs a " { $snippet "0" } "." } ;
185
186 HELP: sq
187 { $values { "x" number } { "y" number } }
188 { $description "Multiplies a number by itself." } ;
189
190 HELP: neg
191 { $values { "x" number } { "-x" number } }
192 { $description "Computes a number's additive inverse." } ;
193
194 HELP: recip
195 { $values { "x" number } { "y" number } }
196 { $description "Computes a number's multiplicative inverse." }
197 { $errors "Throws an error if " { $snippet "x" } " is the integer 0." } ;
198
199 HELP: rem
200 { $values { "x" rational } { "y" rational } { "z" rational } }
201 { $description
202     "Computes the remainder of dividing " { $snippet "x" } " by " { $snippet "y" } ", with the remainder always positive."
203     { $list 
204         "Given fixnums, always yields a fixnum."
205         "Given bignums, always yields a bignum."
206         "Given rationals, always yields a rational."    
207     }
208 }
209 { $see-also "division-by-zero" mod } ;
210
211 HELP: sgn
212 { $values { "x" real } { "n" "-1, 0 or 1" } }
213 { $description
214     "Outputs one of the following:"
215     { $list
216         { "-1 if " { $snippet "x" } " is negative" }
217         { "0 if " { $snippet "x" } " is equal to 0" }
218         { "1 if " { $snippet "x" } " is positive" }
219     }
220 } ;
221
222 HELP: 2/
223 { $values { "x" integer } { "y" integer } }
224 { $description "Shifts " { $snippet "x" } " to the right by one bit." }
225 { $examples
226     { $example "USING: math prettyprint ;" "14 2/ ." "7" }
227     { $example "USING: math prettyprint ;" "17 2/ ." "8" }
228     { $example "USING: math prettyprint ;" "-17 2/ ." "-9" }
229 }
230 { $notes "This word is not equivalent to " { $snippet "2 /" } " or " { $snippet "2 /i" } "; the name is historic and originates from the Forth programming language." } ;
231
232 HELP: 2^
233 { $values { "n" "a positive integer" } { "2^n" "a positive integer" } }
234 { $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 ^" } "." } ;
235
236 HELP: zero?
237 { $values { "x" number } { "?" "a boolean" } }
238 { $description "Tests if the number is equal to zero." } ;
239
240 HELP: times
241 { $values { "n" integer } { "quot" quotation } }
242 { $description "Calls the quotation " { $snippet "n" } " times." }
243 { $notes "If you need to pass the current index to the quotation, use " { $link each } "." }
244 { $examples
245     { $example "USING: io math ;" "3 [ \"Hi\" print ] times" "Hi\nHi\nHi" }
246 } ;
247
248 HELP: fp-bitwise=
249 { $values
250     { "x" float } { "y" float }
251     { "?" boolean }
252 }
253 { $description "Compares two floating point numbers for bit equality." } ;
254
255 HELP: fp-special?
256 { $values { "x" real } { "?" "a boolean" } }
257 { $description "Tests if " { $snippet "x" } " is an IEEE special value (Not-a-Number or Infinity). While " { $snippet "x" } " can be any real number, this word will only ever yield true if " { $snippet "x" } " is a " { $link float } "." } ;
258
259 HELP: fp-nan?
260 { $values { "x" real } { "?" "a boolean" } }
261 { $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 } "." } ;
262
263 HELP: fp-qnan?
264 { $values { "x" real } { "?" "a boolean" } }
265 { $description "Tests if " { $snippet "x" } " is an IEEE Quiet 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 } "." } ;
266
267 HELP: fp-snan?
268 { $values { "x" real } { "?" "a boolean" } }
269 { $description "Tests if " { $snippet "x" } " is an IEEE Signaling 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 } "." } ;
270
271 HELP: fp-infinity?
272 { $values { "x" real } { "?" "a boolean" } }
273 { $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 } "." }
274 { $examples
275     { $example "USING: math prettyprint ;" "1/0. fp-infinity? ." "t" }
276     { $example "USING: io kernel math ;" "-1/0. [ fp-infinity? ] [ 0 < ] bi and [ \"negative infinity\" print ] when" "negative infinity" }
277 } ;
278
279 HELP: fp-nan-payload
280 { $values { "x" real } { "bits" integer } }
281 { $description "If " { $snippet "x" } " is an IEEE Not-a-Number value, returns the payload encoded in the value. Returns " { $link f } " if " { $snippet "x" } " is not a " { $link float } "." } ;
282
283 HELP: <fp-nan>
284 { $values { "payload" integer } { "nan" float } }
285 { $description "Constructs an IEEE Not-a-Number value with a payload of " { $snippet "payload" } "." }
286 { $notes "A " { $snippet "payload" } " of " { $snippet "0" } " will construct an Infinity value." } ;
287
288 { fp-special? fp-nan? fp-qnan? fp-snan? fp-infinity? fp-nan-payload <fp-nan> } related-words
289
290 HELP: next-float
291 { $values { "m" float } { "n" float } }
292 { $description "Returns the least representable " { $link float } " value greater than " { $snippet "m" } ", or in the case of " { $snippet "-0.0" } ", returns " { $snippet "+0.0" } "." } ;
293
294 HELP: prev-float
295 { $values { "m" float } { "n" float } }
296 { $description "Returns the greatest representable " { $link float } " value less than " { $snippet "m" } ", or in the case of " { $snippet "+0.0" } ", returns " { $snippet "-0.0" } "." } ;
297
298 { next-float prev-float } related-words
299
300 HELP: real-part
301 { $values { "z" number } { "x" real } }
302 { $description "Outputs the real part of a complex number. This acts as the identity on real numbers." }
303 { $examples { $example "USING: math prettyprint ; C{ 1 2 } real-part ." "1" } } ;
304
305 HELP: imaginary-part
306 { $values { "z" number } { "y" real } }
307 { $description "Outputs the imaginary part of a complex number. This outputs zero for real numbers." }
308 { $examples
309     { $example "USING: math prettyprint ; C{ 1 2 } imaginary-part ." "2" }
310     { $example "USING: math prettyprint ; 3 imaginary-part ." "0" }
311 } ;
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" { $quotation "( 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" { $quotation "( i -- ? )" } } { "?" "a boolean" } }
334 { $description "Applies the quotation to each integer from 0 up to " { $snippet "n" } ", excluding " { $snippet "n" } ". Iteration 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" { $quotation "( 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" { $quotation "( 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 } "." } ;
346
347 HELP: byte-array>bignum
348 { $values { "x" byte-array } { "y" bignum } }
349 { $description "Converts a byte-array, interpreted as little-endian, into a bignum integer. User code should call " { $link le> } " or " { $link be> } " instead." } ;
350
351 ARTICLE: "division-by-zero" "Division by zero"
352 "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."
353 $nl
354 "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."
355 $nl
356 "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." ;
357
358 ARTICLE: "number-protocol" "Number protocol"
359 "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."
360 $nl
361 "Two examples where you should note the types of the inputs and outputs:"
362 { $example "USE: classes" "3 >fixnum 6 >bignum * class ." "bignum" }
363 { $example "1/2 2.0 + ." "2.5" }
364 "The following usual operations are supported by all numbers."
365 { $subsection + }
366 { $subsection - }
367 { $subsection * }
368 { $subsection / }
369 "Non-commutative operations take operands from the stack in the natural order; " { $snippet "6 2 /" } " divides 6 by 2."
370 { $subsection "division-by-zero" }
371 "Real numbers (but not complex numbers) can be ordered:"
372 { $subsection < }
373 { $subsection <= }
374 { $subsection > }
375 { $subsection >= }
376 "Numbers can be compared for equality using " { $link = } ", or a less precise test which disregards types:"
377 { $subsection number= } ;
378
379 ARTICLE: "modular-arithmetic" "Modular arithmetic"
380 { $subsection mod }
381 { $subsection rem }
382 { $subsection /mod }
383 { $subsection /i }
384 { $see-also "integer-functions" } ;
385
386 ARTICLE: "bitwise-arithmetic" "Bitwise arithmetic"
387 "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" } "."
388 { $subsection bitand }
389 { $subsection bitor }
390 { $subsection bitxor }
391 { $subsection bitnot }
392 { $subsection shift }
393 { $subsection 2/ }
394 { $subsection 2^ }
395 { $subsection bit? }
396 { $subsection "math.bitwise" }
397 { $subsection "math.bits" }
398 { $see-also "booleans" } ;
399
400 ARTICLE: "arithmetic" "Arithmetic"
401 "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."
402 $nl
403 "Math words are in the " { $vocab-link "math" } " vocabulary. Implementation details are in the " { $vocab-link "math.private" } " vocabulary."
404 { $subsection "number-protocol" }
405 { $subsection "modular-arithmetic" }
406 { $subsection "bitwise-arithmetic" }
407 { $see-also "integers" "rationals" "floats" "complex-numbers" } ;
408
409 ABOUT: "arithmetic"
410