]> gitweb.factorcode.org Git - factor.git/blob - core/math/math-docs.factor
core: update docs for new words
[factor.git] / core / math / math-docs.factor
1 USING: help.markup help.syntax kernel quotations sequences ;
2 IN: math
3
4 HELP: number=
5 { $values { "x" number } { "y" number } { "?" boolean } }
6 { $description "Tests if two numbers have the same numeric value." }
7 { $notes "This word differs from " { $link = } " in that it disregards differences in type when comparing numbers."
8 $nl
9 "This word performs an unordered comparison on floating point numbers. See " { $link "math.floats.compare" } " for an explanation." }
10 { $examples
11     { $example "USING: math prettyprint ;" "3.0 3 number= ." "t" }
12     { $example "USING: kernel math prettyprint ;" "3.0 3 = ." "f" }
13 } ;
14
15 HELP: <
16 { $values { "x" real } { "y" real } { "?" boolean } }
17 { $description "Tests if " { $snippet "x" } " is less than " { $snippet "y" } "." }
18 { $notes "This word performs an ordered comparison on floating point numbers. See " { $link "math.floats.compare" } " for an explanation." } ;
19
20 HELP: <=
21 { $values { "x" real } { "y" real } { "?" boolean } }
22 { $description "Tests if " { $snippet "x" } " is less than or equal to " { $snippet "y" } "." }
23 { $notes "This word performs an ordered comparison on floating point numbers. See " { $link "math.floats.compare" } " for an explanation." } ;
24
25 HELP: >
26 { $values { "x" real } { "y" real } { "?" boolean } }
27 { $description "Tests if " { $snippet "x" } " is greater than " { $snippet "y" } "." }
28 { $notes "This word performs an ordered comparison on floating point numbers. See " { $link "math.floats.compare" } " for an explanation." } ;
29
30 HELP: >=
31 { $values { "x" real } { "y" real } { "?" boolean } }
32 { $description "Tests if " { $snippet "x" } " is greater than or equal to " { $snippet "y" } "." }
33 { $notes "This word performs an ordered comparison on floating point numbers. See " { $link "math.floats.compare" } " for an explanation." } ;
34
35 HELP: unordered?
36 { $values { "x" real } { "y" real } { "?" boolean } }
37 { $description "Tests if " { $snippet "x" } " is unordered with respect to " { $snippet "y" } ". This can only occur if one or both values is a floating-point Not-a-Number value." } ;
38
39 HELP: u<
40 { $values { "x" real } { "y" real } { "?" boolean } }
41 { $description "Tests if " { $snippet "x" } " is less than " { $snippet "y" } "." }
42 { $notes "This word performs an unordered comparison on floating point numbers. On rational numbers it is equivalent to " { $link < } ". See " { $link "math.floats.compare" } " for an explanation." } ;
43
44 HELP: u<=
45 { $values { "x" real } { "y" real } { "?" boolean } }
46 { $description "Tests if " { $snippet "x" } " is less than or equal to " { $snippet "y" } "." }
47 { $notes "This word performs an unordered comparison on floating point numbers. On rational numbers it is equivalent to " { $link <= } ". See " { $link "math.floats.compare" } " for an explanation." } ;
48
49 HELP: u>
50 { $values { "x" real } { "y" real } { "?" boolean } }
51 { $description "Tests if " { $snippet "x" } " is greater than " { $snippet "y" } "." }
52 { $notes "This word performs an unordered comparison on floating point numbers. On rational numbers it is equivalent to " { $link > } ". See " { $link "math.floats.compare" } " for an explanation." } ;
53
54 HELP: u>=
55 { $values { "x" real } { "y" real } { "?" boolean } }
56 { $description "Tests if " { $snippet "x" } " is greater than or equal to " { $snippet "y" } "." }
57 { $notes "This word performs an unordered comparison on floating point numbers. On rational numbers it is equivalent to " { $link >= } ". See " { $link "math.floats.compare" } " for an explanation." } ;
58
59 HELP: +
60 { $values { "x" number } { "y" number } { "z" number } }
61 { $description
62     "Adds two numbers."
63     { $list
64         "Addition of fixnums may overflow and convert the result to a bignum."
65         "Addition of bignums always yields a bignum."
66         "Addition of floats always yields a float."
67         "Addition of ratios and complex numbers proceeds using the relevant mathematical rules."
68     }
69 } ;
70
71 HELP: -
72 { $values { "x" number } { "y" number } { "z" number } }
73 { $description
74     "Subtracts " { $snippet "y" } " from " { $snippet "x" } "."
75     { $list
76         "Subtraction of fixnums may overflow and convert the result to a bignum."
77         "Subtraction of bignums always yields a bignum."
78         "Subtraction of floats always yields a float."
79         "Subtraction of ratios and complex numbers proceeds using the relevant mathematical rules."
80     }
81 } ;
82
83 HELP: *
84 { $values { "x" number } { "y" number } { "z" number } }
85 { $description
86     "Multiplies two numbers."
87     { $list
88         "Multiplication of fixnums may overflow and convert the result to a bignum."
89         "Multiplication of bignums always yields a bignum."
90         "Multiplication of floats always yields a float."
91         "Multiplication 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     "Divides " { $snippet "x" } " by " { $snippet "y" } ", retaining as much precision as possible."
99     { $list
100         "Division of fixnums may yield a ratio, or overflow and yield a bignum."
101         "Division of bignums may yield a ratio."
102         "Division of floats always yields a float."
103         "Division of ratios and complex numbers proceeds using the relevant mathematical rules."
104     }
105 }
106 { $see-also "division-by-zero" } ;
107
108 HELP: /i
109 { $values { "x" real } { "y" real } { "z" integer } }
110 { $description
111     "Divides " { $snippet "x" } " by " { $snippet "y" } ", truncating the result to an integer."
112 }
113 { $see-also "division-by-zero" } ;
114
115 HELP: /f
116 { $values { "x" real } { "y" real } { "z" float } }
117 { $description
118     "Divides " { $snippet "x" } " by " { $snippet "y" } ", representing the result as a floating point number."
119 }
120 { $see-also "division-by-zero" } ;
121
122 HELP: mod
123 { $values { "x" rational } { "y" rational } { "z" rational } }
124 { $description
125     "Computes the remainder of dividing " { $snippet "x" } " by " { $snippet "y" } ", with the remainder being negative if " { $snippet "x" } " is negative."
126     { $list
127         "Modulus of fixnums always yields a fixnum."
128         "Modulus of bignums always yields a bignum."
129         { "Modulus of rationals always yields a rational. In this case, the remainder is computed using the formula " { $snippet "x - (x mod y) * y" } "." }
130     }
131 }
132 { $see-also "division-by-zero" rem } ;
133
134 HELP: /mod
135 { $values { "x" real } { "y" real } { "z" integer } { "w" real } }
136 { $description
137     "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."
138     { $list
139         "The quotient of two fixnums may overflow and yield a bignum; the remainder is always a fixnum"
140         "The quotient and remainder of two bignums is always a bignum."
141     }
142 }
143 { $examples
144     { $example "USING: kernel math prettyprint ;" "5 3 /mod [ . ] bi@" "1\n2" }
145     { $example "USING: kernel math prettyprint ;" "5/2 1/3 /mod [ . ] bi@" "7\n1/6" }
146 }
147 { $see-also "division-by-zero" } ;
148
149 HELP: bitand
150 { $values { "x" integer } { "y" integer } { "z" integer } }
151 { $description "Outputs a new integer where each bit is set if and only if the corresponding bit is set in both inputs." }
152 { $examples
153     { $example "USING: math prettyprint ;" "0b101 0b10 bitand .b" "0b0" }
154     { $example "USING: math prettyprint ;" "0b110 0b10 bitand .b" "0b10" }
155 }
156 { $notes "This word implements bitwise and, so applying it to booleans will throw an error. Boolean and is the " { $link and } " word." } ;
157
158 HELP: bitor
159 { $values { "x" integer } { "y" integer } { "z" integer } }
160 { $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." }
161 { $examples
162     { $example "USING: math prettyprint ;" "0b101 0b10 bitor .b" "0b111" }
163     { $example "USING: math prettyprint ;" "0b110 0b10 bitor .b" "0b110" }
164 }
165 { $notes "This word implements bitwise inclusive or, so applying it to booleans will throw an error. Boolean inclusive or is the " { $link and } " word." } ;
166
167 HELP: bitxor
168 { $values { "x" integer } { "y" integer } { "z" integer } }
169 { $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." }
170 { $examples
171     { $example "USING: math prettyprint ;" "0b101 0b10 bitxor .b" "0b111" }
172     { $example "USING: math prettyprint ;" "0b110 0b10 bitxor .b" "0b100" }
173 }
174 { $notes "This word implements bitwise exclusive or, so applying it to booleans will throw an error. Boolean exclusive or is the " { $link xor } " word." } ;
175
176 HELP: shift
177 { $values { "x" integer } { "n" integer } { "y" integer } }
178 { $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." }
179 { $examples { $example "USING: math prettyprint ;" "0b101 5 shift .b" "0b10100000" } { $example "USING: math prettyprint ;" "0b11111 -2 shift .b" "0b111" } } ;
180
181 HELP: bitnot
182 { $values { "x" integer } { "y" integer } }
183 { $description "Computes the bitwise complement of the input; that is, each bit in the input number is flipped." }
184 { $notes "This word implements bitwise not, so applying it to booleans will throw an error. Boolean not is the " { $link not } " word."
185 $nl
186 "Due to the two's complement representation of signed integers, the following two lines are equivalent:" { $code "bitnot" "neg 1 -" } } ;
187
188 HELP: bit?
189 { $values { "x" integer } { "n" integer } { "?" boolean } }
190 { $description "Tests if the " { $snippet "n" } "th bit of " { $snippet "x" } " is set." }
191 { $examples { $example "USING: math prettyprint ;" "0b101 2 bit? ." "t" } } ;
192
193 HELP: log2
194 { $values { "x" "a positive integer" } { "n" integer } }
195 { $description "Outputs the largest integer " { $snippet "n" } " such that " { $snippet "2^n" } " is less than or equal to " { $snippet "x" } "." }
196 { $errors "Throws an error if " { $snippet "x" } " is zero or negative." } ;
197
198 HELP: ?1+
199 { $values { "x" { $maybe number } } { "y" number } }
200 { $description "If the input is not " { $link f } ", adds one. Otherwise, outputs a " { $snippet "0" } "." } ;
201
202 HELP: sq
203 { $values { "x" number } { "y" number } }
204 { $description "Multiplies a number by itself." } ;
205
206 HELP: neg
207 { $values { "x" number } { "-x" number } }
208 { $description "Computes a number's additive inverse." } ;
209
210 HELP: recip
211 { $values { "x" number } { "y" number } }
212 { $description "Computes a number's multiplicative inverse." }
213 { $errors "Throws an error if " { $snippet "x" } " is the integer 0." } ;
214
215 HELP: rem
216 { $values { "x" rational } { "y" rational } { "z" rational } }
217 { $description
218     "Computes the remainder of dividing " { $snippet "x" } " by " { $snippet "y" } ", with the remainder always positive or zero."
219     { $list
220         "Given fixnums, always yields a fixnum."
221         "Given bignums, always yields a bignum."
222         "Given rationals, always yields a rational."
223     }
224 }
225 { $see-also "division-by-zero" mod } ;
226
227 HELP: sgn
228 { $values { "x" real } { "n" "-1, 0 or 1" } }
229 { $description
230     "Outputs one of the following:"
231     { $list
232         { "-1 if " { $snippet "x" } " is negative" }
233         { "0 if " { $snippet "x" } " is equal to 0" }
234         { "1 if " { $snippet "x" } " is positive" }
235     }
236 } ;
237
238 HELP: rect>
239 { $values { "x" real } { "y" real } { "z" number } }
240 { $description "Creates a complex number from real and imaginary components. If " { $snippet "z" } " is an integer zero, this will simply output " { $snippet "x" } "." } ;
241
242 HELP: >rect
243 { $values { "z" number } { "x" real } { "y" real } }
244 { $description "Extracts the real and imaginary components of a complex number." } ;
245
246 HELP: gcd
247 { $values { "x" integer } { "y" integer } { "a" integer } { "d" integer } }
248 { $description "Computes the positive greatest common divisor " { $snippet "d" } " of " { $snippet "x" } " and " { $snippet "y" } ", and another value " { $snippet "a" } " satisfying:" { $code "a*y = d mod x" } }
249 { $notes "If " { $snippet "d" } " is 1, then " { $snippet "a" } " is the inverse of " { $snippet "y" } " modulo " { $snippet "x" } "." } ;
250
251 HELP: 2/
252 { $values { "x" integer } { "y" integer } }
253 { $description "Shifts " { $snippet "x" } " to the right by one bit." }
254 { $examples
255     { $example "USING: math prettyprint ;" "14 2/ ." "7" }
256     { $example "USING: math prettyprint ;" "17 2/ ." "8" }
257     { $example "USING: math prettyprint ;" "-17 2/ ." "-9" }
258 }
259 { $notes "This word is not equivalent to " { $snippet "2 /" } " or " { $snippet "2 /i" } "; the name is historic and originates from the Forth programming language." } ;
260
261 HELP: 2^
262 { $values { "n" "a positive integer" } { "2^n" "a positive integer" } }
263 { $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 ^" } "." } ;
264
265 HELP: zero?
266 { $values { "x" number } { "?" boolean } }
267 { $description "Tests if the number is equal to zero." } ;
268
269 HELP: if-zero
270 { $values { "n" number } { "quot1" quotation } { "quot2" quotation } }
271 { $description "Makes an implicit check if the number is zero. A zero is dropped and " { $snippet "quot1" } " is called. Otherwise, if the number is not zero, " { $snippet "quot2" } " is called on it." }
272 { $example
273     "USING: kernel math prettyprint sequences ;"
274     "3 [ \"zero\" ] [ sq ] if-zero ."
275     "9"
276 } ;
277
278 HELP: when-zero
279 { $values
280      { "n" number } { "quot" "the first quotation of an " { $link if-zero } } { "x" object } }
281 { $description "Makes an implicit check if the number is zero. A zero is dropped and the " { $snippet "quot" } " is called." }
282 { $examples "This word is equivalent to " { $link if-zero } " with an empty second quotation:"
283     { $example
284     "USING: math prettyprint ;"
285     "0 [ 4 ] [ ] if-zero ."
286     "4"
287     }
288     { $example
289     "USING: math prettyprint ;"
290     "0 [ 4 ] when-zero ."
291     "4"
292     }
293 } ;
294
295 HELP: unless-zero
296 { $values
297      { "n" number } { "quot" "the second quotation of an " { $link if-zero } } }
298 { $description "Makes an implicit check if the number is zero. A zero is dropped. Otherwise, the " { $snippet "quot" } " is called on the number." }
299 { $examples "This word is equivalent to " { $link if-zero } " with an empty first quotation:"
300     { $example
301     "USING: sequences math prettyprint ;"
302     "3 [ ] [ sq . ] if-zero"
303     "9"
304     }
305     { $example
306     "USING: sequences math prettyprint ;"
307     "3 [ sq . ] unless-zero"
308     "9"
309     }
310 } ;
311
312 HELP: until-zero
313 { $values
314      { "n" number } { "quot" { $quotation ( ... x -- ... y ) } } }
315 { $description "Makes a check if the number is zero, and repeatedly calls " { $snippet "quot" } " until the value on the stack is zero." }
316 { $examples
317     { $example
318     "USING: kernel math prettyprint ;"
319     "15 [ dup . 2/ ] until-zero"
320     "15\n7\n3\n1"
321     }
322 } ;
323
324 { if-zero when-zero unless-zero until-zero } related-words
325
326 HELP: times
327 { $values { "n" integer } { "quot" quotation } }
328 { $description "Calls the quotation " { $snippet "n" } " times." }
329 { $notes "If you need to pass the current index to the quotation, use " { $link each-integer } "." }
330 { $examples
331     { $example "USING: io math ;" "3 [ \"Hi\" print ] times" "Hi\nHi\nHi" }
332 } ;
333
334 HELP: fp-bitwise=
335 { $values
336     { "x" float } { "y" float }
337     { "?" boolean }
338 }
339 { $description "Compares two floating point numbers for bit equality." }
340 { $notes "Unlike " { $link = } " or " { $link number= } ", this word will consider NaNs with equal payloads to be equal, and positive zero and negative zero to be not equal." }
341 { $examples
342     "Not-a-number equality:"
343     { $example
344         "USING: kernel math prettyprint ;"
345         "0.0 0.0 / dup number= ."
346         "f"
347     }
348     { $example
349         "USING: kernel math prettyprint ;"
350         "0.0 0.0 / dup fp-bitwise= ."
351         "t"
352     }
353     "Signed zero equality:"
354     { $example
355         "USING: math prettyprint ;"
356         "-0.0 0.0 fp-bitwise= ."
357         "f"
358     }
359     { $example
360         "USING: math prettyprint ;"
361         "-0.0 0.0 number= ."
362         "t"
363     }
364 } ;
365
366 HELP: fp-special?
367 { $values { "x" real } { "?" boolean } }
368 { $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 } "." } ;
369
370 HELP: fp-nan?
371 { $values { "x" real } { "?" boolean } }
372 { $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 } "." } ;
373
374 HELP: fp-qnan?
375 { $values { "x" real } { "?" boolean } }
376 { $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 } "." } ;
377
378 HELP: fp-snan?
379 { $values { "x" real } { "?" boolean } }
380 { $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 } "." } ;
381
382 HELP: fp-infinity?
383 { $values { "x" real } { "?" boolean } }
384 { $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 } "." }
385 { $examples
386     { $example "USING: math prettyprint ;" "1/0. fp-infinity? ." "t" }
387     { $example "USING: io kernel math ;" "-1/0. [ fp-infinity? ] [ 0 < ] bi and [ \"negative infinity\" print ] when" "negative infinity" }
388 } ;
389
390 HELP: fp-sign
391 { $values { "x" float } { "?" boolean } }
392 { $description "Outputs the sign bit of " { $snippet "x" } ". For ordered non-zero values, this is equivalent to calling " { $snippet "0 <" } ". For zero values, this outputs the zero's sign bit." } ;
393
394 HELP: fp-nan-payload
395 { $values { "x" real } { "bits" integer } }
396 { $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 } "." } ;
397
398 HELP: <fp-nan>
399 { $values { "payload" integer } { "nan" float } }
400 { $description "Constructs an IEEE Not-a-Number value with a payload of " { $snippet "payload" } "." }
401 { $notes "A " { $snippet "payload" } " of " { $snippet "0" } " will construct an Infinity value." } ;
402
403 { fp-special? fp-nan? fp-qnan? fp-snan? fp-infinity? fp-nan-payload <fp-nan> } related-words
404
405 HELP: next-float
406 { $values { "m" float } { "n" float } }
407 { $description "Returns the least representable " { $link float } " value greater than " { $snippet "m" } ", or in the case of " { $snippet "-0.0" } ", returns " { $snippet "+0.0" } "." } ;
408
409 HELP: prev-float
410 { $values { "m" float } { "n" float } }
411 { $description "Returns the greatest representable " { $link float } " value less than " { $snippet "m" } ", or in the case of " { $snippet "+0.0" } ", returns " { $snippet "-0.0" } "." } ;
412
413 { next-float prev-float } related-words
414
415 HELP: real-part
416 { $values { "z" number } { "x" real } }
417 { $description "Outputs the real part of a complex number. This acts as the identity on real numbers." }
418 { $examples { $example "USING: math prettyprint ;" "C{ 1 2 } real-part ." "1" } } ;
419
420 HELP: imaginary-part
421 { $values { "z" number } { "y" real } }
422 { $description "Outputs the imaginary part of a complex number. This outputs zero for real numbers." }
423 { $examples
424     { $example "USING: math prettyprint ;" "C{ 1 2 } imaginary-part ." "2" }
425     { $example "USING: math prettyprint ;" "3 imaginary-part ." "0" }
426 } ;
427
428 HELP: real
429 { $class-description "The class of real numbers, which is a disjoint union of rationals and floats." } ;
430
431 HELP: number
432 { $class-description "The class of numbers." } ;
433
434 HELP: next-power-of-2
435 { $values { "m" "a non-negative integer" } { "n" integer } }
436 { $description "Outputs the smallest power of 2 greater than or equal to " { $snippet "m" } ". The output value is always at least 2." } ;
437
438 HELP: power-of-2?
439 { $values { "n" integer } { "?" boolean } }
440 { $description "Tests if " { $snippet "n" } " is a power of 2." } ;
441
442 HELP: each-integer
443 { $values { "n" integer } { "quot" { $quotation ( ... i -- ... ) } } }
444 { $description "Applies the quotation to each integer from 0 up to " { $snippet "n" } ", excluding " { $snippet "n" } "." }
445 { $notes "This word is used to implement " { $link each } "." } ;
446
447 HELP: all-integers?
448 { $values { "n" integer } { "quot" { $quotation ( ... i -- ... ? ) } } { "?" boolean } }
449 { $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 } "." }
450 { $notes "This word is used to implement " { $link all? } "." } ;
451
452 HELP: find-integer-from
453 { $values { "i" integer } { "n" integer } { "quot" { $quotation ( ... i -- ... ? ) } } { "i/f" { $maybe integer } } }
454 { $description "Applies the quotation to each integer from " { $snippet "i" } " up to " { $snippet "n" } ", excluding " { $snippet "n" } ". Iteration 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 } "." }
455 { $notes "This word is used to implement " { $link find-integer } " and " { $link find } "." } ;
456
457 HELP: find-integer
458 { $values { "n" integer } { "quot" { $quotation ( ... i -- ... ? ) } } { "i/f" { $maybe integer } } }
459 { $description "Applies the quotation to each integer from 0 up to " { $snippet "n" } ", excluding " { $snippet "n" } ". Iteration 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 } "." }
460 { $notes "This word is used to implement " { $link find } "." } ;
461
462 HELP: find-last-integer
463 { $values { "n" integer } { "quot" { $quotation ( ... i -- ... ? ) } } { "i/f" { $maybe integer } } }
464 { $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 } "." }
465 { $notes "This word is used to implement " { $link find-last } "." } ;
466
467 ARTICLE: "division-by-zero" "Division by zero"
468 "Behavior of division operations when a denominator of zero is used depends on the data types in question, as well as the platform being used."
469 $nl
470 "Floating point division only throws an error if the appropriate traps are enabled in the floating point environment. If traps are disabled, a Not-a-number value or an infinity is output, depending on whether the numerator is zero or non-zero."
471 $nl
472 "Floating point traps are disabled by default and the " { $vocab-link "math.floats.env" } " vocabulary provides words to enable them. Floating point division is performed by " { $link / } ", " { $link /f } " or " { $link mod } " if at least one of the two inputs is a float. Floating point division is always performed by " { $link /f } "."
473 $nl
474 "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."
475 $nl
476 "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." ;
477
478 ARTICLE: "number-protocol" "Number protocol"
479 "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."
480 $nl
481 "Two examples where you should note the types of the inputs and outputs:"
482 { $example "USE: classes" "3 >fixnum 6 >bignum * class-of ." "bignum" }
483 { $example "1/2 2.0 + ." "2.5" }
484 "The following usual operations are supported by all numbers."
485 { $subsections
486     +
487     -
488     *
489     /
490 }
491 "Non-commutative operations take operands from the stack in the natural order; " { $snippet "6 2 /" } " divides 6 by 2."
492 { $subsections "division-by-zero" }
493 "Real numbers (but not complex numbers) can be ordered:"
494 { $subsections
495     <
496     <=
497     >
498     >=
499 }
500 "Numbers can be compared for equality using " { $link = } ", or a less precise test which disregards types:"
501 { $subsections number= }
502 { $see-also "math.floats.compare" } ;
503
504 ARTICLE: "modular-arithmetic" "Modular arithmetic"
505 { $subsections
506     mod
507     rem
508     /mod
509     /i
510 }
511 { $see-also "integer-functions" } ;
512
513 ARTICLE: "bitwise-arithmetic" "Bitwise arithmetic"
514 "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" } "."
515 { $subsections
516     bitand
517     bitor
518     bitxor
519     bitnot
520     shift
521     2/
522     2^
523     bit?
524 }
525 "Advanced topics:"
526 { $subsections
527     "math.bitwise"
528     "math.bits"
529 }
530 { $see-also "booleans" } ;
531
532 ARTICLE: "arithmetic" "Arithmetic"
533 "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."
534 $nl
535 "Math words are in the " { $vocab-link "math" } " vocabulary. Implementation details are in the " { $vocab-link "math.private" } " vocabulary."
536 { $subsections
537     "number-protocol"
538     "modular-arithmetic"
539     "bitwise-arithmetic"
540 }
541 { $see-also "integers" "rationals" "floats" "complex-numbers" } ;
542
543 ABOUT: "arithmetic"