Skip to content
Dave DeLong edited this page Oct 18, 2011 · 11 revisions

DDMathParser recognizes all common mathematical operators:

Operator Function Description
Standard Operators
+ add addition and unary positive
- subtract and negate subtraction and negation
* and × multiply multiplication
/ and ÷ divide division
% mod modulus
! factorial factorial
** pow exponentiation
Bitwise Operators
& and bitwise and
| or bitwise or
^ xor bitwise xor
~ not bitwise not
<< lshift bitwise left shift
>> rshift bitwise right shift
Comparison Operators
== or = l_eq equal
!= l_neq not equal
< l_lt less than
> l_gt greater than
<= or  l_ltoe less than or equal
>= or  l_gtoe greater than or equal
Logical Operators
&& or  l_and logical and
|| or  l_or logical or
! or ¬ l_not logical not

Considerations

Logical Values

The comparison and logical operators both return a boolean value. During evaluation, that boolean value is strictly interpreted as either 0 (false) or 1 (true). For example, @"41 + (1 && 1)" is literally 41 + true, but is evaluated as 42.

On the same note, the operands to the logical operators are also interpreted as booleans (using -[NSNumber boolValue]).

Factorial and Logical Not

Differentiating between factorial (!) and a logical not (!) is difficult. A ! is interpreted as a logical not if:

  • it is the first token
  • it is preceded by a binary operator
  • it is preceded by a right associative unary operator

Otherwise it is treated as a factorial. A ¬ token is always treated as a logical not (for obvious reasons).

Parentheses and Associativity

For simplification in dealing with implicit multiplication, an opening parenthesis is considered a right associative unary operator, and a closing parenthesis is considered a left associative unary operator.

Clone this wiki locally