I've been working on my math parser a bit, and I have come to realize that a bit of code I'm using is unable to handle an exponent that is non-interger. The bit of code I'm using seems to work just fine with an int
, but not with a double
.
else if ([[token stringValue] isEqualToString: @"^"])
{
NSLog(@"^");
double exponate = [[self popOperand] intValue];
double base = [[self popOperand] doubleValue];
result = base;
exponate--;
while (exponate)
{
result *= base;
exponate--;
}
[self.operandStack addObject: [NSNumber numberWithDouble: result]];
}
' Using Objective-C, how do I make something like 5^5.5 evaluate properly? (6987.71242969)
Let library code do the work for you:
double exponent = [[self popOperand] doubleValue];
double base = [[self popOperand] doubleValue];
[self.operandStack addObject:@(pow(base, exponent))];