1 USING: help.markup help.syntax kernel math math.order
2 sequences quotations math.functions.private ;
5 ARTICLE: "integer-functions" "Integer functions"
9 { $subsection next-power-of-2 }
10 "Modular exponentiation:"
12 { $subsection mod-inv }
14 { $subsection power-of-2? }
16 { $subsection odd? } ;
18 ARTICLE: "arithmetic-functions" "Arithmetic functions"
19 "Computing additive and multiplicative inverses:"
22 "Incrementing, decrementing:"
28 "Complex conjugation:"
29 { $subsection conjugate }
32 { $subsection between? }
36 { $subsection ceiling }
38 { $subsection truncate }
43 ARTICLE: "power-functions" "Powers and logarithms"
47 "Exponential and natural logarithm:"
51 "Raising a number to a power:"
53 "Converting between rectangular and polar form:"
57 { $subsection >polar }
58 { $subsection polar> } ;
60 ARTICLE: "trig-hyp-functions" "Trigonometric and hyperbolic functions"
61 "Trigonometric functions:"
73 "Inverse reciprocals:"
75 { $subsection acosec }
77 "Hyperbolic functions:"
83 { $subsection cosech }
89 "Inverse reciprocals:"
91 { $subsection acosech }
92 { $subsection acoth } ;
94 ARTICLE: "math-functions" "Mathematical functions"
95 { $subsection "integer-functions" }
96 { $subsection "arithmetic-functions" }
97 { $subsection "power-functions" }
98 { $subsection "trig-hyp-functions" } ;
100 ABOUT: "math-functions"
103 { $values { "x" real } { "y" real } { "z" number } }
104 { $description "Creates a complex number from real and imaginary components." }
105 { $warning "This word does not check that the arguments are real numbers, which can have undefined consequences. Use the " { $link rect> } " word instead." } ;
108 { $values { "x" real } { "y" real } { "z" number } }
109 { $description "Creates a complex number from real and imaginary components. If " { $snippet "z" } " is an integer zero, this will simply output " { $snippet "x" } "." } ;
112 { $values { "z" number } { "x" real } { "y" real } }
113 { $description "Extracts the real and imaginary components of a complex number." } ;
116 { $values { "m" integer } { "w" "a power of 2" } { "n" "an integer multiple of " { $snippet "w" } } }
117 { $description "Outputs the least multiple of " { $snippet "w" } " greater than " { $snippet "m" } "." }
118 { $notes "This word will give an incorrect result if " { $snippet "w" } " is not a power of 2." } ;
121 { $values { "x" number } { "y" number } }
122 { $description "Exponential function, " { $snippet "y=e^x" } "." } ;
125 { $values { "x" number } { "y" number } }
126 { $description "Natural logarithm function. Outputs negative infinity if " { $snippet "x" } " is 0." } ;
129 { $values { "x" number } { "y" number } }
130 { $description "Square root function." } ;
134 { $description "Hyperbolic cosine." } ;
138 { $description "Hyperbolic secant." } ;
142 { $description "Hyperbolic sine." } ;
146 { $description "Hyperbolic cosecant." } ;
150 { $description "Hyperbolic tangent." } ;
154 { $description "Hyperbolic cotangent." } ;
158 { $description "Trigonometric cosine." } ;
162 { $description "Trigonometric secant." } ;
166 { $description "Trigonometric sine." } ;
170 { $description "Trigonometric cosecant." } ;
174 { $description "Trigonometric tangent." } ;
178 { $description "Trigonometric cotangent." } ;
182 { $description "Inverse hyperbolic cosine." } ;
186 { $description "Inverse hyperbolic secant." } ;
190 { $description "Inverse hyperbolic sine." } ;
194 { $description "Inverse hyperbolic cosecant." } ;
198 { $description "Inverse hyperbolic tangent." } ;
202 { $description "Inverse hyperbolic cotangent." } ;
206 { $description "Inverse trigonometric cosine." } ;
210 { $description "Inverse trigonometric secant." } ;
214 { $description "Inverse trigonometric sine." } ;
218 { $description "Inverse trigonometric cosecant." } ;
222 { $description "Inverse trigonometric tangent." } ;
226 { $description "Inverse trigonometric cotangent." } ;
229 { $values { "z" number } { "z*" number } }
230 { $description "Computes the complex conjugate by flipping the sign of the imaginary part of " { $snippet "z" } "." } ;
233 { $values { "z" number } { "arg" "a number in the interval " { $snippet "(-pi,pi]" } } }
234 { $description "Computes the complex argument." } ;
237 { $values { "z" number } { "abs" "a non-negative real number" } { "arg" "a number in the interval " { $snippet "(-pi,pi]" } } }
238 { $description "Creates a complex number from an absolute value and argument (polar form)." } ;
241 { $values { "arg" "a real number" } { "z" "a complex number on the unit circle" } }
242 { $description "Computes a point on the unit circle using Euler's formula for " { $snippet "exp(arg*i)" } "." } ;
244 { cis exp } related-words
247 { $values { "z" number } { "abs" "a non-negative real number" } { "arg" real } }
248 { $description "Converts an absolute value and argument (polar form) to a complex number." } ;
251 { $values { "x" number } { "?" "a boolean" } }
252 { $description "Tests if " { $snippet "x" } " is a real number between -1 and 1, inclusive." } ;
255 { $values { "x" number } { "y" "a non-negative real number" } }
256 { $description "Computes the absolute value of a complex number." } ;
259 { $values { "x" number } { "y" "a non-negative real number" } }
260 { $description "Computes the squared absolute value of a complex number. This is marginally more efficient than " { $link abs } "." } ;
263 { $values { "x" number } { "y" number } { "z" number } }
264 { $description "Raises " { $snippet "x" } " to the power of " { $snippet "y" } ". If " { $snippet "y" } " is an integer the answer is computed exactly, otherwise a floating point approximation is used." }
265 { $errors "Throws an error if " { $snippet "x" } " and " { $snippet "y" } " are both integer 0." } ;
268 { $values { "x" integer } { "y" integer } { "a" integer } { "d" integer } }
269 { $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" } }
270 { $notes "If " { $snippet "d" } " is 1, then " { $snippet "a" } " is the inverse of " { $snippet "y" } " modulo " { $snippet "x" } "." } ;
273 { $values { "x" integer } { "n" integer } { "y" integer } }
274 { $description "Outputs an integer " { $snippet "y" } " such that " { $snippet "xy = 1 (mod n)" } "." }
275 { $errors "Throws an error if " { $snippet "n" } " is not invertible modulo " { $snippet "n" } "." }
277 { $example "USING: math.functions prettyprint ;" "173 1119 mod-inv ." "815" }
278 { $example "USING: math prettyprint ;" "173 815 * 1119 mod ." "1" }
282 { $values { "n" integer } { "quot" { $quotation "( ? -- )" } } }
283 { $description "Applies the quotation to each bit of the integer, starting from the least significant bit, and stopping at the last bit from which point on all bits are either clear (if the integer is positive) or all bits are set (if the integer is negataive)." }
285 { $example "USING: math.functions make prettyprint ;" "[ BIN: 1101 [ , ] each-bit ] { } make ." "{ t f t t }" }
286 { $example "USING: math.functions make prettyprint ;" "[ -3 [ , ] each-bit ] { } make ." "{ t f }" }
290 { $values { "x" real } { "y" real } { "epsilon" real } { "?" "a boolean" } }
291 { $description "Tests if " { $snippet "x" } " and " { $snippet "y" } " are approximately equal to each other. There are three possible comparison tests, chosen based on the sign of " { $snippet "epsilon" } ":"
293 { { $snippet "epsilon" } " is zero: exact comparison." }
294 { { $snippet "epsilon" } " is positive: absolute distance test." }
295 { { $snippet "epsilon" } " is negative: relative distance test." }
301 { $values { "x" real } { "y" "a whole real number" } }
302 { $description "Outputs the number that results from subtracting the fractional component of " { $snippet "x" } "." }
303 { $notes "The result is not necessarily an integer." } ;
306 { $values { "x" real } { "y" "a whole real number" } }
307 { $description "Outputs the greatest whole number smaller than or equal to " { $snippet "x" } "." }
308 { $notes "The result is not necessarily an integer." } ;
311 { $values { "x" real } { "y" "a whole real number" } }
312 { $description "Outputs the least whole number greater than or equal to " { $snippet "x" } "." }
313 { $notes "The result is not necessarily an integer." } ;
316 { $values { "x" real } { "y" "a whole real number" } }
317 { $description "Outputs the whole number closest to " { $snippet "x" } "." }
318 { $notes "The result is not necessarily an integer." } ;