Many times a programmer is given an algebraic formula, and must write an assignment statement that will correctly calculate the answer. After writing a statement, check it by hand, using sample values and the method shown above to verify that the equation is correct. Use the following guidelines to convert algebraic expressions to programming statements:
Algebra | ActionScript | Explanation |
---|---|---|
xy | x*y | Algebra uses single letters for variables, and can omit the *, programming languages always require an operator. |
x·y | x*y | Algebra sometimes uses a dot for multiplication. |
x*x | You could use Math.pow(x,2) but x*x is faster. | |
Math.pow(x,z) | The pow function raises X to the Z power. | |
Math.sqrt(x) | The Math.sqrt function returns the square root. | |
(a+b)/c | Compound numerators and divisors must be enclosed in parenthesis: Notice the difference between this example and the one below. | |
a+b/c | Notice the difference between this example and the one above. |
Remember: Algebra uses single letters for variables: programmers should use good variable names wherever possible!