I have been trying to get some basic math done with NSExpression's expressionValueWithObject(), and the results have been inconsistent.
For example, the following all work just fine:
NSExpression(format: "1+1").expressionValueWithObject(nil, context: nil) as? Float
(returns 2)
NSExpression(format: "1+1.2").expressionValueWithObject(nil, context: nil) as? Float
(returns 2.2)
NSExpression(format: "2**3").expressionValueWithObject(nil, context: nil) as? Float
(returns 8)
NSExpression(format: "sqrt(2**3)").expressionValueWithObject(nil, context: nil) as? Float
(returns 2.82...)
NSExpression(format: "log(2**3)").expressionValueWithObject(nil, context: nil) as? Float
(returns 0.903...)
But other string-equations, seemingly no more complex than these, produce erroneous answers or no answers at all. For example,
NSExpression(format: "1+(1/2)").expressionValueWithObject(nil, context: nil) as? Float
(returns 1, rather than 1.5)
and
NSExpression(format: "43 % 2").expressionValueWithObject(nil, context: nil) as? Float
(returns an error, though Swift itself can natively compute 43 Mod 2 using that same notation)
What am I doing wrong in these last two examples?