Page 40 - Programming Microcontrollers in C
P. 40

Operators and Expressions       25

                              Two unary operators are + and –. These operators are of higher
                          precedence than the normal arithmetic operators. They operate on
                          only the operand written to the right of the operator and are therefore
                          called unary. The unary minus sign causes the negative value of the
                          operand to be calculated, and the unary positive sign causes no cal­
                          culation to take place.
                              Among the binary operators, *, /, and % have equal prece­
                          dence, which is higher that of + and – . The unary operators + and –
                          have a higher precedence than *, /, or %. The arithmetic operators
                          will work with any of the arithmetic types. Because the operations
                          needed for an integer operation differ from those needed for the cor­
                          responding double operation, the compiler will place the proper
                          arithmetic routines in the code to perform the specified operation.
                              The concept of a fraction is almost unknown to an integer type.
                          If a division of two integers is executed, the result is rounded toward
                          zero. Therefore, the result of 1/2 is 0 as is 9999/10000. This charac­
                          teristic is often used in programming.
                              The only way that you can handle fractions with integer opera­
                          tions is to make use of the modulus operation. The result of a %b is
                          the remainder that is left over after a is divided by b. The modulus
                          operation can provide insight into the fractional value of what is left
                          over after an integer divide.

                          EXERCISES
                          1. Write a program that evaluates

                            f(x) = X – 3X + 2
                                     2
                            for values of X in 0 ≤ X ≤ 3 in steps of 0.1.
                          2. The roots of a quadratic equation can be evaluated by the equation

                                            2
                            x = (–b + sqrt(b – 4ac))/2a
                            and

                                            2
                            x = (–b – sqrt(b – 4ac))/2a
                                                              2
                            where the quadratic equation is ax + bx + c = 0. Write a pro­
                            gram that will evaluate the roots of such an equation. Note that the
                                          2
                            term sqrt(b - 4ac) is called the discriminant. If its argument
   35   36   37   38   39   40   41   42   43   44   45