42 lines
1.1 KiB
Objective-C
42 lines
1.1 KiB
Objective-C
//
|
|
// MPPowerFunction.m
|
|
// MathPad
|
|
//
|
|
// Created by Kim Wittenburg on 30.09.14.
|
|
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
|
//
|
|
|
|
#import "MPPowerFunction.h"
|
|
#import "MPExpressionEvaluator.h"
|
|
|
|
@implementation MPPowerFunction
|
|
|
|
MPFunctionAccessorImplementation(ExponentExpression, _exponentExpression)
|
|
|
|
- (NSArray *)childrenAccessors
|
|
{
|
|
return @[@"exponentExpression"];
|
|
}
|
|
|
|
- (MPTerm *)parseWithError:(MPParseError *__autoreleasing *)error
|
|
{
|
|
MPExpressionEvaluator *exponent = [[MPExpressionEvaluator alloc] initWithExpression:self.exponentExpression];
|
|
MPTerm *exponentTerm = [exponent parseExpectingVariable:NO
|
|
error:error];
|
|
if (exponentTerm == nil) {
|
|
return nil;
|
|
}
|
|
|
|
return [[MPTerm alloc] initWithBlock:^NSDecimalNumber *{
|
|
double power = pow([self.baseTerm evaluate].doubleValue, [exponentTerm evaluate].doubleValue);
|
|
return [[NSDecimalNumber alloc] initWithDouble:power];
|
|
}];
|
|
}
|
|
|
|
- (NSString *)description
|
|
{
|
|
return [NSString stringWithFormat:@"^%@", self.exponentExpression.description];
|
|
}
|
|
|
|
@end
|