// // MPPowerFunction.m // MathPad // // Created by Kim Wittenburg on 30.09.14. // Copyright (c) 2014 Kim Wittenburg. All rights reserved. // #import "MPPowerFunction.h" #import "MPExpression.h" @implementation MPPowerFunction MPFunctionAccessorImplementation(ExponentExpression, _exponentExpression) - (NSArray *)childrenAccessors { return @[@"exponentExpression"]; } - (BOOL)validate:(MPParseError *__autoreleasing *)error { if (!self.baseValue) { if (error) { *error = MPParseError(NSMakeRange([self.parent convertIndex:[self.parent indexOfElement:self] fromReferenceFrame:MPElementReferenceFrame toReferenceFrame:MPSymbolReferenceFrame], 0), @"No Base for Power"); } return NO; } return [self.baseValue validate:error] && [[self.exponentExpression parse] validate:error]; } - (NSDecimalNumber *)evaluate { NSDecimalNumber *base = [self.baseValue evaluate]; NSDecimalNumber *exponent = [[self.exponentExpression parse] evaluate]; return [[NSDecimalNumber alloc] initWithDouble:pow(base.doubleValue, exponent.doubleValue)]; } - (NSString *)description { return [NSString stringWithFormat:@"^%@", self.exponentExpression.description]; } @end