add docs for if-zero etc, add docs for 10^

db4
Doug Coleman 2009-08-11 18:45:01 -05:00
parent eccc919c18
commit 02becc26fc
5 changed files with 72 additions and 8 deletions

View File

@ -50,8 +50,10 @@ ARTICLE: "power-functions" "Powers and logarithms"
{ $subsection exp }
{ $subsection cis }
{ $subsection log }
{ $subsection log10 }
"Raising a number to a power:"
{ $subsection ^ }
{ $subsection 10^ }
"Converting between rectangular and polar form:"
{ $subsection abs }
{ $subsection absq }
@ -122,6 +124,10 @@ HELP: log
{ $values { "x" number } { "y" number } }
{ $description "Natural logarithm function. Outputs negative infinity if " { $snippet "x" } " is 0." } ;
HELP: log10
{ $values { "x" number } { "y" number } }
{ $description "Logarithm function base 10. Outputs negative infinity if " { $snippet "x" } " is 0." } ;
HELP: sqrt
{ $values { "x" number } { "y" number } }
{ $description "Square root function." } ;
@ -261,6 +267,10 @@ HELP: ^
{ $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." }
{ $errors "Throws an error if " { $snippet "x" } " and " { $snippet "y" } " are both integer 0." } ;
HELP: 10^
{ $values { "x" number } { "y" number } }
{ $description "Raises " { $snippet "x" } " to the power of 10. If " { $snippet "x" } " is an integer the answer is computed exactly, otherwise a floating point approximation is used." } ;
HELP: gcd
{ $values { "x" integer } { "y" integer } { "a" integer } { "d" integer } }
{ $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" } }

View File

@ -118,8 +118,6 @@ ERROR: non-trivial-divisor n ;
-rot (^mod)
] if ; foldable
: 10^ ( n -- n' ) 10 swap ^ ; inline
GENERIC: absq ( x -- y ) foldable
M: real absq sq ;
@ -160,6 +158,10 @@ M: real log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ;
M: complex log >polar swap flog swap rect> ;
: 10^ ( x -- y ) 10 swap ^ ; inline
: log10 ( x -- y ) log 10 log / ; inline
GENERIC: cos ( x -- y ) foldable
M: complex cos

View File

@ -123,7 +123,48 @@ HELP: unless-empty
}
} ;
{ if-empty when-empty unless-empty } related-words
HELP: if-zero
{ $values { "n" number } { "quot1" quotation } { "quot2" quotation } }
{ $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." }
{ $example
"USING: kernel math prettyprint sequences ;"
"3 [ \"zero\" ] [ sq ] if-zero ."
"9"
} ;
HELP: when-zero
{ $values
{ "n" number } { "quot" "the first quotation of an " { $link if-zero } } }
{ $description "Makes an implicit check if the sequence is empty. A zero is dropped and the " { $snippet "quot" } " is called." }
{ $examples "This word is equivalent to " { $link if-zero } " with an empty second quotation:"
{ $example
"USING: sequences prettyprint ;"
"0 [ 4 ] [ ] if-zero ."
"4"
}
{ $example
"USING: sequences prettyprint ;"
"0 [ 4 ] when-zero ."
"4"
}
} ;
HELP: unless-zero
{ $values
{ "n" number } { "quot" "the second quotation of an " { $link if-empty } } }
{ $description "Makes an implicit check if the number is zero. A zero is dropped. Otherwise, the " { $snippet "quot" } " is called on the number." }
{ $examples "This word is equivalent to " { $link if-zero } " with an empty first quotation:"
{ $example
"USING: sequences math prettyprint ;"
"3 [ ] [ sq ] if-empty ."
"9"
}
{ $example
"USING: sequences math prettyprint ;"
"3 [ sq ] unless-zero ."
"9"
}
} ;
HELP: delete-all
{ $values { "seq" "a resizable sequence" } }
@ -1393,6 +1434,18 @@ $nl
$nl
"More elaborate counted loops can be performed with " { $link "math.ranges" } "." ;
ARTICLE: "sequences-if" "Control flow with sequences"
"To reduce the boilerplate of checking if a sequence is empty or a number is zero, several combinators are provided."
$nl
"Checking if a sequence is empty:"
{ $subsection if-empty }
{ $subsection when-empty }
{ $subsection unless-empty }
"Checking if a number is zero:"
{ $subsection if-zero }
{ $subsection when-zero }
{ $subsection unless-zero } ;
ARTICLE: "sequences-access" "Accessing sequence elements"
{ $subsection ?nth }
"Concise way of extracting one of the first four elements:"
@ -1658,6 +1711,8 @@ $nl
"Using sequences for looping:"
{ $subsection "sequences-integers" }
{ $subsection "math.ranges" }
"Using sequences for control flow:"
{ $subsection "sequences-if" }
"For inner loops:"
{ $subsection "sequences-unsafe" } ;

View File

@ -46,9 +46,9 @@ PRIVATE>
: if-zero ( n quot1 quot2 -- )
[ dup zero? ] (if-empty) ; inline
: when-zero ( seq quot -- ) [ ] if-zero ; inline
: when-zero ( n quot -- ) [ ] if-zero ; inline
: unless-zero ( seq quot -- ) [ ] swap if-zero ; inline
: unless-zero ( n quot -- ) [ ] swap if-zero ; inline
: delete-all ( seq -- ) 0 swap set-length ;

View File

@ -62,9 +62,6 @@ PRIVATE>
: cartesian-product ( seq1 seq2 -- seq1xseq2 )
[ [ 2array ] with map ] curry map concat ;
: log10 ( m -- n )
log 10 log / ;
: mediant ( a/c b/d -- (a+b)/(c+d) )
2>fraction [ + ] 2bi@ / ;