Archived
1
This repository has been archived on 2022-08-08. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
mathpad/MathPad/MPPowerFunction.m
Kim Wittenburg 91e7dbe9f2 Cleaned Code
2014-10-21 15:06:57 +02:00

51 lines
1.5 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 "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];
if ([base isEqualToNumber:@(0)] && [exponent isEqualToNumber:@(0)]) {
// The C pow function returns 1 for pow(0, 0). Mathematically this should be undefined.
return [NSDecimalNumber notANumber];
}
return [[NSDecimalNumber alloc] initWithDouble:pow(base.doubleValue, exponent.doubleValue)];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"^%@", self.exponentExpression.description];
}
@end