Skip to content
Browse files

DDMathEvaluator.angleMeasurementMode. Fixes issue #29.

DDMathEvaluator has a new property to specify that the arguments to the trigonometric functions should be interpreted as being in degrees versus radians (the default).
  • Loading branch information...
1 parent 3351b50 commit 29073d7b4e24da15c84374810327750e5a9725d8 @davedelong committed
View
2 Command line demo/main.m
@@ -83,6 +83,8 @@ int main (int argc, const char * argv[]) {
} while (1);
+ DD_RELEASE(evaluator);
+
printf("Goodbye!\n");
#if DD_HAS_ARC
View
1 DDMathParser/DDMathEvaluator+Private.h
@@ -13,6 +13,7 @@
@interface DDMathEvaluator ()
- (DDMathFunction) functionWithName:(NSString *)functionName;
+- (id) variableWithName:(NSString *)variableName;
- (BOOL) functionExpressionFailedToResolve:(_DDFunctionExpression *)functionExpression error:(NSError **)error;
View
5 DDMathParser/DDMathEvaluator.h
@@ -13,15 +13,20 @@
@class DDExpression;
typedef DDMathFunction (^DDFunctionResolver)(NSString *);
+typedef NSNumber* (^DDVariableResolver)(NSString *);
@interface DDMathEvaluator : NSObject {
NSMutableArray *functions;
NSMutableDictionary * functionMap;
NSMutableArray *rewriteRules;
DDFunctionResolver functionResolver;
+ DDVariableResolver variableResolver;
+ DDAngleMeasurementMode angleMeasurementMode;
}
+@property (nonatomic) DDAngleMeasurementMode angleMeasurementMode; // default is Radians
@property (nonatomic, copy) DDFunctionResolver functionResolver;
+@property (nonatomic, copy) DDVariableResolver variableResolver;
+ (id) sharedMathEvaluator;
View
12 DDMathParser/DDMathEvaluator.m
@@ -30,7 +30,9 @@ - (_DDFunctionContainer *)functionContainerWithName:(NSString *)functionName;
@implementation DDMathEvaluator
+@synthesize angleMeasurementMode=angleMeasurementMode;
@synthesize functionResolver=functionResolver;
+@synthesize variableResolver=variableResolver;
static DDMathEvaluator * _sharedEvaluator = nil;
@@ -47,6 +49,7 @@ - (id) init {
if (self) {
functions = [[NSMutableArray alloc] init];
functionMap = [[NSMutableDictionary alloc] init];
+ angleMeasurementMode = DDAngleMeasurementModeRadians;
[self _registerStandardFunctions];
}
@@ -62,6 +65,7 @@ - (void) dealloc {
[functionMap release];
[rewriteRules release];
[functionResolver release];
+ [variableResolver release];
[super dealloc];
#endif
}
@@ -126,6 +130,14 @@ - (BOOL) functionExpressionFailedToResolve:(_DDFunctionExpression *)functionExpr
return NO;
}
+- (id) variableWithName:(NSString *)variableName {
+ id value = nil;
+ if (variableResolver != nil) {
+ value = variableResolver(variableName);
+ }
+ return value;
+}
+
- (BOOL) addAlias:(NSString *)alias forFunctionName:(NSString *)functionName {
//we can't add an alias for a function that already exists
DDMathFunction function = [self functionWithName:alias];
View
2 DDMathParser/DDMathStringToken.m
@@ -78,7 +78,7 @@ - (NSString *)debugDescription {
[d appendFormat:@"arity:%@, ", arityNames[arity]];
NSInteger precedence = [self operatorPrecedence];
- [d appendFormat:@"precedence:%d, ", precedence];
+ [d appendFormat:@"precedence:%ld, ", precedence];
DDOperatorAssociativity assoc = [self operatorAssociativity];
NSString *assocNames[2] = { @"LEFT", @"RIGHT" };
View
7 DDMathParser/DDTypes.h
@@ -49,4 +49,9 @@ typedef enum {
DDErrorCodeInvalidNumberOfArguments, //@"random() may only have up to 2 arguments"
DDErrorCodeInvalidArgument, //@"upper bound (%ld) of random() must be larger than lower bound (%ld)"
-} DDErrorCode;
+} DDErrorCode;
+
+typedef enum {
+ DDAngleMeasurementModeRadians,
+ DDAngleMeasurementModeDegrees
+} DDAngleMeasurementMode;
View
144 DDMathParser/_DDFunctionUtilities.m
@@ -33,6 +33,34 @@
#define RETURN_IF_NIL(_n) if ((_n) == nil) { return nil; }
+
+
+static inline DDExpression* _DDDTOR(DDExpression *e, DDMathEvaluator *evaluator, NSError **error) {
+ DDExpression *final = e;
+ if ([evaluator angleMeasurementMode] == DDAngleMeasurementModeDegrees) {
+ if ([e expressionType] != DDExpressionTypeFunction || ![[e function] isEqualToString:@"dtor"]) {
+ final = [DDExpression functionExpressionWithFunction:@"dtor"
+ arguments:[NSArray arrayWithObject:e]
+ error:error];
+ }
+ }
+ return final;
+}
+
+
+
+static inline DDExpression* _DDRTOD(DDExpression *e, DDMathEvaluator *evaluator, NSError **error) {
+ DDExpression *final = e;
+ if ([evaluator angleMeasurementMode] == DDAngleMeasurementModeDegrees) {
+ if ([e expressionType] != DDExpressionTypeFunction || ![[e function] isEqualToString:@"rtod"]) {
+ final = [DDExpression functionExpressionWithFunction:@"rtod"
+ arguments:[NSArray arrayWithObject:e]
+ error:error];
+ }
+ }
+ return final;
+}
+
@implementation _DDFunctionUtilities
+ (DDMathFunction) addFunction {
@@ -548,7 +576,9 @@ + (DDMathFunction) percentFunction {
+ (DDMathFunction) sinFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:sin([n doubleValue])];
@@ -560,7 +590,9 @@ + (DDMathFunction) sinFunction {
+ (DDMathFunction) cosFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:cos([n doubleValue])];
@@ -572,7 +604,9 @@ + (DDMathFunction) cosFunction {
+ (DDMathFunction) tanFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:tan([n doubleValue])];
@@ -588,7 +622,7 @@ + (DDMathFunction) asinFunction {
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:asin([n doubleValue])];
- return [DDExpression numberExpressionWithNumber:result];
+ return _DDRTOD([DDExpression numberExpressionWithNumber:result], evaluator, error);
};
return DD_AUTORELEASE([function copy]);
}
@@ -600,7 +634,7 @@ + (DDMathFunction) acosFunction {
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:acos([n doubleValue])];
- return [DDExpression numberExpressionWithNumber:result];
+ return _DDRTOD([DDExpression numberExpressionWithNumber:result], evaluator, error);
};
return DD_AUTORELEASE([function copy]);
}
@@ -612,7 +646,7 @@ + (DDMathFunction) atanFunction {
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:atan([n doubleValue])];
- return [DDExpression numberExpressionWithNumber:result];
+ return _DDRTOD([DDExpression numberExpressionWithNumber:result], evaluator, error);
};
return DD_AUTORELEASE([function copy]);
}
@@ -620,7 +654,9 @@ + (DDMathFunction) atanFunction {
+ (DDMathFunction) sinhFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:sinh([n doubleValue])];
@@ -632,7 +668,9 @@ + (DDMathFunction) sinhFunction {
+ (DDMathFunction) coshFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:cosh([n doubleValue])];
@@ -644,7 +682,9 @@ + (DDMathFunction) coshFunction {
+ (DDMathFunction) tanhFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:tanh([n doubleValue])];
@@ -660,7 +700,7 @@ + (DDMathFunction) asinhFunction {
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:asinh([n doubleValue])];
- return [DDExpression numberExpressionWithNumber:result];
+ return _DDRTOD([DDExpression numberExpressionWithNumber:result], evaluator, error);
};
return DD_AUTORELEASE([function copy]);
}
@@ -672,7 +712,7 @@ + (DDMathFunction) acoshFunction {
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:acosh([n doubleValue])];
- return [DDExpression numberExpressionWithNumber:result];
+ return _DDRTOD([DDExpression numberExpressionWithNumber:result], evaluator, error);
};
return DD_AUTORELEASE([function copy]);
}
@@ -684,7 +724,7 @@ + (DDMathFunction) atanhFunction {
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:atanh([n doubleValue])];
- return [DDExpression numberExpressionWithNumber:result];
+ return _DDRTOD([DDExpression numberExpressionWithNumber:result], evaluator, error);
};
return DD_AUTORELEASE([function copy]);
}
@@ -692,7 +732,9 @@ + (DDMathFunction) atanhFunction {
+ (DDMathFunction) cscFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:1/sin([n doubleValue])];
@@ -704,7 +746,9 @@ + (DDMathFunction) cscFunction {
+ (DDMathFunction) secFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:1/cos([n doubleValue])];
@@ -716,7 +760,9 @@ + (DDMathFunction) secFunction {
+ (DDMathFunction) cotanFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:1/tan([n doubleValue])];
@@ -732,7 +778,7 @@ + (DDMathFunction) acscFunction {
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:1/asin([n doubleValue])];
- return [DDExpression numberExpressionWithNumber:result];
+ return _DDRTOD([DDExpression numberExpressionWithNumber:result], evaluator, error);
};
return DD_AUTORELEASE([function copy]);
}
@@ -744,7 +790,7 @@ + (DDMathFunction) asecFunction {
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:1/acos([n doubleValue])];
- return [DDExpression numberExpressionWithNumber:result];
+ return _DDRTOD([DDExpression numberExpressionWithNumber:result], evaluator, error);
};
return DD_AUTORELEASE([function copy]);
}
@@ -756,7 +802,7 @@ + (DDMathFunction) acotanFunction {
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:1/atan([n doubleValue])];
- return [DDExpression numberExpressionWithNumber:result];
+ return _DDRTOD([DDExpression numberExpressionWithNumber:result], evaluator, error);
};
return DD_AUTORELEASE([function copy]);
}
@@ -764,7 +810,9 @@ + (DDMathFunction) acotanFunction {
+ (DDMathFunction) cschFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:1/sinh([n doubleValue])];
@@ -776,7 +824,9 @@ + (DDMathFunction) cschFunction {
+ (DDMathFunction) sechFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:1/cosh([n doubleValue])];
@@ -788,7 +838,9 @@ + (DDMathFunction) sechFunction {
+ (DDMathFunction) cotanhFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:1/tanh([n doubleValue])];
@@ -804,7 +856,7 @@ + (DDMathFunction) acschFunction {
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:1/sinh([n doubleValue])];
- return [DDExpression numberExpressionWithNumber:result];
+ return _DDRTOD([DDExpression numberExpressionWithNumber:result], evaluator, error);
};
return DD_AUTORELEASE([function copy]);
}
@@ -816,7 +868,7 @@ + (DDMathFunction) asechFunction {
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:1/cosh([n doubleValue])];
- return [DDExpression numberExpressionWithNumber:result];
+ return _DDRTOD([DDExpression numberExpressionWithNumber:result], evaluator, error);
};
return DD_AUTORELEASE([function copy]);
}
@@ -828,7 +880,7 @@ + (DDMathFunction) acotanhFunction {
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:1/atanh([n doubleValue])];
- return [DDExpression numberExpressionWithNumber:result];
+ return _DDRTOD([DDExpression numberExpressionWithNumber:result], evaluator, error);
};
return DD_AUTORELEASE([function copy]);
}
@@ -837,7 +889,9 @@ + (DDMathFunction) acotanhFunction {
+ (DDMathFunction) versinFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:1-cos([n doubleValue])];
@@ -849,7 +903,9 @@ + (DDMathFunction) versinFunction {
+ (DDMathFunction) vercosinFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:1+cos([n doubleValue])];
@@ -861,7 +917,9 @@ + (DDMathFunction) vercosinFunction {
+ (DDMathFunction) coversinFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:1-sin([n doubleValue])];
@@ -873,7 +931,9 @@ + (DDMathFunction) coversinFunction {
+ (DDMathFunction) covercosinFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:1+sin([n doubleValue])];
@@ -885,7 +945,9 @@ + (DDMathFunction) covercosinFunction {
+ (DDMathFunction) haversinFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:(1-cos([n doubleValue]))/2];
@@ -897,7 +959,9 @@ + (DDMathFunction) haversinFunction {
+ (DDMathFunction) havercosinFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:(1+cos([n doubleValue]))/2];
@@ -909,7 +973,9 @@ + (DDMathFunction) havercosinFunction {
+ (DDMathFunction) hacoversinFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:(1-sin([n doubleValue]))/2];
@@ -921,7 +987,9 @@ + (DDMathFunction) hacoversinFunction {
+ (DDMathFunction) hacovercosinFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:(1+sin([n doubleValue]))/2];
@@ -933,7 +1001,9 @@ + (DDMathFunction) hacovercosinFunction {
+ (DDMathFunction) exsecFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:(1/cos([n doubleValue]))-1];
@@ -945,7 +1015,9 @@ + (DDMathFunction) exsecFunction {
+ (DDMathFunction) excscFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:(1/sin([n doubleValue]))-1];
@@ -957,7 +1029,9 @@ + (DDMathFunction) excscFunction {
+ (DDMathFunction) crdFunction {
DDMathFunction function = ^ DDExpression* (NSArray *arguments, NSDictionary *variables, DDMathEvaluator *evaluator, NSError **error) {
REQUIRE_N_ARGS(1);
- NSNumber *n = [[arguments objectAtIndex:0] evaluateWithSubstitutions:variables evaluator:evaluator error:error];
+ DDExpression *argument = [arguments objectAtIndex:0];
+ argument = _DDDTOR(argument, evaluator, error);
+ NSNumber *n = [argument evaluateWithSubstitutions:variables evaluator:evaluator error:error];
RETURN_IF_NIL(n);
NSNumber *result = [NSNumber numberWithDouble:2*sin([n doubleValue]/2)];
View
5 DDMathParser/_DDVariableExpression.m
@@ -57,6 +57,11 @@ - (NSNumber *) evaluateWithSubstitutions:(NSDictionary *)substitutions evaluator
if (evaluator == nil) { evaluator = [DDMathEvaluator sharedMathEvaluator]; }
id variableValue = [substitutions objectForKey:[self variable]];
+
+ if (variableValue == nil) {
+ variableValue = [evaluator variableWithName:[self variable]];
+ }
+
if ([variableValue isKindOfClass:[DDExpression class]]) {
return [variableValue evaluateWithSubstitutions:substitutions evaluator:evaluator error:error];
}

0 comments on commit 29073d7

Please sign in to comment.
Something went wrong with that request. Please try again.