]> gitweb.factorcode.org Git - factor.git/blob - core/math/math-docs.factor
138d557c0fd51c0b384ccf8a1a88fbcff85ba681
[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" real } { "y" real } { "z" integer } { "w" real } }
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 { $examples
145     { $example "USING: kernel math prettyprint ;" "5 3 /mod [ . ] bi@" "1\n2" }
146     { $example "USING: kernel math prettyprint ;" "5/2 1/3 /mod [ . ] bi@" "7\n1/6" }
147 }
148 { $see-also "division-by-zero" } ;
149
150 HELP: bitand
151 { $values { "x" integer } { "y" integer } { "z" integer } }
152 { $description "Outputs a new integer where each bit is set if and only if the corresponding bit is set in both inputs." }
153 { $examples
154     { $example "USING: math prettyprint ;" "0b101 0b10 bitand .b" "0" }
155     { $example "USING: math prettyprint ;" "0b110 0b10 bitand .b" "10" }
156 }
157 { $notes "This word implements bitwise and, so applying it to booleans will throw an error. Boolean and is the " { $link and } " word." } ;
158
159 HELP: bitor
160 { $values { "x" integer } { "y" integer } { "z" integer } }
161 { $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." }
162 { $examples
163     { $example "USING: math prettyprint ;" "0b101 0b10 bitor .b" "111" }
164     { $example "USING: math prettyprint ;" "0b110 0b10 bitor .b" "110" }
165 }
166 { $notes "This word implements bitwise inclusive or, so applying it to booleans will throw an error. Boolean inclusive or is the " { $link and } " word." } ;
167
168 HELP: bitxor
169 { $values { "x" integer } { "y" integer } { "z" integer } }
170 { $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." }
171 { $examples
172     { $example "USING: math prettyprint ;" "0b101 0b10 bitxor .b" "111" }
173     { $example "USING: math prettyprint ;" "0b110 0b10 bitxor .b" "100" }
174 }
175 { $notes "This word implements bitwise exclusive or, so applying it to booleans will throw an error. Boolean exclusive or is the " { $link xor } " word." } ;
176
177 HELP: shift
178 { $values { "x" integer } { "n" integer } { "y" integer } }
179 { $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." }
180 { $examples { $example "USING: math prettyprint ;" "0b101 5 shift .b" "10100000" } { $example "USING: math prettyprint ;" "0b11111 -2 shift .b" "111" } } ;
181
182 HELP: bitnot
183 { $values { "x" integer } { "y" integer } }
184 { $description "Computes the bitwise complement of the input; that is, each bit in the input number is flipped." }
185 { $notes "This word implements bitwise not, so applying it to booleans will throw an error. Boolean not is the " { $link not } " word."
186 $nl
187 "Due to the two's complement representation of signed integers, the following two lines are equivalent:" { $code "bitnot" "neg 1 -" } } ;
188
189 HELP: bit?
190 { $values { "x" integer } { "n" integer } { "?" "a boolean" } }
191 { $description "Tests if the " { $snippet "n" } "th bit of " { $snippet "x" } " is set." }
192 { $examples { $example "USING: math prettyprint ;" "0b101 2 bit? ." "t" } } ;
193
194 HELP: log2
195 { $values { "x" "a positive integer" } { "n" integer } }
196 { $description "Outputs the largest integer " { $snippet "n" } " such that " { $snippet "2^n" } " is less than or equal to " { $snippet "x" } "." }
197 { $errors "Throws an error if " { $snippet "x" } " is zero or negative." } ;
198
199 HELP: ?1+
200 { $values { "x" { $maybe number } } { "y" number } }
201 { $description "If the input is not " { $link f } ", adds one. Otherwise, outputs a " { $snippet "0" } "." } ;
202
203 HELP: sq
204 { $values { "x" number } { "y" number } }
205 { $description "Multiplies a number by itself." } ;
206
207 HELP: neg
208 { $values { "x" number } { "-x" number } }
209 { $description "Computes a number's additive inverse." } ;
210
211 HELP: recip
212 { $values { "x" number } { "y" number } }
213 { $description "Computes a number's multiplicative inverse." }
214 { $errors "Throws an error if " { $snippet "x" } " is the integer 0." } ;
215
216 HELP: rem
217 { $values { "x" rational } { "y" rational } { "z" rational } }
218 { $description
219     "Computes the remainder of dividing " { $snippet "x" } " by " { $snippet "y" } ", with the remainder always positive or zero."
220     { $list
221         "Given fixnums, always yields a fixnum."
222         "Given bignums, always yields a bignum."
223         "Given rationals, always yields a rational."
224     }
225 }
226 { $see-also "division-by-zero" mod } ;
227
228 HELP: sgn
229 { $values { "x" real } { "n" "-1, 0 or 1" } }
230 { $description
231     "Outputs one of the following:"
232     { $list
233         { "-1 if " { $snippet "x" } " is negative" }
234         { "0 if " { $snippet "x" } " is equal to 0" }
235         { "1 if " { $snippet "x" } " is positive" }
236     }
237 } ;
238
239 HELP: 2/
240 { $values { "x" integer } { "y" integer } }
241 { $description "Shifts " { $snippet "x" } " to the right by one bit." }
242 { $examples
243     { $example "USING: math prettyprint ;" "14 2/ ." "7" }
244     { $example "USING: math prettyprint ;" "17 2/ ." "8" }
245     { $example "USING: math prettyprint ;" "-17 2/ ." "-9" }
246 }
247 { $notes "This word is not equivalent to " { $snippet "2 /" } " or " { $snippet "2 /i" } "; the name is historic and originates from the Forth programming language." } ;
248
249 HELP: 2^
250 { $values { "n" "a positive integer" } { "2^n" "a positive integer" } }
251 { $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 ^" } "." } ;
252
253 HELP: zero?
254 { $values { "x" number } { "?" "a boolean" } }
255 { $description "Tests if the number is equal to zero." } ;
256
257 HELP: if-zero
258 { $values { "n" number } { "quot1" quotation } { "quot2" quotation } }
259 { $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." }
260 { $example
261     "USING: kernel math prettyprint sequences ;"
262     "3 [ \"zero\" ] [ sq ] if-zero ."
263     "9"
264 } ;
265
266 HELP: when-zero
267 { $values
268      { "n" number } { "quot" "the first quotation of an " { $link if-zero } } }
269 { $description "Makes an implicit check if the number is zero. A zero is dropped and the " { $snippet "quot" } " is called." }
270 { $examples "This word is equivalent to " { $link if-zero } " with an empty second quotation:"
271     { $example
272     "USING: math prettyprint ;"
273     "0 [ 4 ] [ ] if-zero ."
274     "4"
275     }
276     { $example
277     "USING: math prettyprint ;"
278     "0 [ 4 ] when-zero ."
279     "4"
280     }
281 } ;
282
283 HELP: unless-zero
284 { $values
285      { "n" number } { "quot" "the second quotation of an " { $link if-empty } } }
286 { $description "Makes an implicit check if the number is zero. A zero is dropped. Otherwise, the " { $snippet "quot" } " is called on the number." }
287 { $examples "This word is equivalent to " { $link if-zero } " with an empty first quotation:"
288     { $example
289     "USING: sequences math prettyprint ;"
290     "3 [ ] [ sq ] if-zero ."
291     "9"
292     }
293     { $example
294     "USING: sequences math prettyprint ;"
295     "3 [ sq ] unless-zero ."
296     "9"
297     }
298 } ;
299
300 HELP: times
301 { $values { "n" integer } { "quot" quotation } }
302 { $description "Calls the quotation " { $snippet "n" } " times." }
303 { $notes "If you need to pass the current index to the quotation, use " { $link each } "." }
304 { $examples
305     { $example "USING: io math ;" "3 [ \"Hi\" print ] times" "Hi\nHi\nHi" }
306 } ;
307
308 HELP: fp-bitwise=
309 { $values
310     { "x" float } { "y" float }
311     { "?" boolean }
312 }
313 { $description "Compares two floating point numbers for bit equality." }
314 { $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." }
315 { $examples
316     "Not-a-number equality:"
317     { $example
318         "USING: kernel math prettyprint ;"
319         "0.0 0.0 / dup number= ."
320         "f"
321     }
322     { $example
323         "USING: kernel math prettyprint ;"
324         "0.0 0.0 / dup fp-bitwise= ."
325         "t"
326     }
327     "Signed zero equality:"
328     { $example
329         "USING: math prettyprint ;"
330         "-0.0 0.0 fp-bitwise= ."
331         "f"
332     }
333     { $example
334         "USING: math prettyprint ;"
335         "-0.0 0.0 number= ."
336         "t"
337     }
338 } ;
339
340 HELP: fp-special?
341 { $values { "x" real } { "?" "a boolean" } }
342 { $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 } "." } ;
343
344 HELP: fp-nan?
345 { $values { "x" real } { "?" "a boolean" } }
346 { $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 } "." } ;
347
348 HELP: fp-qnan?
349 { $values { "x" real } { "?" "a boolean" } }
350 { $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 } "." } ;
351
352 HELP: fp-snan?
353 { $values { "x" real } { "?" "a boolean" } }
354 { $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 } "." } ;
355
356 HELP: fp-infinity?
357 { $values { "x" real } { "?" "a boolean" } }
358 { $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 } "." }
359 { $examples
360     { $example "USING: math prettyprint ;" "1/0. fp-infinity? ." "t" }
361     { $example "USING: io kernel math ;" "-1/0. [ fp-infinity? ] [ 0 < ] bi and [ \"negative infinity\" print ] when" "negative infinity" }
362 } ;
363
364 HELP: fp-sign
365 { $values { "x" float } { "?" "a boolean" } }
366 { $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." } ;
367
368 HELP: fp-nan-payload
369 { $values { "x" real } { "bits" integer } }
370 { $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 } "." } ;
371
372 HELP: <fp-nan>
373 { $values { "payload" integer } { "nan" float } }
374 { $description "Constructs an IEEE Not-a-Number value with a payload of " { $snippet "payload" } "." }
375 { $notes "A " { $snippet "payload" } " of " { $snippet "0" } " will construct an Infinity value." } ;
376
377 { fp-special? fp-nan? fp-qnan? fp-snan? fp-infinity? fp-nan-payload <fp-nan> } related-words
378
379 HELP: next-float
380 { $values { "m" float } { "n" float } }
381 { $description "Returns the least representable " { $link float } " value greater than " { $snippet "m" } ", or in the case of " { $snippet "-0.0" } ", returns " { $snippet "+0.0" } "." } ;
382
383 HELP: prev-float
384 { $values { "m" float } { "n" float } }
385 { $description "Returns the greatest representable " { $link float } " value less than " { $snippet "m" } ", or in the case of " { $snippet "+0.0" } ", returns " { $snippet "-0.0" } "." } ;
386
387 { next-float prev-float } related-words
388
389 HELP: real-part
390 { $values { "z" number } { "x" real } }
391 { $description "Outputs the real part of a complex number. This acts as the identity on real numbers." }
392 { $examples { $example "USING: math prettyprint ;" "C{ 1 2 } real-part ." "1" } } ;
393
394 HELP: imaginary-part
395 { $values { "z" number } { "y" real } }
396 { $description "Outputs the imaginary part of a complex number. This outputs zero for real numbers." }
397 { $examples
398     { $example "USING: math prettyprint ;" "C{ 1 2 } imaginary-part ." "2" }
399     { $example "USING: math prettyprint ;" "3 imaginary-part ." "0" }
400 } ;
401
402 HELP: real
403 { $class-description "The class of real numbers, which is a disjoint union of rationals and floats." } ;
404
405 HELP: number
406 { $class-description "The class of numbers." } ;
407
408 HELP: next-power-of-2
409 { $values { "m" "a non-negative integer" } { "n" "an integer" } }
410 { $description "Outputs the smallest power of 2 greater than or equal to " { $snippet "m" } ". The output value is always at least 2." } ;
411
412 HELP: power-of-2?
413 { $values { "n" integer } { "?" "a boolean" } }
414 { $description "Tests if " { $snippet "n" } " is a power of 2." } ;
415
416 HELP: each-integer
417 { $values { "n" integer } { "quot" { $quotation "( ... i -- ... )" } } }
418 { $description "Applies the quotation to each integer from 0 up to " { $snippet "n" } ", excluding " { $snippet "n" } "." }
419 { $notes "This word is used to implement " { $link each } "." } ;
420
421 HELP: all-integers?
422 { $values { "n" integer } { "quot" { $quotation "( ... i -- ... ? )" } } { "?" "a boolean" } }
423 { $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 } "." }
424 { $notes "This word is used to implement " { $link all? } "." } ;
425
426 HELP: find-integer
427 { $values { "n" integer } { "quot" { $quotation "( ... i -- ... ? )" } } { "i" "an integer or " { $link f } } }
428 { $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 } "." }
429 { $notes "This word is used to implement " { $link find } "." } ;
430
431 HELP: find-last-integer
432 { $values { "n" integer } { "quot" { $quotation "( ... i -- ... ? )" } } { "i" "an integer or " { $link f } } }
433 { $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 } "." }
434 { $notes "This word is used to implement " { $link find-last } "." } ;
435
436 ARTICLE: "division-by-zero" "Division by zero"
437 "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."
438 $nl
439 "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."
440 $nl
441 "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 } "."
442 $nl
443 "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."
444 $nl
445 "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." ;
446
447 ARTICLE: "number-protocol" "Number protocol"
448 "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."
449 $nl
450 "Two examples where you should note the types of the inputs and outputs:"
451 { $example "USE: classes" "3 >fixnum 6 >bignum * class-of ." "bignum" }
452 { $example "1/2 2.0 + ." "2.5" }
453 "The following usual operations are supported by all numbers."
454 { $subsections
455     +
456     -
457     *
458     /
459 }
460 "Non-commutative operations take operands from the stack in the natural order; " { $snippet "6 2 /" } " divides 6 by 2."
461 { $subsections "division-by-zero" }
462 "Real numbers (but not complex numbers) can be ordered:"
463 { $subsections
464     <
465     <=
466     >
467     >=
468 }
469 "Numbers can be compared for equality using " { $link = } ", or a less precise test which disregards types:"
470 { $subsections number= }
471 { $see-also "math.floats.compare" } ;
472
473 ARTICLE: "modular-arithmetic" "Modular arithmetic"
474 { $subsections
475     mod
476     rem
477     /mod
478     /i
479 }
480 { $see-also "integer-functions" } ;
481
482 ARTICLE: "bitwise-arithmetic" "Bitwise arithmetic"
483 "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" } "."
484 { $subsections
485     bitand
486     bitor
487     bitxor
488     bitnot
489     shift
490     2/
491     2^
492     bit?
493 }
494 "Advanced topics:"
495 { $subsections
496     "math.bitwise"
497     "math.bits"
498 }
499 { $see-also "booleans" } ;
500
501 ARTICLE: "arithmetic" "Arithmetic"
502 "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."
503 $nl
504 "Math words are in the " { $vocab-link "math" } " vocabulary. Implementation details are in the " { $vocab-link "math.private" } " vocabulary."
505 { $subsections
506     "number-protocol"
507     "modular-arithmetic"
508     "bitwise-arithmetic"
509 }
510 { $see-also "integers" "rationals" "floats" "complex-numbers" } ;
511
512 ABOUT: "arithmetic"
513