2013-07-24 23:59:18 +00:00
|
|
|
/*
|
|
|
|
abCalcOpPower.c
|
|
|
|
By: Jeremy Rand
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "abCalcOpPower.h"
|
|
|
|
|
|
|
|
#include "abCalcOp.h"
|
|
|
|
#include "abCalcError.h"
|
|
|
|
#include "abCalcExpr.h"
|
2013-07-25 02:28:20 +00:00
|
|
|
#include "abCalcExpReal.h"
|
2013-07-24 23:59:18 +00:00
|
|
|
#include "abCalcStack.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define OP_NAME "^"
|
|
|
|
|
|
|
|
|
|
|
|
static void powerExecute(void);
|
|
|
|
|
|
|
|
|
|
|
|
void abCalcOpPowerInit(void)
|
|
|
|
{
|
|
|
|
abCalcOpRegister(OP_NAME, powerExecute);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void powerExecute(void)
|
|
|
|
{
|
|
|
|
abCalcExpr result;
|
|
|
|
double integral;
|
|
|
|
AB_CALC_OP_TWO_ARGS(OP_NAME);
|
|
|
|
|
|
|
|
if (expr1->type != abCalcExprTypeReal) {
|
|
|
|
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expr2->type != abCalcExprTypeReal) {
|
|
|
|
abCalcRaiseError(abCalcBadArgTypeError, OP_NAME);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expr2->u.real == 0.0) {
|
|
|
|
if (expr1->u.real < 0.0) {
|
|
|
|
abCalcRaiseError(abCalcInfiniteResultError, OP_NAME);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (expr1->u.real == 0.0) {
|
2013-07-25 02:28:20 +00:00
|
|
|
abCalcExprRealSet(&result, 1.0);
|
2013-07-24 23:59:18 +00:00
|
|
|
} else {
|
2013-07-25 02:28:20 +00:00
|
|
|
abCalcExprRealSet(&result, 0.0);
|
2013-07-24 23:59:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (expr2->u.real < 0.0) {
|
|
|
|
modf(expr1->u.real, &integral);
|
|
|
|
if (expr1->u.real != integral) {
|
|
|
|
abCalcRaiseError(abCalcComplexResultError, OP_NAME);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-07-25 02:28:20 +00:00
|
|
|
|
|
|
|
abCalcExprRealSet(&result, pow(expr2->u.real, expr1->u.real));
|
2013-07-24 23:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
abCalcStackExprPop(NULL);
|
|
|
|
abCalcStackExprPop(NULL);
|
|
|
|
abCalcStackExprPush(&result);
|
|
|
|
}
|