45 lines
1.2 KiB
Objective-C
45 lines
1.2 KiB
Objective-C
//
|
|
// MPRootFunction.m
|
|
// MathPad
|
|
//
|
|
// Created by Kim Wittenburg on 22.10.14.
|
|
// Copyright (c) 2014 Kim Wittenburg. All rights reserved.
|
|
//
|
|
|
|
#import "MPRootFunction.h"
|
|
|
|
#import "MPExpression.h"
|
|
#import "MPExpressionTree.h"
|
|
|
|
@implementation MPRootFunction
|
|
|
|
MPFunctionAccessorImplementation(ExponentExpression, _exponentExpression)
|
|
MPFunctionAccessorImplementation(RadicantExpression, _radicantExpression)
|
|
|
|
- (NSArray *)childrenAccessors
|
|
{
|
|
return @[@"exponentExpression", @"radicantExpression"];
|
|
}
|
|
|
|
- (BOOL)validate:(NSError *__autoreleasing *)error
|
|
{
|
|
return [[self.exponentExpression parse] validate:error] && [[self.radicantExpression parse] validate:error];
|
|
}
|
|
|
|
- (NSDecimalNumber *)evaluate
|
|
{
|
|
NSDecimalNumber *exponent = [[self.exponentExpression parse] evaluate];
|
|
NSDecimalNumber *radicant = [[self.radicantExpression parse] evaluate];
|
|
return [[NSDecimalNumber alloc] initWithDouble:pow(radicant.doubleValue, exponent.doubleValue)];
|
|
}
|
|
|
|
- (NSString *)description
|
|
{
|
|
if (self.exponentExpression == nil) {
|
|
return [NSString stringWithFormat:@"√%@", self.radicantExpression.description];
|
|
}
|
|
return [NSString stringWithFormat:@"pow(%@; %@)", self.radicantExpression.description, self.exponentExpression];
|
|
}
|
|
|
|
@end
|