source: release-kits/lirk3/ant-scripts/tasks/antelope/docs/manual/bk03ch15.html@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 9.6 KB
Line 
1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Chapter 15. Math</title><meta name="generator" content="DocBook XSL Stylesheets V1.68.1"><link rel="start" href="index.html" title="Antelope Users Guide"><link rel="up" href="bk03.html" title="Additional Ant Tasks"><link rel="prev" href="bk03ch14.html" title="Chapter 14. UID"><link rel="next" href="bk03ch16.html" title="Chapter 16. Hostname"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 15. Math</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="bk03ch14.html">Prev</a> </td><th width="60%" align="center">Additional Ant Tasks</th><td width="20%" align="right"> <a accesskey="n" href="bk03ch16.html">Next</a></td></tr></table><hr></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="math"></a>Chapter 15. Math</h2></div></div></div>
2<STYLE TYPE="text/css"> <!-- @import url(./style.css); --> </STYLE>
3 <p>
4The Math task provides support for all the basic mathematical operations
5provided 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!).
6</p><p>
7In 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.
8</p><p>
9To use this task in your build files, include a task definition like this:
10</p><p>
11</p><pre class="programlisting">
12
13 &lt;taskdef name="math" classname="ise.antelope.tasks.MathTask"/&gt;
14
15</pre><p>
16</p><p>
17</p><div class="table"><a name="id2523260"></a><p class="title"><b>Table 15.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
18"int", "long", "float", or "double". Optional, if
19used, 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>
20</p><p>
21The 'result' property is reusable.
22</p>
23
24The Math task supports nested "Op" elements. An Op element represents single mathematical operation, such as "min" or "add".
25
26<p>
27</p><div class="table"><a name="id2523416"></a><p class="title"><b>Table 15.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
28one of the method names from java.lang.Math or java.lang.StrictMath, or one of "add", "subtract", "multiply", "divide", or "mod" (or "+", "-", "*", "/", or "%", respectively)</td><td>None</td><td>Yes</td></tr><tr><td>datatype</td><td>Sets the datatype of this calculation. Allowed values are
29"int", "long", "float", or "double". Optional, default
30is "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></tbody></table></div><p>
31</p><p>
32The Op element supports nested "Op" elements and nested "Num" elements. A Num represents a number. When an Op is nested in another Op, the nested Op is treated as a Num. The nested elements can be any combination of Op 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.
33</p><p>
34</p><div class="table"><a name="id2523516"></a><p class="title"><b>Table 15.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
35"int", "long", "float", or "double". Optional, default
36is "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>
37</p><p>
38Some examples:
39</p><p>
40</p><pre class="programlisting">
41
42 &lt;var name="op1" value="12"/&gt;
43 &lt;var name="op2" value="6"/&gt;
44 &lt;var name="op" value="+"/&gt;
45
46 &lt;!-- demo plus --&gt;
47 &lt;math result="result" operand1="${op1}" operation="${op}" operand2="${op2}" datatype="int"/&gt;
48 &lt;echo&gt;${op1} ${op} ${op2} = ${result}&lt;/echo&gt;
49 &lt;assert name="result" value="18"/&gt;
50
51 &lt;!-- demo reusing result --&gt;
52 &lt;math result="result" operand1="${result}" operation="${op}" operand2="${op2}" datatype="int"/&gt;
53 &lt;echo&gt;${op1} ${op} ${op2} = ${result}&lt;/echo&gt;
54 &lt;assert name="result" value="24"/&gt;
55
56 &lt;!-- demo minus --&gt;
57 &lt;var name="op" value="-"/&gt;
58 &lt;math result="result" operand1="${op1}" operation="${op}" operand2="${op2}" datatype="int"/&gt;
59 &lt;echo&gt;${op1} ${op} ${op2} = ${result}&lt;/echo&gt;
60 &lt;assert name="result" value="6"/&gt;
61
62 &lt;!-- demo multiply --&gt;
63 &lt;var name="op" value="*"/&gt;
64 &lt;math result="result" operand1="${op1}" operation="${op}" operand2="${op2}" datatype="int"/&gt;
65 &lt;echo&gt;${op1} ${op} ${op2} = ${result}&lt;/echo&gt;
66 &lt;assert name="result" value="72"/&gt;
67
68 &lt;!-- demo divide --&gt;
69 &lt;var name="op" value="/"/&gt;
70 &lt;math result="result" operand1="${op1}" operation="${op}" operand2="${op2}" datatype="int"/&gt;
71 &lt;echo&gt;${op1} ${op} ${op2} = ${result}&lt;/echo&gt;
72 &lt;assert name="result" value="2"/&gt;
73
74 &lt;!-- demo modulo --&gt;
75 &lt;var name="op" value="%"/&gt;
76 &lt;math result="result" operand1="${op1}" operation="${op}" operand2="${op2}" datatype="int"/&gt;
77 &lt;echo&gt;${op1} ${op} ${op2} = ${result}&lt;/echo&gt;
78 &lt;assert name="result" value="0"/&gt;
79
80 &lt;!-- demo calculating the area of a circle --&gt;
81 &lt;!-- first, calculate the radius --&gt;
82 &lt;math result="radius"&gt; &lt;!-- defaults to double datatype --&gt;
83 &lt;op type="*"&gt;
84 &lt;num value="1"/&gt;
85 &lt;num value="2"/&gt;
86 &lt;num value="3"/&gt;
87 &lt;num value="4"/&gt;
88 &lt;num value="5"/&gt;
89 &lt;/op&gt;
90 &lt;/math&gt;
91 &lt;echo&gt; 1 * 2 * 3 * 4 * 5 = ${radius}&lt;/echo&gt;
92
93 &lt;!-- now calculate the area --&gt;
94 &lt;math result="area" precision="float"&gt;
95 &lt;op type="*"&gt;
96 &lt;num value="PI"/&gt;
97 &lt;op type="pow"&gt;
98 &lt;num value="${radius}"/&gt;
99 &lt;num value="2"/&gt;
100 &lt;/op&gt;
101 &lt;/op&gt;
102 &lt;/math&gt;
103 &lt;echo&gt;area = PI * radius ^ 2 = ${area}&lt;/echo&gt;
104
105 &lt;!-- demo calculating a random number between 0 and 100 --&gt;
106 &lt;math result="result"&gt;
107 &lt;op op="rint"&gt;
108 &lt;op op="*"&gt;
109 &lt;num value="100"/&gt;
110 &lt;op op="random"/&gt;
111 &lt;/op&gt;
112 &lt;/op&gt;
113 &lt;/math&gt;
114 &lt;echo&gt;a random number between 0 and 100: ${result}&lt;/echo&gt;
115
116 &lt;!-- demo another multiplication --&gt;
117 &lt;math result="result" operation="multiply" operand1="17" operand2="13"/&gt;
118 &lt;echo&gt;${result}&lt;/echo&gt;
119
120</pre><p>
121</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="bk03ch14.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="bk03.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="bk03ch16.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 14. UID </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 16. Hostname</td></tr></table></div></body></html>
Note: See TracBrowser for help on using the repository browser.