Using Mathematical Expressions
At times, you’ll want to perform some kind of mathematical
operation in your scripts and assign the results to a variable. As with most
programming languages, the command shell allows you to write mathematical
expressions using a variety of operators, including
-
Arithmetic operators to perform standard mathematical operations (such as addition, subtraction, multiplication, and division)
-
Assignment operators that combine an assignment operation (symbolized by the equal sign) with an arithmetic operation
-
Comparison operators that compare values and are usually used with if statements
-
Bitwise operators that allow you to manipulate the sequences of binary values
Math operations are performed using set with the /A (arithmetic) parameter, such as
set /a theTotal=18+2set /a theTotal=18*2set /a theTotal=18/2
All mathematical expressions are evaluated using 32-bit signed
integer arithmetic. This allows for values –232 to 232–1.
If you exceed this range, you’ll get an arithmetic error (code –2) instead of
the intended value.
The most commonly used operators are those for arithmetic,
assignment, and comparison. Arithmetic and assignment operators are discussed in
the sections that follow. Comparison operators are discussed in the section
titled “Making
Comparisons in If Statements,” later in this chapter. Pay particular
attention to the additional discussions on operator precedence and simulating
exponents in scripts.
Working with Arithmetic and Assignment Operators
You use arithmetic operators to perform basic math
operations on numerical values. These values can be expressed literally as a
number, such as 5, or as a variable that contains the value you want to work
with, such as %TOTAL%.
Table 3-2
summarizes the available arithmetic and assignment operators. Most of the
arithmetic operators are fairly straightforward. You use * in multiplication, /
in division, + in addition, and – in subtraction. You use the equal sign (=) to
assign values to variables. You use % (modulus) to obtain the remainder from
division. For example, if you divide 8 into 60, the answer is 7 Remainder 4; the
value 4 is what the result would be if you use the modulus operator.
Arithmetic Operators
|
Assignment Operators
|
---|---|
+ (Addition)
|
+= (Increment, that is, add and assign)
|
- (Subtraction)
|
-= (Decrement, that is, subtract and assign)
|
* (Multiplication)
|
*= (Scale up, that is, multiply and assign)
|
/ (Division)
|
/= (Scale down, that is, divide and assign)
|
% (Modulus)
|
%= (Modulus and assign)
|
Examples of working with arithmetic operators follow:
set /a theCount=5+3
set /a theCount=%nServers% + %nWstations%
set /a theCount=%nServers% - 1
Tip |
Earlier, I stated that everything stored in a variable is a
string, and that remains true. However, the command shell can detect when a
string contains only numerals, and this is what allows you to use variables in
expressions. The key detail to remember is to use the proper syntax for
substitution, which is %variableName%.
|
You use assignment operators to increment, decrement, scale up, or
scale down. These operators combine arithmetic and assignment operation
functions. For example, the += operator is used to increment a value and
combines the effects of the + operator and the = operator. Thus, the following
two expressions are equivalent and yield the identical results when entered at
the command line:
set /a total=total+1set /a total+=1
Understanding Operator Precedence
One thing you should understand when working with mathematic
operations is operator precedence. Operator precedence determines what happens
when the command shell must evaluate an expression that involves more than one
operator. For example:
set /a total=8+3*4
If evaluated from left to right, this expression equals 44
(8+3=11, 11*4=44). But as in standard mathematics, that’s not how the command
line evaluates the expression. Instead, the command shell evaluates the
expression as 20 (3*4=12, 8+12=20) because the precedence of operations is the
following:
-
Modulus
-
Multiplication and division
-
Addition and subtraction
Note |
When an expression contains multiple operations at the same
precedence level, these operations are performed from left to right. Hence set /a total=10-4+2 equals 8 (10-4=6,
6+2=8).
|
However, as with standard mathematics, you can use parenthetical
grouping to ensure numbers are processed in a certain way. This means you can
use the expression
set /a total=(8+3)*4
to ensure that the command-line interprets the expression as
(8+3=11, 11*4=44).
Simulating Exponents
Although you can perform many mathematical operations at the
command line, there is no way to raise values to exponents. You can, however,
perform these operations manually. For example, the easiest way to get a value
for 23 is to enter
set /a total=2*2*2
The result is 8. Similarly, you can get a value for 105
by entering
set /a total=10*10*10*10*10
The result is 100,000.
No comments:
Post a Comment