diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2012-03-29 15:57:45 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2012-03-29 15:57:45 -0700 |
commit | 2b1e05769d01cb036cf0a82231eb87b698a33426 (patch) | |
tree | d072354049911ec058a01b9cc69f41e2ca2ebe2d | |
parent | 9e60a829fdd3874f510ebc57d1a378d020a55fb9 (diff) | |
download | txr-2b1e05769d01cb036cf0a82231eb87b698a33426.tar.gz txr-2b1e05769d01cb036cf0a82231eb87b698a33426.tar.bz2 txr-2b1e05769d01cb036cf0a82231eb87b698a33426.zip |
* arith.c (gcd): Allow zeros. Don't issue "non-integral"
exception if MPI fails.
(floorf, ceili): Map integer argument to itself.
(tang, asine, acosi): New functions.
* eval.c (eval_init): New intrinsics: tan, asin, acos.
* lib.h (tang, asine, acosi): Declared.
* txr.1: Documented gcd, abs, floor, ceil, sin, cos, tan
asin, acos, atan, log, and exp.
* txr.vim: Highlighting for tang, asine, acosi.
-rw-r--r-- | ChangeLog | 16 | ||||
-rw-r--r-- | arith.c | 27 | ||||
-rw-r--r-- | eval.c | 3 | ||||
-rw-r--r-- | lib.h | 3 | ||||
-rw-r--r-- | txr.1 | 93 | ||||
-rw-r--r-- | txr.vim | 2 |
6 files changed, 141 insertions, 3 deletions
@@ -1,5 +1,21 @@ 2012-03-29 Kaz Kylheku <kaz@kylheku.com> + * arith.c (gcd): Allow zeros. Don't issue "non-integral" + exception if MPI fails. + (floorf, ceili): Map integer argument to itself. + (tang, asine, acosi): New functions. + + * eval.c (eval_init): New intrinsics: tan, asin, acos. + + * lib.h (tang, asine, acosi): Declared. + + * txr.1: Documented gcd, abs, floor, ceil, sin, cos, tan + asin, acos, atan, log, and exp. + + * txr.vim: Highlighting for tang, asine, acosi. + +2012-03-29 Kaz Kylheku <kaz@kylheku.com> + * arith.c (dmod): New static function. (mod): Use dmod instead of fmod directly, to calculate the correct semantics for combinations of @@ -1283,6 +1283,9 @@ val gcd(val anum, val bnum) if (!integerp(anum) || !integerp(bnum)) goto inval; + if (zerop(anum)) + return zero; + if (fixnump(anum)) anum = bignum(c_num(anum)); @@ -1292,21 +1295,28 @@ val gcd(val anum, val bnum) n = make_bignum(); if (mp_gcd(mp(anum), mp(bnum), mp(n)) != MP_OKAY) - goto inval; + goto bad; return n; inval: uw_throwf(error_s, lit("gcd: non-integral operands ~s ~s"), anum, bnum, nao); +bad: + uw_throwf(error_s, lit("gcd: operation failed on ~s ~s"), + anum, bnum, nao); } val floorf(val num) { + if (integerp(num)) + return num; return flo(floor(c_flo(to_float(lit("floor"), num)))); } val ceili(val num) { + if (integerp(num)) + return num; return flo(ceil(c_flo(to_float(lit("ceil"), num)))); } @@ -1320,6 +1330,21 @@ val cosi(val num) return flo(cos(c_flo(to_float(lit("cos"), num)))); } +val tang(val num) +{ + return flo(tan(c_flo(to_float(lit("tan"), num)))); +} + +val asine(val num) +{ + return flo(asin(c_flo(to_float(lit("asin"), num)))); +} + +val acosi(val num) +{ + return flo(acos(c_flo(to_float(lit("acos"), num)))); +} + val atang(val num) { return flo(atan(c_flo(to_float(lit("atan"), num)))); @@ -2191,6 +2191,9 @@ void eval_init(void) reg_fun(intern(lit("ceil"), user_package), func_n1(ceili)); reg_fun(intern(lit("sin"), user_package), func_n1(sine)); reg_fun(intern(lit("cos"), user_package), func_n1(cosi)); + reg_fun(intern(lit("tan"), user_package), func_n1(tang)); + reg_fun(intern(lit("asin"), user_package), func_n1(asine)); + reg_fun(intern(lit("acos"), user_package), func_n1(acosi)); reg_fun(intern(lit("atan"), user_package), func_n1(atang)); reg_fun(intern(lit("log"), user_package), func_n1(loga)); reg_fun(intern(lit("exp"), user_package), func_n1(expo)); @@ -431,6 +431,9 @@ val floorf(val); val ceili(val); val sine(val); val cosi(val); +val tang(val); +val asine(val); +val acosi(val); val atang(val); val loga(val); val expo(val); @@ -6854,9 +6854,100 @@ once, with a "remainder" of 0.25. .SS Arithmetic function gcd +.TP +Syntax: + + (gcd <left> <right>) + +.TP +Description: + +The gcd function computes the greatest common divisor: the largest positive +integer which divides both arguments. + +Operands <left> and <right> must be integers, or else an exception is thrown. + +The value of (gcd 0 x) is 0 for all x, including 0. + +The value of (gcd x 123) is is (abs x) for all x. + +Negative operands are permitted; this operation effectivelly ignores sign, so +that the value of (gcd x y) is the same as (gcd (abs x) (abs y)) for all +x and y. + .SS Arithmetic function abs -.SS Arithmetic functions floor, ceil, sin, cos, atan, log, exp +.TP +Syntax: + + (abs <number>) + +.TP +Description: + +The abs function computes the absolute value of the given number. If the number +is positive, it is returned. If the number is negative, its additive inverse is +returned: a positive number of the same type with exactly the same magnitude. + +.SS Arithmetic functions floor, ceil + +.TP +Syntax: + + (floor <number>) + (ceil <number>) + +.TP +Description: + +The floor function returns the highest integer which does not exceed +the argument. The ceiling function returns the lowest integer which does +not exceed the argument. + +If the argument is an integer, it is simply returned. + +If the argument is a float, then the value returned is a float. +For instance (floor 1.1) returns 1.0. + +.SS Arithmetic functions sin, cos, tan, asin, acos, atan + +.TP +Syntax: + + (sin <radians>) + (cos <radians>) + (tan <radians>) + (atan <slope>) + (asin <num>) + (acos <num>) + +.TP +Description: + +These trigonometric functions convert their argument to floating point and +return a float result. The sin, cos and tan functions compute the sine and +cosine and tangent of the argument. The argument represents an angle expressed +in radians. The atan, acos and asin are their respective inverse functions. +The argument to asin and acos must be in the range -1.0 to 1.0. + +.SS Arithmetic functions log, exp + +.TP +Syntax: + + (exp <number>) + (log <number>) + +.TP +Description: + +The exp function calculates the value of the transcendental number e raised to +the specified exponent. + +The log function calculates the base e logarithm of its argument, which must +be a positive value. + +Integer arguments are converted to floats. .SS Arithmetic functions expt, sqrt, isqrt @@ -44,7 +44,7 @@ syn keyword txl_keyword contained memq memql memqual tree-find some syn keyword txl_keyword contained remq remql remqual syn keyword txl_keyword contained all none eq eql equal + - * / abs trunc mod syn keyword txl_keyword contained expt exptmod sqrt isqrt gcd -syn keyword txl_keyword contained floor ceil sin cos atan log exp +syn keyword txl_keyword contained floor ceil sin cos tan asin acos atan log exp syn keyword txl_keyword contained fixnump bignump integerp floatp syn keyword txl_keyword contained numberp zerop evenp oddp > syn keyword txl_keyword contained zerop evenp oddp > < >= <= max min |