 |
ooPIC Programmer's Guide
Chapter 11 - ooPIC Mathematics
|
|
|
|
ooPIC Mathematics |
| | Any mathematical formula that needs to be solved while an application's program is running requires that it be written out as an Expression in the program's code. ooPIC math is integer only, there is no support for floating point math. |
Expressions |
| | An Expression is a written mathematical formula that details the values, operations and functions as well as the order in which the operations and functions are performed for the formula to be solved. The resulting value of the mathematical formula is the final value derived after all of the formula's operations and functions have been executed. An Expression can be a single value or variable, or it can be comprised of several values and variables separated by Operators and functions. Expressions can be used in the following situations:
- An assignment statement that assigns the resulting value to a Variable
- A flow control statement that conditionally changes the flow of the program depending on the resulting value.
If an Expression is comprised of more than one value or variable, then it is evaluated from left to right, performing in the order of precedence the operators and functions that it encounters. |
Operators |
| | Operators are used to specify that a mathematical operation needs to be performed between two values. They are always performed by applying the value on the right side to the value on the left side in the manner specified by the operator. The following is a list of Operators.
- Arithmetic: +, -, *, /, mod
- Logical: AND, OR, XOR, Not
- Relational: =, <>, <, >, <=. >=
In the following example, the variable "A" is assigned the value of the Variable "B" plus the value of the variable "C" minus the value of the variable "D"
A = B + C - D
|
Precedence of Operators |
| | The precedence of operators determines the order in which mathematical operations are executed. ooPIC languages follow the operator precedence rules of algebraic expressions. These rules dictate that the expression is scanned from left to right and that no operation is performed until it encounters an operator of lower or equal precedence. NOTE: ooPIC order of precedence is NOT the same as in other programming languages, read the following carefully. The following list defines ooPIC's order of precedence:
- Operators in parenthesis
- Negation (-)
- Multiplication (*), Division (/) and Modulus (Mod)
- Addition (+) and Subtraction (-)
- Relational expressions (=, <>, <, >, <=. >=)
- Logical AND (And)
- Logical OR (Or)
- Logical XOR (XOR)
Parenthesized expressions have the highest precedence, so their use is a good way for you to reduce ambiguity and make your programs more readable. In the following example, the variable "A" is assigned the value of the Variable "C" divided by the value of the variable "D" plus the value of the variable "B"
A = B + C / D
In the following example, the variable "A" is assigned the value of the Variable "B" plus the value of the variable "C" divided by the value of the variable "D"
A = (B + C) / D
|
Arithmetic Operators |
| | The Arithmetic Operators perform basic arithmetic functions between two values.
| Basic Syntax | C/Java Syntax | Resulting value |
| {expr1} + {expr2} | {expr1} + {expr2} | The sum of the two expressions. |
| {expr1} - {expr2} | {expr1} - {expr2} | The difference of the two expressions. |
| {expr1} * {expr2} | {expr1} * {expr2} | The product of the two expressions. |
| {expr1} / {expr2} | {expr1} / {expr2} | The quotient of the two expressions. |
| {expr1} Mod {expr2} | {expr1} % {expr2} | The remainder of the two expressions. |
|
Logical Operators |
| | The Logical Operators perform the bitwise logic functions between two values NOTE: C and Java syntax is nonstandard for logical operations.
| Basic Syntax | C/Java Syntax | Resulting value |
| {expr1} And {expr2} | {expr1} & {expr2} | The result of Logically ANDing the bits of the two expressions. |
| {expr1} Or {expr2} | {expr1} | {expr2} | The result of Logically ORing the bits of the two expressions. |
| {expr1} Xor {expr2} | {expr1} ^ {expr2} | The result of Logically Exclusively ORing the bits of the two expressions. |
| Not {expr1} | ~{expr1} | The result of Logically Inverting the bits of {expr1} |
|
Relational Operators |
| | The Relational Operators perform comparisons between two values.
| Basic Syntax | C/Java Syntax | Resulting value |
| {expr1} = {expr2} | {expr1} == {expr2} | 1 if the two expressions are equal, 0 if not. |
| {expr1} <> {expr2} | {expr1} != {expr2} | 1 if the two expressions are not equal, 0 if equal. |
| {expr1} < {expr2} | {expr1} < {expr2} | 1 if {expr1} is less than {expr2}, 0 if not. |
| {expr1} > {expr2} | {expr1} > {expr2} | 1 if {expr1} is more than {expr2}, 0 if not. |
| {expr1} <= {expr2} | {expr1} <= {expr2} | 1 if {expr1} is less than or equal to {expr2}, 0 if not. |
| {expr1} >= {expr2} | {expr1} >= {expr2} | 1 if {expr1} is more than or equal to {expr2}, 0 if not. |
|
Functions |
| | Functions are used when one value needs to be converted from one form to another. They are performed by applying the value within the functions in parenthesis to the conversion process specified by the function. The result is in reference to binary radians. See the specific references for the trigonometric functions. The following is a list of Function.
| Syntax | Resulting Value |
| Sin({expr}) | The binary formatted trigonometric sine of {expr} |
| Cos({expr}) | The binary formatted trigonometric cosine of {expr} |
|
|