Arithmetic Operators
D provides the binary arithmetic operators shown in the following table for
use in your programs. These operators all have the same meaning for
integers as they do in ANSI-C.
Table 2-6 D Binary Arithmetic Operators
+ |
integer addition |
- |
integer subtraction |
* |
integer multiplication |
/ |
integer division |
% |
integer modulus |
Arithmetic in D may only be performed on integer operands, or on
pointers, as discussed in Chapter 5, Pointers and Arrays. Arithmetic may not be performed on floating-point
operands in D programs. The DTrace execution environment does not take any
action on integer overflow or underflow. You must check for these conditions
yourself in situations where overflow and underflow can occur.
The DTrace execution environment does automatically check for and report division by
zero errors resulting from improper use of the / and % operators.
If a D program executes an invalid division operation, DTrace will automatically
disable the affected instrumentation and report the error. Errors detected by DTrace
have no effect on other DTrace users or on the operating system
kernel, so you don't need to worry about causing any damage if
your D program inadvertently contains one of these errors.
In addition to these binary operators, the + and - operators may
also be used as unary operators as well; these operators have higher
precedence than any of the binary arithmetic operators. The order of precedence
and associativity properties for all the D operators is presented in Table 2-11.
You can control precedence by grouping expressions in parentheses ( ).