Permalink
Browse files

allow expressions to test for inf and nan values

  • Loading branch information...
1 parent da4641b commit a9fa6571e2c069052722c3e661515ba45edcca14 @ktsaou ktsaou committed Aug 15, 2016
Showing with 21 additions and 7 deletions.
  1. +11 −2 profile/test-eval.c
  2. +10 −5 src/eval.c
View
@@ -259,6 +259,15 @@ void print_expression(EVAL_NODE *op, const char *failed_at, int error) {
}
*/
+int health_variable_lookup(const char *variable, uint32_t hash, RRDCALC *rc, calculated_number *result) {
+ (void)variable;
+ (void)hash;
+ (void)rc;
+ (void)result;
+
+ return 0;
+}
+
int main(int argc, char **argv) {
if(argc != 2) {
fprintf(stderr, "I need an epxression (enclose it in single-quotes (') as a single parameter)\n");
@@ -270,10 +279,10 @@ int main(int argc, char **argv) {
EVAL_EXPRESSION *exp = expression_parse(argv[1], &failed_at, &error);
if(!exp)
- printf("\nFAILED\nExpression: '%s'\nParsing stopped at: '%s'\nError code: %d (%s)\n", argv[1], (failed_at)?((*failed_at)?failed_at:"<END OF EXPRESSION>"):"<NONE>", error, expression_strerror(error));
+ printf("\nPARSING FAILED\nExpression: '%s'\nParsing stopped at: '%s'\nParsing error code: %d (%s)\n", argv[1], (failed_at)?((*failed_at)?failed_at:"<END OF EXPRESSION>"):"<NONE>", error, expression_strerror(error));
else {
- printf("\nOK\nExpression: '%s'\nParsed as : '%s'\nError code: %d (%s)\n", argv[1], exp->parsed_as, error, expression_strerror(error));
+ printf("\nPARSING OK\nExpression: '%s'\nParsed as : '%s'\nParsing error code: %d (%s)\n", argv[1], exp->parsed_as, error, expression_strerror(error));
if(expression_evaluate(exp)) {
printf("\nEvaluates to: %Lf\n\n", exp->result);
View
@@ -119,7 +119,8 @@ static inline calculated_number eval_value(EVAL_EXPRESSION *exp, EVAL_VALUE *v,
break;
}
- return eval_check_number(n, error);
+ // return eval_check_number(n, error);
+ return n;
}
calculated_number eval_and(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
@@ -212,7 +213,8 @@ static inline calculated_number eval_node(EVAL_EXPRESSION *exp, EVAL_NODE *op, i
calculated_number n = operators[op->operator].eval(exp, op, error);
- return eval_check_number(n, error);
+ // return eval_check_number(n, error);
+ return n;
}
// ----------------------------------------------------------------------------
@@ -225,12 +227,12 @@ static inline void print_parsed_as_variable(BUFFER *out, EVAL_VARIABLE *v, int *
static inline void print_parsed_as_constant(BUFFER *out, calculated_number n) {
if(unlikely(isnan(n))) {
- buffer_strcat(out, "NaN");
+ buffer_strcat(out, "nan");
return;
}
if(unlikely(isinf(n))) {
- buffer_strcat(out, "INFINITE");
+ buffer_strcat(out, "inf");
return;
}
@@ -572,7 +574,7 @@ static inline int parse_variable(const char **string, char *buffer, size_t len)
static inline int parse_constant(const char **string, calculated_number *number) {
char *end = NULL;
calculated_number n = strtold(*string, &end);
- if(unlikely(!end || *string == end || isnan(n) || isinf(n))) {
+ if(unlikely(!end || *string == end)) {
*number = 0;
return 0;
}
@@ -844,6 +846,9 @@ int expression_evaluate(EVAL_EXPRESSION *exp) {
buffer_reset(exp->error_msg);
exp->result = eval_node(exp, (EVAL_NODE *)exp->nodes, &exp->error);
+ if(exp->error == EVAL_ERROR_OK)
+ exp->result = eval_check_number(exp->result, &exp->error);
+
if(exp->error != EVAL_ERROR_OK) {
exp->result = NAN;

0 comments on commit a9fa657

Please sign in to comment.