diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2023-08-14 20:56:54 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2023-08-14 20:56:54 -0700 |
commit | 725112ef78d8c6e655db4a99fc69e54d0ed1eb5a (patch) | |
tree | 89b456d55b28fb07a41483aa838555ee3035c1a5 /arith.c | |
parent | f94ae27b4021b61eabe50d3b088b8d8917bfdfe8 (diff) | |
download | txr-725112ef78d8c6e655db4a99fc69e54d0ed1eb5a.tar.gz txr-725112ef78d8c6e655db4a99fc69e54d0ed1eb5a.tar.bz2 txr-725112ef78d8c6e655db4a99fc69e54d0ed1eb5a.zip |
math: tofloat and toint in user-defined arithmetic.
* arith.c (tofloat_s, toint_s): New symbol variables.
(tofloat, toint): If the argument is a COBJ, handle
it via do_unary_method.
(arith_init): Initialize new symbol variables.
The functions tofloat, toint, tofloatz and tointz.
are now registered here, rather than eval_init.
* eval.c (eval_init): Remove registrations of tofloat,
toint, tofloatz and tointz.
* tests/016/ud-arith.tl: New tests.
* txr.1: Documented.
* stdlib/doc-syms.tl: Updated.
Diffstat (limited to 'arith.c')
-rw-r--r-- | arith.c | 19 |
1 files changed, 17 insertions, 2 deletions
@@ -84,6 +84,8 @@ val r_copysign_s, r_drem_s, r_fdim_s, r_fmax_s, r_fmin_s, r_hypot_s; val r_jn_s, r_ldexp_s, r_nextafter_s, r_remainder_s, r_scalb_s; val r_scalbln_s, r_yn_s; +val tofloat_s, toint_s; + val make_bignum(void) { val n = make_obj(); @@ -4358,6 +4360,8 @@ val n_perm_k(val n, val k) val tofloat(val obj) { + val self = tofloat_s; + switch (tag(obj)) { case TAG_NUM: return flo_int(obj); @@ -4385,13 +4389,16 @@ val tofloat(val obj) } /* fallthrough */ default: - uw_throwf(error_s, lit("tofloat: ~s is not convertible to float"), obj, nao); + if (type(obj) == COBJ) + return do_unary_method(self, self, obj); + uw_throwf(error_s, lit("~s: ~s is not convertible to float"), + self, obj, nao); } } val toint(val obj, val base) { - val self = lit("toint"); + val self = toint_s; switch (tag(obj)) { case TAG_NUM: @@ -4429,6 +4436,8 @@ val toint(val obj, val base) } /* fallthrough */ default: + if (type(obj) == COBJ) + return do_unary_method(self, self, obj); uw_throwf(error_s, lit("~a: ~s is not convertible to integer"), self, obj, nao); } @@ -5366,6 +5375,8 @@ void arith_init(void) width_s = intern(lit("width"), user_package); bitset_s = intern(lit("bitset"), user_package); logcount_s = intern(lit("logcount"), user_package); + tofloat_s = intern(lit("tofloat"), user_package); + toint_s = intern(lit("toint"), user_package); reg_varl(intern(lit("flo-dig"), user_package), num_fast(DBL_DIG)); reg_varl(intern(lit("flo-max-dig"), user_package), num_fast(FLO_MAX_DIG)); @@ -5384,6 +5395,10 @@ void arith_init(void) #endif reg_varl(intern(lit("%e%"), user_package), flo(M_E)); + reg_fun(tofloat_s, func_n1(tofloat)); + reg_fun(toint_s, func_n2o(toint, 1)); + reg_fun(intern(lit("tofloatz"), user_package), func_n1(tofloatz)); + reg_fun(intern(lit("tointz"), user_package), func_n2o(tointz, 1)); reg_fun(plus_s, func_n0v(plusv)); reg_fun(minus_s, func_n1v(minusv)); reg_fun(mul_s, func_n0v(mulv)); |