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