-
Notifications
You must be signed in to change notification settings - Fork 0
Operators
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 |
º 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 |
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 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]).
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).
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.