Mathematical Expressions
Numeric variables and literals can be combined using the inx style of mathematical ex-pressions. Inx is the method of expression in which the mathematical operation is placed
between the values upon which it acts (as opposed to prex or postx). This is the style
common in most TI calculators.
5.4.1 Basic arithmetic
RobotC recognizes the usual mathematical symbols: + (addition), - (subtraction), * (mul-
tiplication), and / (division). In addition, RobotC recognizes the use of parentheses for
grouping in mathematical expressions. RobotC also recognizes a number of more advanced
mathematical functions like sine, cosine, logarithms and the exponential function. Some
of those additional functions are summarized in the RobotC On-line Support on the left
side-bar under the NXT Functions ! Math section.
5.4.2 Integer arithmetic
The basic arithmetic operations are straightforward and intuitive in most cases. However,
when working with integer datatypes there is an exception. The / operator (division) au-
tomatically detects when it is operating on a pair of integers and, in that case, switches to
whole number division. Whole number division returns an integer value that represents
the number of times the denominator goes into the numerator, dropping any remainder.
For example, the expression 3/2 in RobotC evaluates to 1 not 1.5. The expression -1/2
evaluates to 0. The expression 33/10 evaluates to 3.
The / (division) operator only performs whole number division if both the numerator
and denominator are integer type variables or literals. In all other cases, ordinary division
is used. To force ordinary division of two integers either include a decimal point if it is a
literal value, e.g. 3/2.0 instead of just 3/2, or convert the integer variable to a
oat, e.g.
((float)n)/2 instead of n/2.
If we want the remainder after whole number division, there is separate operator for inte-
gers, % (modulus) that returns an integer value that represents the remainder after dividing
the left-hand side by the right-hand side. For example, 3%2 evaluates to 1, 33%10 evaluates
to 3.
Together these operators provide powerful methods of manipulating integer values.
5.4.3 Exponentiation
A curious omission from this collection of mathematical functions is the exponentiation
function for computing quantities like 23 or 100:33. However, we have the tools necessary
to build our own. The C programming language uses the function, pow(), to perform
exponentiation. For example, 23 = pow(2,3), and 100:33 = pow(10,0.33). If we have need
of exponentiation, we simply take advantage of the properties of logarithms base e and the
exponential function, ex. Recall that
loge(xa) = a loge x:
Also, recall that loge(ex) = x. Combining these two properties, we have
xa = ea loge(x):
In RobotC, ex = exp(x), and loge(x) = log(x). Therefore, the function in Listing 5.6 will
give us the standard C exponentiation function.
float pow( float x, float a) {
return exp (a*log(x ));
}
Listing 5.6: The standard C language exponentiation function built of available RobotC
functions.
No comments:
Post a Comment