Subversion Repositories bacoAlunos

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1777 jmachado 1
<html>
2
<head>
3
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4
<title>
5
Math</title>
6
<meta content="DocBook XSL Stylesheets V1.60.1" name="generator">
7
<link rel="home" href="index.html" title="Antelope Users Guide">
8
<link rel="up" href="bk03.html" title="Additional Ant Tasks">
9
<link rel="previous" href="bk03ch10.html" title="Chapter&nbsp;10.&nbsp;Limit">
10
<link rel="next" href="bk03ch12.html" title="Chapter&nbsp;12.&nbsp;HTTP Post">
11
</head>
12
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13
<div class="chapter" lang="en">
14
<div class="titlepage">
15
<div>
16
<div>
17
<h2 class="title">
18
<a name="math">
19
</a>
20
Math</h2>
21
</div>
22
</div>
23
<div>
24
</div>
25
</div>
26
<p>
27
 
28
The Math task provides support for all the basic mathematical operations
29
provided by the java.lang.Math and java.lang.StrictMath classed. It supports int, long, float and double data types. Nesting of operations is supported to allow computation of formulas like (6 + (7.25 * 3.9))/(2 * 3 * 3) or calculating the area of a circle given a radius (I'm sure this comes up often in builds controlled by Ant!).
30
</p>
31
<p>
32
 
33
In addition to the operations provided by the java.lang.Math and java.lang.StrictMath classes, the Math task provides several additional operations: "add", "subtract", "multiply", "divide", and "mod", which duplicate the basic Java mathematical operations "+", "-", "*", "/", and "%", respectively. In fact, either notation can be used, that is, the operation can be set to "add" or "+", depending only on which you feel is more convenient.
34
</p>
35
 
36
<p>
37
 
38
<div class="table">
39
<a name="N109AE">
40
</a>
41
<p class="title">
42
<b>
43
Table&nbsp;11.1.&nbsp;Math Task Attributes</b>
44
</p>
45
<table summary="Math Task Attributes" border="1">
46
<colgroup>
47
<col>
48
<col>
49
<col>
50
<col>
51
</colgroup>
52
<thead>
53
<tr>
54
<th>
55
Attribute</th>
56
<th>
57
Description</th>
58
<th>
59
Default</th>
60
<th>
61
Required</th>
62
</tr>
63
</thead>
64
<tbody>
65
<tr>
66
<td>
67
result</td>
68
<td>
69
The name of the property to hold the result of the operation.</td>
70
<td>
71
None</td>
72
<td>
73
Yes</td>
74
</tr>
75
<tr>
76
<td>
77
datatype</td>
78
<td>
79
Sets the datatype of the calculation. Allowed values are
80
"int", "long", "float", or "double". Optional, if
81
used, will be applied to all numbers in this math operation.</td>
82
<td>
83
double</td>
84
<td>
85
No</td>
86
</tr>
87
<tr>
88
<td>
89
strict</td>
90
<td>
91
If true, use the methods in the java.lang.StrictMath class.</td>
92
<td>
93
false</td>
94
<td>
95
No</td>
96
</tr>
97
<tr>
98
<td>
99
operation</td>
100
<td>
101
If used, any nested Ops will be ignored. This is for convenience for simple calculations.</td>
102
<td>
103
None</td>
104
<td>
105
No</td>
106
</tr>
107
<tr>
108
<td>
109
operand1</td>
110
<td>
111
A number to use with the operation specified in the 'operation' attribute.</td>
112
<td>
113
None</td>
114
<td>
115
Depends on the specific operation.</td>
116
</tr>
117
<tr>
118
<td>
119
operand2</td>
120
<td>
121
A number to use with the operation specified in the 'operation' attribute.</td>
122
<td>
123
None</td>
124
<td>
125
Depends on the specific operation.</td>
126
</tr>
127
</tbody>
128
</table>
129
</div>
130
 
131
</p>
132
<p>
133
 
134
The 'result' property is reusable.
135
</p>
136
 
137
 
138
The Math task supports nested "Op" elements. An Op element represents single mathematical operation, such as "min" or "add".
139
As an alternate, if using Ant 1.5+, you can specify the operation in the tag name itself.  However, you must use the text
140
name (+,-,/,*,% are not permitted as tag names)
141
 
142
<code><pre>
143
  &lt;radians&gt;
144
     &lt;num value="90" /&gt;
145
  &lt;/radians&gt;
146
</pre></code>
147
 
148
instead of
149
 
150
<code><pre>
151
  &lt;op op="radians"&gt;
152
     &lt;num value="90" /&gt;
153
  &lt;/op&gt;
154
</pre></code>
155
 
156
 
157
<p>
158
 
159
<div class="table">
160
<a name="N109FB">
161
</a>
162
<p class="title">
163
<b>
164
Table&nbsp;11.2.&nbsp;Op Attributes</b>
165
</p>
166
<table summary="Op Attributes" border="1">
167
<colgroup>
168
<col>
169
<col>
170
<col>
171
<col>
172
</colgroup>
173
<thead>
174
<tr>
175
<th>
176
Attribute</th>
177
<th>
178
Description</th>
179
<th>
180
Default</th>
181
<th>
182
Required</th>
183
</tr>
184
</thead>
185
<tbody>
186
<tr>
187
<td>
188
op</td>
189
<td>
190
Set the name of this operation. Allowed values are
191
one of the method names from java.lang.Math or java.lang.StrictMath, or one of
192
"add", "subtract", "multiply", "divide", or "mod" (or "+", "-", "*", "/", or "%",
193
respectively).  "toRadians" and "toDegrees" can be represented by "radians" and
194
"degrees", respectively, as a shorthand</td>
195
<td>
196
None</td>
197
<td>
198
Yes, if not specified in the tag name.</td>
199
</tr>
200
<tr>
201
<td>
202
datatype</td>
203
<td>
204
Sets the datatype of this calculation. Allowed values are
205
"int", "long", "float", or "double". Optional, default
206
is "double". If the parent Math task has a datatype set, this value will be ignored and the datatype specifed in the task will be used.</td>
207
<td>
208
"int"</td>
209
<td>
210
No</td>
211
</tr>
212
 
213
<tr>
214
<td>
215
arg1, arg2, arg3, arg4, arg5/td>
216
<td>
217
The arguments for this operation.  This is a shorthand to avoid having to use nested
218
elements when performing a simple calculation.
219
</td>
220
<td>
221
None</td>
222
<td>
223
No. However, these attributes are mutually exclusive with the <op> and <num> subelements.</td>
224
</tr>
225
 
226
</tbody>
227
</table>
228
</div>
229
 
230
</p>
231
<p>
232
 
233
The Op element supports nested "Op" elements and nested "Num" elements. A Num represents a number.
234
When an Op is nested in another Op, the result of the Op is treated as a Num. The nested elements
235
can be any combination of Op (short form included as mentioned above) or Num as appropriate for the
236
formula being calculated. Most of the
237
operations provided by java.lang.Math and java.lang.StrictMath operate on one or two numbers.
238
The "+", "-", "*", "/", and "%" operations can task any number of nested numbers.
239
</p>
240
<p>
241
 
242
<div class="table">
243
<a name="N10A23">
244
</a>
245
<p class="title">
246
<b>
247
Table&nbsp;11.3.&nbsp;Num Attributes</b>
248
</p>
249
<table summary="Num Attributes" border="1">
250
<colgroup>
251
<col>
252
<col>
253
<col>
254
<col>
255
</colgroup>
256
<thead>
257
<tr>
258
<th>
259
Attribute</th>
260
<th>
261
Description</th>
262
<th>
263
Default</th>
264
<th>
265
Required</th>
266
</tr>
267
</thead>
268
<tbody>
269
<tr>
270
<td>
271
value</td>
272
<td>
273
Set the value for this number. Must be able to parse to the datatype set by the parent element or the default datatype set by the task. Two special numbers, pi and e, can be represented by PI and E respectively. ("PI" is the ratio of the diameter of a circle to its radius, "E" is Euler's e, the base for natural logrithms.)</td>
274
<td>
275
None</td>
276
<td>
277
Yes</td>
278
</tr>
279
<tr>
280
<td>
281
datatype</td>
282
<td>
283
Sets the datatype of this number. Allowed values are
284
"int", "long", "float", or "double". Optional, default
285
is "double". If the parent Math task has a datatype set, this value will be ignored and the datatype specifed in the task will be used.</td>
286
<td>
287
double</td>
288
<td>
289
No</td>
290
</tr>
291
</tbody>
292
</table>
293
</div>
294
 
295
</p>
296
<p>
297
 
298
Some examples:
299
</p>
300
<p>
301
 
302
<table border="0" bgcolor="#E0E0E0">
303
<tr>
304
<td>
305
<pre class="programlisting">
306
 
307
 
308
    &lt;var name="op1" value="12"/&gt;
309
    &lt;var name="op2" value="6"/&gt;
310
    &lt;var name="op" value="+"/&gt;
311
 
312
    &lt;!-- demo plus --&gt;
313
    &lt;math result="result" operand1="${op1}" operation="${op}" operand2="${op2}" datatype="int"/&gt;
314
    &lt;echo&gt;${op1} ${op} ${op2} = ${result}&lt;/echo&gt;
315
    &lt;assert name="result" value="18"/&gt;
316
 
317
    &lt;!-- demo reusing result --&gt;
318
    &lt;math result="result" operand1="${result}" operation="${op}" operand2="${op2}" datatype="int"/&gt;
319
    &lt;echo&gt;${op1} ${op} ${op2} = ${result}&lt;/echo&gt;
320
    &lt;assert name="result" value="24"/&gt;
321
 
322
    &lt;!-- demo minus --&gt;
323
    &lt;var name="op" value="-"/&gt;
324
    &lt;math result="result" operand1="${op1}" operation="${op}" operand2="${op2}" datatype="int"/&gt;
325
    &lt;echo&gt;${op1} ${op} ${op2} = ${result}&lt;/echo&gt;
326
    &lt;assert name="result" value="6"/&gt;
327
 
328
    &lt;!-- demo multiply --&gt;
329
    &lt;var name="op" value="*"/&gt;
330
    &lt;math result="result" operand1="${op1}" operation="${op}" operand2="${op2}" datatype="int"/&gt;
331
    &lt;echo&gt;${op1} ${op} ${op2} = ${result}&lt;/echo&gt;
332
    &lt;assert name="result" value="72"/&gt;
333
 
334
    &lt;!-- demo divide --&gt;
335
    &lt;var name="op" value="/"/&gt;
336
    &lt;math result="result" operand1="${op1}" operation="${op}" operand2="${op2}" datatype="int"/&gt;
337
    &lt;echo&gt;${op1} ${op} ${op2} = ${result}&lt;/echo&gt;
338
    &lt;assert name="result" value="2"/&gt;
339
 
340
    &lt;!-- demo modulo --&gt;
341
    &lt;var name="op" value="%"/&gt;
342
    &lt;math result="result" operand1="${op1}" operation="${op}" operand2="${op2}" datatype="int"/&gt;
343
    &lt;echo&gt;${op1} ${op} ${op2} = ${result}&lt;/echo&gt;
344
    &lt;assert name="result" value="0"/&gt;
345
 
346
    &lt;!-- demo calculating the area of a circle --&gt;
347
    &lt;!-- first, calculate the radius --&gt;
348
    &lt;math result="radius"&gt;  &lt;!-- defaults to double datatype --&gt;
349
        &lt;op type="*"&gt;
350
            &lt;num value="1"/&gt;
351
            &lt;num value="2"/&gt;
352
            &lt;num value="3"/&gt;
353
            &lt;num value="4"/&gt;
354
            &lt;num value="5"/&gt;
355
        &lt;/op&gt;
356
    &lt;/math&gt;
357
    &lt;echo&gt; 1 * 2 * 3 * 4 * 5 = ${radius}&lt;/echo&gt;
358
 
359
    &lt;!-- now calculate the area --&gt;
360
    &lt;math result="area" precision="float"&gt;
361
        &lt;op type="*"&gt;
362
            &lt;num value="PI"/&gt;
363
            &lt;op type="pow"&gt;
364
                &lt;num value="${radius}"/&gt;
365
                &lt;num value="2"/&gt;
366
            &lt;/op&gt;
367
        &lt;/op&gt;
368
    &lt;/math&gt;
369
    &lt;echo&gt;area = PI * radius ^ 2 = ${area}&lt;/echo&gt;
370
 
371
    &lt;!-- demo calculating a random number between 0 and 100 --&gt;
372
    &lt;math result="result"&gt;
373
        &lt;op op="rint"&gt;
374
            &lt;op op="*"&gt;
375
                &lt;num value="100"/&gt;
376
                &lt;op op="random"/&gt;
377
            &lt;/op&gt;
378
        &lt;/op&gt;
379
    &lt;/math&gt;
380
    &lt;echo&gt;a random number between 0 and 100: ${result}&lt;/echo&gt;
381
 
382
    &lt;!-- demo another multiplication --&gt;
383
    &lt;math result="result" operation="multiply" operand1="17" operand2="13"/&gt;
384
    &lt;echo&gt;${result}&lt;/echo&gt;
385
 
386
    &lt;!-- demo shorthand notation for calculating sin of an angle, which is degrees --&gt;
387
    &lt;math result="sin"&gt;
388
      &lt;sin&gt;
389
        &lt;radians arg1="${angle_in_degrees}" /&gt;
390
      &lt;/sin&gt;
391
    &lt;/math&gt;
392
    &lt;echo&gt;${sin}&lt;/echo&gt;
393
 
394
</pre>
395
</td>
396
</tr>
397
</table>
398
 
399
</p>
400
</div>
401
    <hr>
402
    <p align="center">Copyright &copy; 2003 Ant-Contrib Project. All
403
    rights Reserved.</p>
404
</body>
405
</html>