From 856c7188075a3e74ac4d9ef4dd665774f193d8d1 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sat, 24 Sep 2016 06:57:29 -0700 Subject: New functions: tofloatz and tointz. * arith.c (tofloatz, tointz): New functions. * arith.h (tofloatz, tointz): Declared. * eval.c (eval_init): Register tofloatz and tointz intrinsics. * txr.1: Documented new functions. --- arith.c | 16 ++++++++++++++++ arith.h | 2 ++ eval.c | 2 ++ txr.1 | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) diff --git a/arith.c b/arith.c index 5eb022ec..df8dbb5d 100644 --- a/arith.c +++ b/arith.c @@ -2297,6 +2297,22 @@ val toint(val obj, val base) } } +val tofloatz(val obj) +{ + uses_or2; + if (nilp(obj)) + return flo(0.0); + return or2(tofloat(obj), flo(0.0)); +} + +val tointz(val obj, val base) +{ + uses_or2; + if (nilp(obj)) + return zero; + return or2(toint(obj, base), zero); +} + val width(val obj) { switch (tag(obj)) { diff --git a/arith.h b/arith.h index 4e1bf5d2..532fb5ee 100644 --- a/arith.h +++ b/arith.h @@ -39,6 +39,8 @@ val n_choose_k(val n, val k); val n_perm_k(val n, val k); val tofloat(val obj); val toint(val obj, val base); +val tofloatz(val obj); +val tointz(val obj, val base); val width(val num); void arith_init(void); void arith_free_all(void); diff --git a/eval.c b/eval.c index cfc1f32c..08c24f1a 100644 --- a/eval.c +++ b/eval.c @@ -5239,6 +5239,8 @@ void eval_init(void) reg_fun(intern(lit("flo-int"), user_package), func_n1(flo_int)); reg_fun(intern(lit("tofloat"), user_package), func_n1(tofloat)); reg_fun(intern(lit("toint"), user_package), 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(intern(lit("less"), user_package), func_n1v(lessv)); reg_fun(intern(lit("greater"), user_package), func_n1v(greaterv)); reg_fun(intern(lit("lequal"), user_package), func_n1v(lequalv)); diff --git a/txr.1 b/txr.1 index 55be9bbc..71e3493a 100644 --- a/txr.1 +++ b/txr.1 @@ -29721,6 +29721,49 @@ Integers outside of this range are .code bignum integers. +.coNP Functions @ tofloatz and @ tointz +.synb +.mets (tofloatz << value ) +.mets (tointz < value <> [ radix ]) +.syne +.desc +These functions are closely related to, respectively, +.code tofloat +and +.codn toint . +They differ in that these functions return a floating-point +or integer zero, respectively, in some situations +in which those functions would return +.code nil +or throw an error. + +Whereas those functions reject a +.meta value +argument of +.codn nil , +for that same argument +.code tofloatz +function returns 0.0 and +.code tointz +returns 0. + +Likewise, in cases when +.code value +contains a string or character which cannot be +converted to a number, and +.code tofloat +and +.code toint +would return +.codn nil , +these functions return 0.0 and 0, respectively. + +In other situations, these functions behave +exactly like +.code tofloat +and +.codn toint . + .coNP Variables @, flo-min @ flo-max and @ flo-epsilon .desc These variables hold, respectively: the smallest positive floating-point -- cgit v1.2.3