Monday, 11 February 2013

Mathematical Expressions


Mathematical Expressions

Numeric variables and literals can be combined using the in x style of mathematical ex-
pressions. In x is the method of expression in which the mathematical operation is placed
between the values upon which it acts (as opposed to pre x or post x). 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.

No comments:

Post a Comment