comparison libs/commons-math-2.1/docs/userguide/fraction.html @ 10:5f2c5fb36e93

commons-math-2.1 added
author dwinter
date Tue, 04 Jan 2011 10:00:53 +0100
parents
children
comparison
equal deleted inserted replaced
9:e63a64652f4d 10:5f2c5fb36e93
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
3
4
5
6
7
8
9
10
11
12
13 <html xmlns="http://www.w3.org/1999/xhtml">
14 <head>
15 <title>Math - The Commons Math User Guide - Fractions</title>
16 <style type="text/css" media="all">
17 @import url("../css/maven-base.css");
18 @import url("../css/maven-theme.css");
19 @import url("../css/site.css");
20 </style>
21 <link rel="stylesheet" href="../css/print.css" type="text/css" media="print" />
22 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
23 </head>
24 <body class="composite">
25 <div id="banner">
26 <span id="bannerLeft">
27
28 Commons Math User Guide
29
30 </span>
31 <div class="clear">
32 <hr/>
33 </div>
34 </div>
35 <div id="breadcrumbs">
36
37
38
39
40
41
42
43
44 <div class="xright">
45
46
47
48
49
50
51
52 </div>
53 <div class="clear">
54 <hr/>
55 </div>
56 </div>
57 <div id="leftColumn">
58 <div id="navcolumn">
59
60
61
62
63
64
65
66
67 <h5>User Guide</h5>
68 <ul>
69
70 <li class="none">
71 <a href="../userguide/index.html">Contents</a>
72 </li>
73
74 <li class="none">
75 <a href="../userguide/overview.html">Overview</a>
76 </li>
77
78 <li class="none">
79 <a href="../userguide/stat.html">Statistics</a>
80 </li>
81
82 <li class="none">
83 <a href="../userguide/random.html">Data Generation</a>
84 </li>
85
86 <li class="none">
87 <a href="../userguide/linear.html">Linear Algebra</a>
88 </li>
89
90 <li class="none">
91 <a href="../userguide/analysis.html">Numerical Analysis</a>
92 </li>
93
94 <li class="none">
95 <a href="../userguide/special.html">Special Functions</a>
96 </li>
97
98 <li class="none">
99 <a href="../userguide/utilities.html">Utilities</a>
100 </li>
101
102 <li class="none">
103 <a href="../userguide/complex.html">Complex Numbers</a>
104 </li>
105
106 <li class="none">
107 <a href="../userguide/distribution.html">Distributions</a>
108 </li>
109
110 <li class="none">
111 <strong>Fractions</strong>
112 </li>
113
114 <li class="none">
115 <a href="../userguide/transform.html">Transform Methods</a>
116 </li>
117
118 <li class="none">
119 <a href="../userguide/geometry.html">3D Geometry</a>
120 </li>
121
122 <li class="none">
123 <a href="../userguide/optimization.html">Optimization</a>
124 </li>
125
126 <li class="none">
127 <a href="../userguide/ode.html">Ordinary Differential Equations</a>
128 </li>
129
130 <li class="none">
131 <a href="../userguide/genetics.html">Genetic Algorithms</a>
132 </li>
133 </ul>
134 <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
135 <img alt="Built by Maven" src="../images/logos/maven-feather.png"></img>
136 </a>
137
138
139
140
141
142
143
144
145 </div>
146 </div>
147 <div id="bodyColumn">
148 <div id="contentBox">
149 <div class="section"><h2><a name="a9_Fractions"></a>9 Fractions</h2>
150 <div class="section"><h3><a name="a9.1_Overview"></a>9.1 Overview</h3>
151 <p>
152 The fraction packages provides a fraction number type as well as
153 fraction number formatting.
154 </p>
155 </div>
156 <div class="section"><h3><a name="a9.2_Fraction_Numbers"></a>9.2 Fraction Numbers</h3>
157 <p><a href="../apidocs/org/apache/commons/math/fraction/Fraction.html">
158 org.apache.commons.math.fraction.Fraction</a> and
159 <a href="../apidocs/org/apache/commons/math/fraction/BigFraction.html">
160 org.apache.commons.math.fraction.BigFraction</a> provide fraction number
161 type that forms the basis for the fraction functionality found in
162 commons-math. The former one can be used for fractions whose numerators
163 and denominators are small enough to fit in an int (taking care of intermediate
164 values) while the second class should be used when there is a risk the numerator
165 and denominator grow very large.
166 </p>
167 <p>
168 A fraction number, can be built from two integer arguments representing numerator
169 and denominator or from a double which will be approximated:
170 <div class="source"><pre>Fraction f = new Fraction(1, 3); // 1 / 3
171 Fraction g = new Fraction(0.25); // 1 / 4</pre>
172 </div>
173 </p>
174 <p>
175 Of special note with fraction construction, when a fraction is created it is always reduced to lowest terms.
176 </p>
177 <p>
178 The <code>Fraction</code> class provides many unary and binary
179 fraction operations. These operations provide the means to add,
180 subtract, multiple and, divide fractions along with other functions similar to the real number functions found in
181 <code>java.math.BigDecimal</code>:
182 <div class="source"><pre>Fraction lhs = new Fraction(1, 3);
183 Fraction rhs = new Fraction(2, 5);
184
185 Fraction answer = lhs.add(rhs); // add two fractions
186 answer = lhs.subtract(rhs); // subtract two fractions
187 answer = lhs.abs(); // absolute value
188 answer = lhs.reciprocal(); // reciprocal of lhs</pre>
189 </div>
190 </p>
191 <p>
192 Like fraction construction, for each of the fraction functions, the resulting fraction is reduced to lowest terms.
193 </p>
194 </div>
195 <div class="section"><h3><a name="a9.3_Fraction_Formatting_and_Parsing"></a>9.3 Fraction Formatting and Parsing</h3>
196 <p><code>Fraction</code> instances can be converted to and from strings
197 using the<a href="../apidocs/org/apache/commons/math/fraction/FractionFormat.html">
198 org.apache.commons.math.fraction.FractionFormat</a> class.
199 <code>FractionFormat</code> is a <code>java.text.Format</code>
200 extension and, as such, is used like other formatting objects (e.g.
201 <code>java.text.SimpleDateFormat</code>):
202 <div class="source"><pre>FractionFormat format = new FractionFormat(); // default format
203 Fraction f = new Fraction(2, 4);
204 String s = format.format(f); // s contains &quot;1 / 2&quot;, note the reduced fraction</pre>
205 </div>
206 </p>
207 <p>
208 To customize the formatting output, one or two
209 <code>java.text.NumberFormat</code> instances can be used to construct
210 a <code>FractionFormat</code>. These number formats control the
211 formatting of the numerator and denominator of the fraction:
212 <div class="source"><pre>NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE);
213 // create fraction format with custom number format
214 // when one number format is used, both numerator and
215 // denominator are formatted the same
216 FractionFormat format = new FractionFormat(nf);
217 Fraction f = new Fraction(2000, 3333);
218 String s = format.format(c); // s contains &quot;2.000 / 3.333&quot;
219
220 NumberFormat nf2 = NumberFormat.getInstance(Locale.US);
221 // create fraction format with custom number formats
222 format = new FractionFormat(nf, nf2);
223 s = format.format(f); // s contains &quot;2.000 / 3,333&quot;</pre>
224 </div>
225 </p>
226 <p>
227 Formatting's inverse operation, parsing, can also be performed by
228 <code>FractionFormat</code>. To parse a fraction from a string,
229 simply call the <code>parse</code> method:
230 <div class="source"><pre>FractionFormat ff = new FractionFormat();
231 Fraction f = ff.parse(&quot;-10 / 21&quot;);</pre>
232 </div>
233 </p>
234 </div>
235 </div>
236
237 </div>
238 </div>
239 <div class="clear">
240 <hr/>
241 </div>
242 <div id="footer">
243 <div class="xright">&#169;
244 2003-2010
245
246
247
248
249
250
251
252
253
254 </div>
255 <div class="clear">
256 <hr/>
257 </div>
258 </div>
259 </body>
260 </html>