Added division by zero detection
This commit is contained in:
15
src/ee.c
15
src/ee.c
@@ -1,4 +1,4 @@
|
||||
#include "ee.h"
|
||||
#include "ee.h"
|
||||
int error = 0;
|
||||
void printStack(STACK);
|
||||
|
||||
@@ -75,6 +75,11 @@ arrity(const char query)
|
||||
0;
|
||||
}
|
||||
|
||||
bool
|
||||
isDivByZero(double *_operands)
|
||||
{
|
||||
return _operands[0] == 0;
|
||||
}
|
||||
|
||||
double
|
||||
evaluate(STACK* target_stack, char operator)
|
||||
@@ -107,7 +112,10 @@ evaluate(STACK* target_stack, char operator)
|
||||
break;
|
||||
|
||||
case '/' :
|
||||
value = _operands[1] / _operands[0];
|
||||
if (isDivByZero(_operands))
|
||||
error |= ERR_DIV_BY_ZERO;
|
||||
else
|
||||
value = _operands[1] / _operands[0];
|
||||
break;
|
||||
|
||||
case '^' :
|
||||
@@ -187,6 +195,9 @@ report(STACK* expr, char* fmt)
|
||||
else if (error & ERR_INVALID_FACTORIAL)
|
||||
printf("Error: Factorials are only supported for positive integers.\n");
|
||||
|
||||
else if (error & ERR_DIV_BY_ZERO)
|
||||
printf("Undefined\n");
|
||||
|
||||
else
|
||||
printf(fmt, peek(expr));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user