1 USING: help.markup help.syntax kernel math math.order
2 sequences quotations math.functions.private math.constants ;
5 ARTICLE: "integer-functions" "Integer functions"
12 "Modular exponentiation:"
13 { $subsections ^mod mod-inv }
22 ARTICLE: "arithmetic-functions" "Arithmetic functions"
23 "Computing additive and multiplicative inverses:"
24 { $subsections neg recip }
25 "Complex conjugation:"
26 { $subsections conjugate }
28 { $subsections zero? between? }
46 "Numbers implement the " { $link "math.order" } ", therefore operations such as " { $link min } " and " { $link max } " can be used with numbers." ;
48 ARTICLE: "power-functions" "Powers and logarithms"
50 { $subsections sq sqrt }
51 "Exponential and natural logarithm:"
52 { $subsections e^ cis log }
54 { $subsections log1+ log10 logn }
55 "Raising a number to a power:"
56 { $subsections ^ e^ 10^ }
57 "Logistics functions:"
58 { $subsections sigmoid }
59 "Finding the root of a number:"
60 { $subsections nth-root }
61 "Converting between rectangular and polar form:"
70 ARTICLE: "trig-hyp-functions" "Trigonometric and hyperbolic functions"
71 "Trigonometric functions:"
72 { $subsections cos sin tan }
74 { $subsections sec cosec cot }
76 { $subsections acos asin atan }
77 "Inverse reciprocals:"
78 { $subsections asec acosec acot }
79 "Hyperbolic functions:"
80 { $subsections cosh sinh tanh }
82 { $subsections sech cosech coth }
84 { $subsections acosh asinh atanh }
85 "Inverse reciprocals:"
86 { $subsections asech acosech acoth } ;
88 ARTICLE: "math-functions" "Mathematical functions"
91 "arithmetic-functions"
96 ABOUT: "math-functions"
99 { $values { "m" integer } { "w" "a power of 2" } { "n" "an integer multiple of " { $snippet "w" } } }
100 { $description "Outputs the least multiple of " { $snippet "w" } " greater than " { $snippet "m" } "." }
101 { $notes "This word will give an incorrect result if " { $snippet "w" } " is not a power of 2." } ;
104 { $values { "x" number } { "e^x" number } }
105 { $description "Exponential function, raises " { $link e } " to the power of " { $snippet "x" } "." } ;
108 { $values { "x" number } { "y" float } { "exp" integer } }
109 { $description "Break the number " { $snippet "x" } " into a normalized fraction " { $snippet "y" } " and an integral power of 2 " { $snippet "e^" } "." $nl "The function returns a number " { $snippet "y" } " in the interval [1/2, 1) or 0, and a number " { $snippet "exp" } " such that " { $snippet "x = y*(2**exp)" } "." } ;
112 { $values { "x" number } { "exp" number } { "y" number } }
113 { $description "Multiply " { $snippet "x" } " by " { $snippet "2^exp" } "." }
114 { $notes { $link ldexp } " is the inverse of " { $link frexp } "." } ;
117 { $values { "x" number } { "y" number } }
118 { $description "Natural logarithm function. Outputs negative infinity if " { $snippet "x" } " is 0." } ;
121 { $values { "x" number } { "n" number } { "y" number } }
122 { $description "Finds the base " { $snippet "n" } " logarithm of " { $snippet "x" } "." } ;
125 { $values { "x" number } { "y" number } }
126 { $description "Takes the natural logarithm of " { $snippet "1 + x" } ". Outputs negative infinity if " { $snippet "1 + x" } " is zero. This word may be more accurate than " { $snippet "1 + log" } " for very small values of " { $snippet "x" } "." } ;
129 { $values { "x" number } { "y" number } }
130 { $description "Logarithm function base 10. Outputs negative infinity if " { $snippet "x" } " is 0." } ;
133 { $values { "x" number } { "y" number } }
134 { $description "Square root function." } ;
138 { $description "Hyperbolic cosine." } ;
142 { $description "Hyperbolic secant." } ;
146 { $description "Hyperbolic sine." } ;
150 { $description "Hyperbolic cosecant." } ;
154 { $description "Hyperbolic tangent." } ;
158 { $description "Hyperbolic cotangent." } ;
162 { $description "Trigonometric cosine." } ;
166 { $description "Trigonometric secant." } ;
170 { $description "Trigonometric sine." } ;
174 { $description "Trigonometric cosecant." } ;
178 { $description "Trigonometric tangent." } ;
182 { $description "Trigonometric cotangent." } ;
186 { $description "Inverse hyperbolic cosine." } ;
190 { $description "Inverse hyperbolic secant." } ;
194 { $description "Inverse hyperbolic sine." } ;
198 { $description "Inverse hyperbolic cosecant." } ;
202 { $description "Inverse hyperbolic tangent." } ;
206 { $description "Inverse hyperbolic cotangent." } ;
210 { $description "Inverse trigonometric cosine." } ;
214 { $description "Inverse trigonometric secant." } ;
218 { $description "Inverse trigonometric sine." } ;
222 { $description "Inverse trigonometric cosecant." } ;
226 { $description "Inverse trigonometric tangent." } ;
230 { $description "Inverse trigonometric cotangent." } ;
233 { $values { "z" number } { "z*" number } }
234 { $description "Computes the complex conjugate by flipping the sign of the imaginary part of " { $snippet "z" } "." } ;
237 { $values { "z" number } { "arg" "a number in the interval " { $snippet "(-pi,pi]" } } }
238 { $description "Computes the complex argument." } ;
241 { $values { "z" number } { "abs" "a non-negative real number" } { "arg" "a number in the interval " { $snippet "(-pi,pi]" } } }
242 { $description "Converts a complex number into an absolute value and argument (polar form)." } ;
245 { $values { "arg" "a real number" } { "z" "a complex number on the unit circle" } }
246 { $description "Computes a point on the unit circle using Euler's formula for " { $snippet "e^(arg*i)" } "." } ;
248 { cis e^ } related-words
251 { $values { "abs" "a non-negative real number" } { "arg" real } { "z" number } }
252 { $description "Converts an absolute value and argument (polar form) to a complex number." } ;
255 { $values { "x" number } { "?" boolean } }
256 { $description "Tests if " { $snippet "x" } " is a real number between -1 and 1, inclusive." } ;
259 { $values { "x" number } { "y" "a non-negative real number" } }
260 { $description "Computes the absolute value of a number." }
264 { $values { "x" number } { "y" "a non-negative real number" } }
265 { $description "Computes the squared absolute value of a number. For complex numbers this is marginally more efficient than " { $link abs } "." } ;
268 { $values { "x" number } { "y" number } { "x^y" number } }
269 { $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." }
270 { $errors "Throws an error if " { $snippet "x" } " and " { $snippet "y" } " are both integer 0." } ;
273 { $values { "n" integer } { "x" number } { "y" number } }
274 { $description "Calculates the nth root of a number, such that " { $snippet "y^n=x" } "." } ;
277 { $values { "x" number } { "10^x" number } }
278 { $description "Raises 10 to the power of " { $snippet "x" } ". If " { $snippet "x" } " is an integer the answer is computed exactly, otherwise a floating point approximation is used." } ;
281 { $values { "m" integer } { "n" integer } { "?" boolean } }
282 { $description "Tests if " { $snippet "n" } " is a divisor of " { $snippet "m" } ". This is the same thing as asking if " { $snippet "m" } " is divisible by " { $snippet "n" } "." }
283 { $notes "Returns t for both negative and positive divisors, as well as for trivial and non-trivial divisors." } ;
286 { $values { "x" integer } { "n" integer } { "y" integer } }
287 { $description "Outputs an integer " { $snippet "y" } " such that " { $snippet "xy = 1 (mod n)" } "." }
288 { $errors "Throws an error if " { $snippet "n" } " is not invertible modulo " { $snippet "n" } "." }
290 { $example "USING: math.functions prettyprint ;" "173 1119 mod-inv ." "815" }
291 { $example "USING: math prettyprint ;" "173 815 * 1119 mod ." "1" }
295 { $values { "x" real } { "y" real } { "n" real } { "z" real } }
296 { $description "Outputs the result of computing " { $snippet "x^y mod n" } "." } ;
299 { $values { "x" real } { "y" real } { "epsilon" real } { "?" boolean } }
300 { $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" } ":"
302 { { $snippet "epsilon" } " is zero: exact comparison." }
303 { { $snippet "epsilon" } " is positive: absolute distance test." }
304 { { $snippet "epsilon" } " is negative: relative distance test." }
309 { $values { "x" real } { "y" "a whole real number" } }
310 { $description "Outputs the number that results from subtracting the fractional component of " { $snippet "x" } "." }
311 { $notes "The result is not necessarily an integer." } ;
314 { $values { "x" real } { "y" "a whole real number" } }
315 { $description "Outputs the greatest whole number smaller than or equal to " { $snippet "x" } "." }
316 { $notes "The result is not necessarily an integer." } ;
319 { $values { "x" real } { "y" "a whole real number" } }
320 { $description "Outputs the least whole number greater than or equal to " { $snippet "x" } "." }
321 { $notes "The result is not necessarily an integer." } ;
324 { $values { "x" real } { "y" "a whole real number" } }
325 { $description "Outputs the whole number closest to " { $snippet "x" } ", rounding out at half." }
326 { $notes "The result is not necessarily an integer." }
328 { $example "USING: math.functions prettyprint ;" "4.5 round ." "5.0" }
329 { $example "USING: math.functions prettyprint ;" "4.4 round ." "4.0" }
333 { $values { "x" real } { "y" "a whole real number" } }
334 { $description "Outputs the whole number closest to " { $snippet "x" } ", rounding out at half, breaking ties towards even numbers. This is also known as banker's rounding or unbiased rounding." }
335 { $notes "The result is not necessarily an integer." }
337 { $example "USING: math.functions prettyprint ;" "0.5 round-to-even ." "0.0" }
338 { $example "USING: math.functions prettyprint ;" "1.5 round-to-even ." "2.0" }
342 { $values { "x" real } { "y" "a whole real number" } }
343 { $description "Outputs the whole number closest to " { $snippet "x" } ", rounding out at half, breaking ties towards odd numbers." }
344 { $notes "The result is not necessarily an integer." }
346 { $example "USING: math.functions prettyprint ;" "0.5 round-to-odd ." "1.0" }
347 { $example "USING: math.functions prettyprint ;" "1.5 round-to-odd ." "1.0" }
351 { $values { "x" number } { "t" integer } { "seq" sequence } }
352 { $description "Outputs the " { $snippet "t" } " roots of a number " { $snippet "x" } "." }
353 { $notes "The results are not necessarily real." } ;
356 { $values { "x" number } { "y" number } }
357 { $description "Outputs the sigmoid, an S-shaped \"logistic\" function, from 0 to 1, of the number " { $snippet "x" } "." } ;
360 { $values { "x" number } { "y" number } }
361 { $description "Calculates the signum value. For a real number, " { $snippet "x" } ", this is its sign (-1, 0, or 1). For a complex number, " { $snippet "x" } ", this is the point on the unit circle of the complex plane that is nearest to " { $snippet "x" } "." } ;
364 { $values { "x" number } { "y" number } { "x'" number } }
365 { $description "Returns " { $snippet "x" } " with the sign of " { $snippet "y" } ", as a " { $link float } "." } ;