diff --git a/CHANGES b/CHANGES index dadc6a3..87c9a96 100644 --- a/CHANGES +++ b/CHANGES @@ -86,6 +86,9 @@ The following are the changes from calc version 2.14.3.5 to date: The len element in a ZVALUE is of type LEN. LEN type is SB32 when MAJOR_VER < 3, or a uintptr_t otherwise. + Setting an invalid epsilon via the epsilon(value) or confiv("epsilon", + value) triggers an error. The epsilon value must be: 0 < epsilon < 1. + The following are the changes from calc version 2.14.3.4 to 2.14.3.5: diff --git a/qfunc.c b/qfunc.c index 38fc89a..e304b49 100644 --- a/qfunc.c +++ b/qfunc.c @@ -43,6 +43,29 @@ STATIC long E_num; #define QALLOCNUM 64 + +/* + * verify_epsilon - verify that 0 < epsilon < 1 + * + * This function is called via the OP_SETEPSILON op code, config("epsilon", + * and from various builtin functions that take an epsilon argument. + * + * If all is well, this function just returns. If the arg passed is + * out of range, then a math_error() is triggered causing this function + * to not return. + */ +void +verify_epsilon(NUMBER *q) +{ + /* verify that 0 < epsilon < 1 */ + if (q == NULL || qisneg(q) || qiszero(q) || qisone(q) || qreli(q, 1) > 0) { + math_error("Invalid value for epsilon: must be: 0 < epsilon < 1"); + not_reached(); + } + return; +} + + /* * Set the default epsilon for approximate calculations. * This must be greater than zero. @@ -55,10 +78,7 @@ setepsilon(NUMBER *q) { NUMBER *old; - if (qisneg(q) || qiszero(q)) { - math_error("Epsilon value must be greater than zero"); - not_reached(); - } + verify_epsilon(q); old = conf->epsilon; conf->epsilonprec = qprecision(q); conf->epsilon = qlink(q); diff --git a/qmath.h b/qmath.h index eb32f3b..16cca81 100644 --- a/qmath.h +++ b/qmath.h @@ -167,6 +167,7 @@ E_FUNC long qprecision(NUMBER *q); E_FUNC long qplaces(NUMBER *q, ZVALUE base); E_FUNC long qdecplaces(NUMBER *q); E_FUNC long qdigits(NUMBER *q, ZVALUE base); +E_FUNC void verify_epsilon(NUMBER *q); E_FUNC void setepsilon(NUMBER *q); E_FUNC NUMBER *qbitvalue(long i); E_FUNC NUMBER *qtenpow(long i);