226 lines
8.6 KiB
Plaintext
226 lines
8.6 KiB
Plaintext
USING: help math prettyprint sequences ;
|
|
|
|
ARTICLE: "math" "Mathematics"
|
|
"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."
|
|
$terpri
|
|
"Math words are in the " { $snippet "math" } " vocabulary. Implementation details are in the " { $snippet "math-internals" } " vocabulary."
|
|
{ $subsection "number-protocol" }
|
|
{ $subsection "number-types" }
|
|
{ $subsection "math-functions" }
|
|
{ $subsection "math-constants" }
|
|
{ $subsection "math-vectors" } ;
|
|
|
|
ARTICLE: "number-types" "Types of numbers"
|
|
{ $subsection "integers" }
|
|
{ $subsection "rationals" }
|
|
{ $subsection "floats" }
|
|
{ $subsection "complex-numbers" } ;
|
|
|
|
ARTICLE: "number-protocol" "Number protocol"
|
|
"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."
|
|
$terpri
|
|
"Two examples where you should note the types of the inputs and outputs:"
|
|
{ $example "3 >fixnum 6 >bignum * class ." "bignum" }
|
|
{ $example "1/2 2.0 + ." "4.5" }
|
|
"The following usual operations are supported by all numbers."
|
|
{ $subsection + }
|
|
{ $subsection - }
|
|
{ $subsection * }
|
|
{ $subsection / }
|
|
"Non-commutative operations take operands from the stack in the natural order; " { $snippet "6 2 /" } " divides 6 by 2."
|
|
$terpri
|
|
"Real numbers (but not complex numbers) can be ordered:"
|
|
{ $subsection < }
|
|
{ $subsection <= }
|
|
{ $subsection > }
|
|
{ $subsection >= } ;
|
|
|
|
ARTICLE: "integers" "Integers"
|
|
{ $subsection integer }
|
|
"Integers come in two varieties -- fixnums and bignums. Fixnums fit in a machine word and are faster to manipulate; if the result of a fixnum operation is too large to fit in a fixnum, the result is upgraded to a bignum. Here is an example where two fixnums are multiplied yielding a bignum:"
|
|
{ $example "134217728 class ." "fixnum" }
|
|
{ $example "128 class ." "fixnum" }
|
|
{ $example "134217728 128 * ." "17179869184" }
|
|
{ $example "134217728 128 * class ." "bignum" }
|
|
"Integers can be entered using a different base; see " { $link "syntax-numbers" } "."
|
|
$terpri
|
|
"Integers can be tested for, and real numbers can be converted to integers:"
|
|
{ $subsection fixnum? }
|
|
{ $subsection bignum? }
|
|
{ $subsection >fixnum }
|
|
{ $subsection >bignum }
|
|
"The " { $link . } " word prints numbers in decimal. A set of words in the " { $snippet "prettyprint" } " vocabulary is provided to print integers using another base."
|
|
{ $subsection .b }
|
|
{ $subsection .o }
|
|
{ $subsection .h }
|
|
"Some mathematical operations are only supported on integers."
|
|
{ $subsection "modular-arithmetic" }
|
|
{ $subsection "bitwise-arithmetic" }
|
|
{ $subsection "random-numbers" } ;
|
|
|
|
ARTICLE: "modular-arithmetic" "Modular arithmetic"
|
|
{ $subsection mod }
|
|
{ $subsection rem }
|
|
{ $subsection /mod }
|
|
{ $subsection /i }
|
|
{ $subsection gcd } ;
|
|
|
|
ARTICLE: "bitwise-arithmetic" "Bitwise arithmetic"
|
|
"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" } "."
|
|
{ $subsection bitand }
|
|
{ $subsection bitor }
|
|
{ $subsection bitxor }
|
|
{ $subsection bitnot }
|
|
{ $subsection shift }
|
|
{ $subsection log2 }
|
|
{ $subsection power-of-2? }
|
|
{ $subsection next-power-of-2 }
|
|
{ $subsection each-bit } ;
|
|
|
|
ARTICLE: "random-numbers" "Generating random integers"
|
|
{ $subsection (random-int) }
|
|
{ $subsection random-int } ;
|
|
|
|
ARTICLE: "rationals" "Rational numbers"
|
|
{ $subsection ratio }
|
|
"When we add, subtract or multiply any two integers, the result is always an integer. However, dividing a numerator by a denominator that is not an integral divisor of the denominator yields a ratio:"
|
|
{ $example "1210 11 / ." "110" }
|
|
{ $example "100 330 / ." "10/33" }
|
|
"Ratios are printed and can be input literally in the form above. Ratios are always reduced to lowest terms by factoring out the greatest common divisor of the numerator and denominator. A ratio with a denominator of 1 becomes an integer. Division with a denominator of 0 throws an error."
|
|
$terpri
|
|
"Ratios behave just like any other number -- all numerical operations work as you would expect."
|
|
{ $example "1/2 1/3 + ." "5/6" }
|
|
{ $example "100 6 / 3 * ." "50" }
|
|
"Ratios can be taken apart:"
|
|
{ $subsection numerator }
|
|
{ $subsection denominator }
|
|
{ $subsection >fraction } ;
|
|
|
|
ARTICLE: "floats" "Floats"
|
|
"Rational numbers represent " { $emphasis "exact" } " quantities. On the other hand, a floating point number is an " { $emphasis "approximation" } ". While rationals can grow to any required precision, floating point numbers are fixed-width, and manipulating them is usually faster than manipulating ratios or bignums (but slower than manipulating fixnums). Floating point numbers are often used to represent irrational numbers, which have no exact representation as a ratio of two integers."
|
|
$terpri
|
|
"Floating point literals are input with a decimal point."
|
|
{ $example "1.23 1.5 + ." "1.73" }
|
|
"Introducing a floating point number in a computation forces the result to be expressed in floating point."
|
|
{ $example "5/4 1/2 + ." "7/4" }
|
|
{ $example "5/4 0.5 + ." "1.75" }
|
|
"Integers and rationals can be converted to floats:"
|
|
{ $subsection >float }
|
|
"Two real numbers can be divided yielding a float result:"
|
|
{ $subsection /f }
|
|
"Floating point numbers are represented internally in IEEE 754 double-precision format. This internal representation can be accessed for advanced operations and input/output purposes."
|
|
{ $subsection float>bits }
|
|
{ $subsection double>bits }
|
|
{ $subsection bits>float }
|
|
{ $subsection bits>double } ;
|
|
|
|
ARTICLE: "complex-numbers" "Complex numbers"
|
|
{ $subsection complex }
|
|
"Complex numbers arise as solutions to quadratic equations whose graph does not intersect the " { $emphasis "x" } " axis. Their literal syntax is covered in " { $link "syntax-complex-numbers" } "."
|
|
$terpri
|
|
"Unlike math, where all real numbers are also complex numbers, Factor only considers a number to be a complex number if its imaginary part is non-zero. However, complex number operations are fully supported for real numbers; they are treated as having an imaginary part of zero."
|
|
$terpri
|
|
"Complex numbers can be taken apart:"
|
|
{ $subsection real }
|
|
{ $subsection imaginary }
|
|
{ $subsection >rect }
|
|
"Complex numbers can be constructed from real numbers:"
|
|
{ $subsection rect> }
|
|
"The polar form can be computed:"
|
|
{ $subsection abs }
|
|
{ $subsection absq }
|
|
{ $subsection arg }
|
|
{ $subsection >polar }
|
|
{ $subsection polar> }
|
|
"Reflection in the " { $snippet "x" } " axis:"
|
|
{ $subsection conjugate } ;
|
|
|
|
ARTICLE: "math-functions" "Mathematical functions"
|
|
{ $subsection "arithmetic-functions" }
|
|
{ $subsection "power-functions" }
|
|
{ $subsection "trig-hyp-functions" } ;
|
|
|
|
ARTICLE: "arithmetic-functions" "Arithmetic functions"
|
|
"Computing additive and multiplicative inverses:"
|
|
{ $subsection neg }
|
|
{ $subsection recip }
|
|
"Rounding:"
|
|
{ $subsection ceiling }
|
|
{ $subsection floor }
|
|
{ $subsection truncate } ;
|
|
|
|
ARTICLE: "power-functions" "Powers and logarithms"
|
|
"Squares:"
|
|
{ $subsection sq }
|
|
{ $subsection sqrt }
|
|
"Exponential and natural logarithm:"
|
|
{ $subsection exp }
|
|
{ $subsection cis }
|
|
{ $subsection log }
|
|
"Raising a number to a power:"
|
|
{ $subsection ^ } ;
|
|
|
|
ARTICLE: "trig-hyp-functions" "Trigonometric and hyperbolic functions"
|
|
"Trigonometric functions:"
|
|
{ $subsection cos }
|
|
{ $subsection sin }
|
|
{ $subsection tan }
|
|
"Reciprocals:"
|
|
{ $subsection sec }
|
|
{ $subsection cosec }
|
|
{ $subsection cot }
|
|
"Inverses:"
|
|
{ $subsection acos }
|
|
{ $subsection asin }
|
|
{ $subsection atan }
|
|
"Inverse reciprocals:"
|
|
{ $subsection asec }
|
|
{ $subsection acosec }
|
|
{ $subsection acot }
|
|
"Hyperbolic functions:"
|
|
{ $subsection cosh }
|
|
{ $subsection sinh }
|
|
{ $subsection tanh }
|
|
"Reciprocals:"
|
|
{ $subsection sech }
|
|
{ $subsection cosech }
|
|
{ $subsection coth }
|
|
"Inverses:"
|
|
{ $subsection acosh }
|
|
{ $subsection asinh }
|
|
{ $subsection atanh }
|
|
"Inverse reciprocals:"
|
|
{ $subsection asech }
|
|
{ $subsection acosech }
|
|
{ $subsection acoth } ;
|
|
|
|
ARTICLE: "math-constants" "Constants"
|
|
{ $subsection i }
|
|
{ $subsection -i }
|
|
{ $subsection e }
|
|
{ $subsection pi }
|
|
{ $subsection most-positive-fixnum }
|
|
{ $subsection most-negative-fixnum } ;
|
|
|
|
ARTICLE: "math-vectors" "Vector arithmetic"
|
|
"Any Factor sequence can be used to represent a mathematical vector."
|
|
$terpri
|
|
"Acting on vectors by a scalar:"
|
|
{ $subsection vneg }
|
|
{ $subsection v*n }
|
|
{ $subsection n*v }
|
|
{ $subsection v/n }
|
|
{ $subsection n/v }
|
|
"Combining two vectors to form another vector using " { $link 2map } ":"
|
|
{ $subsection v+ }
|
|
{ $subsection v- }
|
|
{ $subsection v* }
|
|
{ $subsection v/ }
|
|
{ $subsection vmax }
|
|
{ $subsection vmin }
|
|
"Inner product and norm:"
|
|
{ $subsection v. }
|
|
{ $subsection norm }
|
|
{ $subsection norm-sq }
|
|
{ $subsection normalize } ;
|