Archived
1

Add Root Function and Product Function

This commit is contained in:
Kim Wittenburg
2017-08-31 00:04:21 +02:00
parent 43b907ba88
commit 3681e1416c
18 changed files with 864 additions and 0 deletions

44
MathKit/MPRootTerm.m Executable file
View File

@@ -0,0 +1,44 @@
//
// MPRootTerm.m
// MathPad
//
// Created by Kim Wittenburg on 11.01.15.
// Copyright (c) 2015 Kim Wittenburg. All rights reserved.
//
#import "MPRootTerm.h"
#import "MPParsedExpression.h"
@implementation MPRootTerm
- (NSDecimalNumber *)doEvaluation:(NSError *__autoreleasing *)error
{
MPEvaluateExpression(exponent, 0);
MPEvaluateExpression(root, 1);
if ([root compare:[NSDecimalNumber zero]] < 0) {
// There are no negative roots.
if (error) {
*error = [NSError errorWithDomain:MPMathKitErrorDomain
code:101
userInfo:@{NSLocalizedDescriptionKey: NSLocalizedString(@"The root of a negative number is undefined.", nil)}];
}
return nil;
}
if ([exponent isEqualToNumber:@(0)]) {
// The C pow function returns 1 for pow(0, 0). Mathematically this should be undefined.
if (error) {
*error = [NSError errorWithDomain:MPMathKitErrorDomain
code:101
userInfo:@{NSLocalizedDescriptionKey: NSLocalizedString(@"The 0th root is undefined.", nil)}];
}
return nil;
}
NSDecimalNumber *actualExponent = [[NSDecimalNumber one] decimalNumberByDividingBy:exponent];
return [[NSDecimalNumber alloc] initWithDouble:pow(root.doubleValue, actualExponent.doubleValue)];
}
@end