1 USING: help.markup help.syntax kernel sequences quotations
2 math.private byte-arrays io.binary ;
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." }
10 { $example "USING: math prettyprint ;" "3.0 3 number= ." "t" }
11 { $example "USING: kernel math prettyprint ;" "3.0 3 = ." "f" }
15 { $values { "x" real } { "y" real } { "?" "a boolean" } }
16 { $description "Tests if " { $snippet "x" } " is less than " { $snippet "y" } "." } ;
19 { $values { "x" real } { "y" real } { "?" "a boolean" } }
20 { $description "Tests if " { $snippet "x" } " is less than or equal to " { $snippet "y" } "." } ;
23 { $values { "x" real } { "y" real } { "?" "a boolean" } }
24 { $description "Tests if " { $snippet "x" } " is greater than " { $snippet "y" } "." } ;
27 { $values { "x" real } { "y" real } { "?" "a boolean" } }
28 { $description "Tests if " { $snippet "x" } " is greater than or equal to " { $snippet "y" } "." } ;
32 { $values { "x" number } { "y" number } { "z" number } }
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."
44 { $values { "x" number } { "y" number } { "z" number } }
46 "Subtracts " { $snippet "y" } " from " { $snippet "x" } "."
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."
56 { $values { "x" number } { "y" number } { "z" number } }
58 "Multiplies two numbers."
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."
68 { $values { "x" number } { "y" number } { "z" number } }
70 "Divides " { $snippet "x" } " by " { $snippet "y" } ", retaining as much precision as possible."
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."
78 { $see-also "division-by-zero" } ;
81 { $values { "x" real } { "y" real } { "z" integer } }
83 "Divides " { $snippet "x" } " by " { $snippet "y" } ", truncating the result to an integer."
85 { $see-also "division-by-zero" } ;
88 { $values { "x" real } { "y" real } { "z" float } }
90 "Divides " { $snippet "x" } " by " { $snippet "y" } ", representing the result as a floating point number."
92 { $see-also "division-by-zero" } ;
95 { $values { "x" rational } { "y" rational } { "z" rational } }
97 "Computes the remainder of dividing " { $snippet "x" } " by " { $snippet "y" } ", with the remainder being negative if " { $snippet "x" } " is negative."
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" } "." }
104 { $see-also "division-by-zero" rem } ;
107 { $values { "x" integer } { "y" integer } { "z" integer } { "w" integer } }
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."
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."
115 { $see-also "division-by-zero" } ;
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." }
121 { $example "USING: math prettyprint ;" "BIN: 101 BIN: 10 bitand .b" "0" }
122 { $example "USING: math prettyprint ;" "BIN: 110 BIN: 10 bitand .b" "10" }
124 { $notes "This word implements bitwise and, so applying it to booleans will throw an error. Boolean and is the " { $link and } " word." } ;
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." }
130 { $example "USING: math prettyprint ;" "BIN: 101 BIN: 10 bitor .b" "111" }
131 { $example "USING: math prettyprint ;" "BIN: 110 BIN: 10 bitor .b" "110" }
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." } ;
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." }
139 { $example "USING: math prettyprint ;" "BIN: 101 BIN: 10 bitxor .b" "111" }
140 { $example "USING: math prettyprint ;" "BIN: 110 BIN: 10 bitxor .b" "100" }
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." } ;
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" } } ;
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."
154 "Due to the two's complement representation of signed integers, the following two lines are equivalent:" { $code "bitnot" "neg 1-" } } ;
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" } } ;
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." } ;
167 { $values { "x" number } { "y" number } }
169 "Increments a number by 1. The following two lines are equivalent:"
171 "There is no difference in behavior or efficiency."
175 { $values { "x" number } { "y" number } }
177 "Decrements a number by 1. The following two lines are equivalent:"
179 "There is no difference in behavior or efficiency."
183 { $values { "x" { $maybe number } } { "y" number } }
184 { $description "If the input is not " { $link f } ", adds one. Otherwise, outputs a " { $snippet "0" } "." } ;
187 { $values { "x" number } { "y" number } }
188 { $description "Multiplies a number by itself." } ;
191 { $values { "x" number } { "-x" number } }
192 { $description "Computes a number's additive inverse." } ;
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." } ;
200 { $values { "x" rational } { "y" rational } { "z" rational } }
202 "Computes the remainder of dividing " { $snippet "x" } " by " { $snippet "y" } ", with the remainder always positive."
204 "Given fixnums, always yields a fixnum."
205 "Given bignums, always yields a bignum."
206 "Given rationals, always yields a rational."
209 { $see-also "division-by-zero" mod } ;
212 { $values { "x" real } { "n" "-1, 0 or 1" } }
214 "Outputs one of the following:"
216 "-1 if " { $snippet "x" } " is negative"
217 "0 if " { $snippet "x" } " is equal to 0"
218 "1 if " { $snippet "x" } " is positive"
223 { $values { "x" integer } { "y" integer } }
224 { $description "Shifts " { $snippet "x" } " to the right by one bit." }
226 { $example "USING: math prettyprint ;" "14 2/ ." "7" }
227 { $example "USING: math prettyprint ;" "17 2/ ." "8" }
228 { $example "USING: math prettyprint ;" "-17 2/ ." "-9" }
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." } ;
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 ^" } "." } ;
237 { $values { "x" number } { "?" "a boolean" } }
238 { $description "Tests if the number is equal to zero." } ;
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 } "." }
245 { $example "USING: io math ;" "3 [ \"Hi\" print ] times" "Hi\nHi\nHi" }
249 { $values { "x" real } { "?" "a boolean" } }
250 { $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 } "." } ;
253 { $values { "x" real } { "?" "a boolean" } }
254 { $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 } "." }
256 { $example "USING: math prettyprint ;" "1/0. fp-infinity? ." "t" }
257 { $example "USING: io kernel math ;" "-1/0. [ fp-infinity? ] [ 0 < ] bi [ \"negative infinity\" print ] when" "negative infinity" }
260 { fp-nan? fp-infinity? } related-words
263 { $values { "z" number } { "x" real } }
264 { $description "Outputs the real part of a complex number. This acts as the identity on real numbers." }
265 { $examples { $example "USING: math prettyprint ; C{ 1 2 } real-part ." "1" } } ;
268 { $values { "z" number } { "y" real } }
269 { $description "Outputs the imaginary part of a complex number. This outputs zero for real numbers." }
271 { $example "USING: math prettyprint ; C{ 1 2 } imaginary-part ." "2" }
272 { $example "USING: math prettyprint ; 3 imaginary-part ." "0" }
276 { $class-description "The class of real numbers, which is a disjoint union of rationals and floats." } ;
279 { $class-description "The class of numbers." } ;
281 HELP: next-power-of-2
282 { $values { "m" "a non-negative integer" } { "n" "an integer" } }
283 { $description "Outputs the smallest power of 2 greater than " { $snippet "m" } ". The output value is always at least 1." } ;
286 { $values { "n" integer } { "?" "a boolean" } }
287 { $description "Tests if " { $snippet "n" } " is a power of 2." } ;
290 { $values { "n" integer } { "quot" { $quotation "( i -- )" } } }
291 { $description "Applies the quotation to each integer from 0 up to " { $snippet "n" } ", excluding " { $snippet "n" } "." }
292 { $notes "This word is used to implement " { $link each } "." } ;
295 { $values { "n" integer } { "quot" { $quotation "( i -- ? )" } } { "?" "a boolean" } }
296 { $description "Applies the quotation to each integer from 0 up to " { $snippet "n" } ", excluding " { $snippet "n" } ". Iterationi stops when the quotation outputs " { $link f } " or the end is reached. If the quotation yields a false value for some integer, this word outputs " { $link f } ". Otherwise, this word outputs " { $link t } "." }
297 { $notes "This word is used to implement " { $link all? } "." } ;
300 { $values { "n" integer } { "quot" { $quotation "( i -- ? )" } } { "i" "an integer or " { $link f } } }
301 { $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 } "." }
302 { $notes "This word is used to implement " { $link find } "." } ;
304 HELP: find-last-integer
305 { $values { "n" integer } { "quot" { $quotation "( i -- ? )" } } { "i" "an integer or " { $link f } } }
306 { $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 } "." }
307 { $notes "This word is used to implement " { $link find-last } "." } ;
309 HELP: byte-array>bignum
310 { $values { "byte-array" byte-array } { "n" integer } }
311 { $description "Converts a byte-array, interpreted as little-endian, into a bignum integer. User code should call " { $link >le } " or " { $link >be } " instead." } ;
313 ARTICLE: "division-by-zero" "Division by zero"
314 "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."
316 "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."
318 "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." ;
320 ARTICLE: "number-protocol" "Number protocol"
321 "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."
323 "Two examples where you should note the types of the inputs and outputs:"
324 { $example "3 >fixnum 6 >bignum * class ." "bignum" }
325 { $example "1/2 2.0 + ." "4.5" }
326 "The following usual operations are supported by all numbers."
331 "Non-commutative operations take operands from the stack in the natural order; " { $snippet "6 2 /" } " divides 6 by 2."
332 { $subsection "division-by-zero" }
333 "Real numbers (but not complex numbers) can be ordered:"
338 "Numbers can be compared for equality using " { $link = } ", or a less precise test which disregards types:"
339 { $subsection number= } ;
341 ARTICLE: "modular-arithmetic" "Modular arithmetic"
346 { $see-also "integer-functions" } ;
348 ARTICLE: "bitwise-arithmetic" "Bitwise arithmetic"
349 "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" } "."
350 { $subsection bitand }
351 { $subsection bitor }
352 { $subsection bitxor }
353 { $subsection bitnot }
354 { $subsection shift }
358 "The " { $vocab-link "math.bitwise" } " vocabulary implements additional bitwise integer operations."
359 { $see-also "conditionals" } ;
361 ARTICLE: "arithmetic" "Arithmetic"
362 "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."
364 "Math words are in the " { $vocab-link "math" } " vocabulary. Implementation details are in the " { $vocab-link "math.private" } " vocabulary."
365 { $subsection "number-protocol" }
366 { $subsection "modular-arithmetic" }
367 { $subsection "bitwise-arithmetic" }
368 { $see-also "integers" "rationals" "floats" "complex-numbers" } ;