3

How to calculate formulas like (a + b) ^ 2, sum((a + b) / 2) using NSExpression? I am the beginner to NSExpression.

(a + b) ^ 2 how to use whole square 2 in objective c.

SnakingPrabhu
  • 156
  • 1
  • 15

4 Answers4

13

NSExpression is useful for evaluating math expressions in objective c.

let's see how to find the standard deviation of numbers using NSExpression

NSArray *arrNumbers = @[@3, @6, @3, @8, @4, @12, @9, @11];

NSExpression *expression = [NSExpression expressionForFunction:@"stddev:" arguments:@[[NSExpression expressionForConstantValue:arrNumbers]]];

id value = [expression expressionValueWithObject:nil context:nil];

It's that simple.

Below is list of functions grouped by category.

1)List of statistics methods

average:
sum:
count:
min:
max:
median:
mode:
stddev:

2)Arithmetic functions

add:to:
from:subtract:
multiply:by:
divide:by:
modulus:by:
abs:
sqrt:
log:
ln:
raise:toPower:
exp:

3)Bounding functions

ceiling:
trunc: 

4)Random functions

random
random:

5)Binary arithmetic functions

bitwiseAnd:with:
bitwiseOr:with:
bitwiseXor:with:
leftshift:by:
rightshift:by:
onesComplement:

6)Date functions

now

7)String functions

lowercase:
uppercase:

8)No-Op

noindex:
Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241
  • 2
    Is the full syntax documented anywhere? The Apple docs for NSExpression don't really say anything about what operators (or functions) you can pass to `expressionWithFormat:` – peterflynn Feb 05 '16 at 09:30
  • I also couldn't find much elsewhere too..If you come across lemme know – Durai Amuthan.H Feb 05 '16 at 10:57
9

NSExpression is great for evaluating mathematical expressions. It is NOT only limited to core data queries and you DO NOT need additional third party libraries or write your own parser to evaluate expressions as strings.

To actually answer the question for (a + b) ^ 2 ;)

NSNumber *a = @4;
NSNumber *b = @4;
NSExpression *expression = [NSExpression expressionWithFormat:@"(%@ + %@)**2", a, b];
NSNumber *result = [expression expressionValueWithObject:nil context:nil];
NSLog(@"%@",result);
Brett
  • 2,635
  • 22
  • 35
  • Thanks god one answer got it right! NSExpression is so generic, strong and good! Its only drawbacks are the awkward and lengthy syntax for building NSExpressions, and Apples lack of proper documentation and sample-code. Otherwise, it's a rock-solid piece of software (even if a bit old). It DOES play well in NSPredicates, Core-Data and the Cocoa echo-system, but not limited to it. – Motti Shneor Feb 20 '19 at 08:01
1

NSExpression is not a general-purpose mathematical expression evaluator. It's meant to be used with NSPredicate to describe selection criteria in Core Data queries.

NSResponder
  • 16,861
  • 7
  • 32
  • 46
  • 1
    simply not true. It IS general purpose, and quite extensible and although projects like DDMathParser have built decent replacements for it - it still is very strong. That Apple doesn't promote or document NSExpression abilities appropriately - does not mean it is not suitable for general purpose calculations. – Motti Shneor Feb 20 '19 at 07:52
0

NSExpression is used to represent expressions in NSPredicate. It is not intended for the tasks like yours.

Actually, you can use NSExpression to calculate the formula value, but this usage is quite limited. To evaluate any mathematical expression you would need to make a string parser using some algorithm i.e. Shunting-yard or just reuse a library like this one.

Community
  • 1
  • 1
Vadim
  • 9,383
  • 7
  • 36
  • 58
  • simply and plainly - not true. NSExpression is general purpose, not limited to building NSPredicates, and allows for arbitrarily complex expressions (with sub-expressions etc). You DO NOT need to build any string parser or string builder. You can initialise NSExpressions BOTH using their string-format, or programmatically using the NSExpression building blocks. – Motti Shneor Feb 20 '19 at 07:55