improve check for invalid epsilon

We added check_epsilon(NUMBER *q) so that later, builtin
functions can check the eps value as well.
This commit is contained in:
Landon Curt Noll
2023-08-31 03:32:49 -07:00
parent b95a62c14e
commit 4787199462
2 changed files with 24 additions and 3 deletions

26
qfunc.c
View File

@@ -47,8 +47,28 @@ STATIC long E_num;
/*
* 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.
* given:
* q epsilon or eps argument
*
* returns:
* false q is NULL or q <= 0 or q >= 1
* true 0 < q < 1
*/
bool
check_epsilon(NUMBER *q)
{
/* verify that 0 < epsilon < 1 */
if (q == NULL || qisneg(q) || qiszero(q) || qisone(q) || qreli(q, 1) > 0) {
return false;
}
return true;
}
/*
* verify_epsilon - verify that 0 < epsilon < 1
*
* This function is called via the OP_SETEPSILON op code, config("epsilon").
*
* 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
@@ -58,7 +78,7 @@ void
verify_epsilon(NUMBER *q)
{
/* verify that 0 < epsilon < 1 */
if (q == NULL || qisneg(q) || qiszero(q) || qisone(q) || qreli(q, 1) > 0) {
if (check_epsilon(q) == false) {
math_error("Invalid value for epsilon: must be: 0 < epsilon < 1");
not_reached();
}

View File

@@ -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 bool check_epsilon(NUMBER *q);
E_FUNC void verify_epsilon(NUMBER *q);
E_FUNC void setepsilon(NUMBER *q);
E_FUNC NUMBER *qbitvalue(long i);