I am new to programming. I want to add some new functions such as derivatives and integrations to ddmathparser
. The only one I can find is the short tutorial on ddmathparser
's wiki page https://github.com/davedelong/DDMathParser/wiki/Adding-New-Functions . However, I can't follow it because it's too short and after reading it several times, I still can't understand what it's doing. So can anyone elaborate steps to add a new function or give me some more detailed tutorials of doing this?
I really did my research but I can't find one. Thanks very much.
Asked
Active
Viewed 325 times
1

zyl1024
- 690
- 1
- 9
- 23
1 Answers
1
DDMathParser author here.
Here's how you add a multiply by two
function:
DDMathEvaluator *evaluator = [DDMathEvaluator sharedMathEvaluator];
// a function takes arguments, variable values, the evaluator, and an error pointer
// and returns a new expression
[evaluator registerFunction:^DDExpression *(NSArray *args, NSDictionary *vars, DDMathEvaluator *eval, NSError *__autoreleasing *error) {
DDExpression *final = nil;
// multiplyBy2() can only handle a single argument
if ([args count] == 1) {
// get the argument and simply wrap it in a "multiply()" function
DDExpression *argExpression = [args objectAtIndex:0];
DDExpression *twoExpression = [DDExpression numberExpressionWithNumber:@2];
final = [DDExpression functionExpressionWithFunction:DDOperatorMultiply arguments:@[argExpression, twoExpression] error:nil];
} else if (error) {
// there wasn't only one argument
NSString *description = [NSString stringWithFormat:@"multiplyBy2() requires 1 argument. %ld were given", [args count]];
*error = [NSError errorWithDomain:DDMathParserErrorDomain code:DDErrorCodeInvalidNumberOfArguments userInfo:@{NSLocalizedDescriptionKey: description}];
}
return final;
} forName:@"multiplyBy2"];
Now you can do:
NSNumber *result = [@"multiplyBy2(21)" stringByEvaluatingString];
and get back @42
.
What's going on here:
Internally, DDMathEvaluator
essentially has a big NSDictionary
where it keeps a list of all the functions it knows about, keyed off the name of that function, kind of like this:
_functions = @{
@"multiply" : multiplyFunctionBlock,
@"add" : addFunctionBlock,
...
};
(Obviously it's a bit more complicated than that, but that's the basic idea)
When the evaluator evaluates a string and comes across the a function, it looks up in this dictionary what the block for the function is. It retrieves the block, and then executes the block with the arguments (if there are any) from the string. The result of the block is the result of the function.
That result gets substituted back in, and evaluation continues.

Dave DeLong
- 242,470
- 58
- 448
- 498
-
Thank you. Now I understand how this works. So where should I put the code? In the file that needs the function or in one of the DDMathParser file? – zyl1024 Mar 24 '13 at 14:48
-
2@zyl1024 put it in your own code. – Dave DeLong Mar 24 '13 at 14:50
-
Thank you, especially for the explanation added. – zyl1024 Mar 24 '13 at 15:31
-
Hi Mr. DeLong, I met a new problem. I want to make a new function that takes several arguments. I found that in DDMathParser all arguments are of type DDExpression (even though they are essentially numbers). Can you tell me how to convert DDExpression to NSNumber or int type because I want to do some complex things to the arguments and I'm more comfortable working with NSNumber or int? – zyl1024 Mar 26 '13 at 13:36
-
@zyl1024 https://github.com/davedelong/DDMathParser/blob/master/DDMathParser/_DDFunctionUtilities.m#L69 `DDExpression` objects don't just represent numbers. They can also represent functions and variables. – Dave DeLong Mar 26 '13 at 15:24
-
Hi, can you look at this question asked by me? I don't know how to fix my summation function code. It's here http://stackoverflow.com/questions/15717995/can-anyone-help-fix-this-ddparser-new-function – zyl1024 Mar 30 '13 at 12:27