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