Skip to content
Dave DeLong edited this page Dec 24, 2011 · 11 revisions

DDMathParser recognizes all common mathematical operators:

Operator Function Description
Standard Operators
+ add addition and unary positive
- or  subtract and negate subtraction and negation
* or × multiply multiplication
/ or ÷ divide division
% mod or percent modulus or a percentage of a value
! factorial factorial
** pow exponentiation
º or ° dtor converts the value to radians
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

The Degree Operator

The degree operator (°) is interesting. Because all of the trigonometric functions require their parameters to be passed in radians, the degree operator will convert its operand into radians. Thus, 45° is equivalent to dtor(45).

The % Sign

The % sign is usually interpreted as the modulo operator. However, DDMathParser.h defines a compile-time switch (DD_INTERPRET_PERCENT_SIGN_AS_MOD) that allows you to change it to be interpreted as a percentage.

When the percent sign is interpreted as modulo, then:

10 % 3

... evaluates to 1 (the remainder after 10 is divided by 3). However, if you flip the switch to make the % sign be a percentage, then you can now do:

250 + 10%

By default, % is usually shorthand for "/100". In other words, 42% becomes 42/100, or 0.42.

However, if the % term is the right hand sign of either a subtraction or addition operation (such as in "250 + 10%"), then the percent is evaluated as a percentage of the left-hand side (i.e. "250 plus 10% of 250").

If you choose to interpret the percent sign as the modulo operator, you can still request a percentage by using the function name directly:

(10 % 3) + percent(50) = 1.5

The question arises: why are these mutually exclusive? It's because of a limitation in the design of the tokenizer. When an operator is encountered that is ambiguous (ex: ! can be both factorial and logical not), it is disambiguated exclusively by the token that precedes it. Since both modulo and percent are preceded by identical things (usually a number), there's currently no way to disambiguate whether % refers to a modulo operation or a percent operation. Therefore, the distinction must be made at compile-time.

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.