Added division by zero detection

This commit is contained in:
Vera Lewis
2025-03-14 02:26:24 -05:00
parent d30d861c2b
commit 56edfa1fef
2 changed files with 15 additions and 2 deletions

View File

@@ -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));
}