Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / decimal-tutorial.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="next" href="node178.html" />
13<link rel="prev" href="module-decimal.html" />
14<link rel="parent" href="module-decimal.html" />
15<link rel="next" href="node178.html" />
16<meta name='aesop' content='information' />
17<title>5.6.1 Quick-start Tutorial </title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="5.6 decimal "
25 href="module-decimal.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="5.6 decimal "
28 href="module-decimal.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="5.6.2 Decimal objects"
31 href="node178.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="module-decimal.html">5.6 decimal </A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="module-decimal.html">5.6 decimal </A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="node178.html">5.6.2 Decimal objects</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H2><A NAME="SECTION007610000000000000000"></A><A NAME="decimal-tutorial"></A>
56<BR>
575.6.1 Quick-start Tutorial
58</H2>
59
60<P>
61The usual start to using decimals is importing the module, viewing the current
62context with <tt class="function">getcontext()</tt> and, if necessary, setting new values
63for precision, rounding, or enabled traps:
64
65<P>
66<div class="verbatim"><pre>
67&gt;&gt;&gt; from decimal import *
68&gt;&gt;&gt; getcontext()
69Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,
70 capitals=1, flags=[], traps=[Overflow, InvalidOperation,
71 DivisionByZero])
72
73&gt;&gt;&gt; getcontext().prec = 7 # Set a new precision
74</pre></div>
75
76<P>
77Decimal instances can be constructed from integers, strings or tuples. To
78create a Decimal from a <tt class="class">float</tt>, first convert it to a string. This
79serves as an explicit reminder of the details of the conversion (including
80representation error). Decimal numbers include special values such as
81<tt class="constant">NaN</tt> which stands for ``Not a number'', positive and negative
82<tt class="constant">Infinity</tt>, and <tt class="constant">-0</tt>.
83
84<P>
85<div class="verbatim"><pre>
86&gt;&gt;&gt; Decimal(10)
87Decimal("10")
88&gt;&gt;&gt; Decimal("3.14")
89Decimal("3.14")
90&gt;&gt;&gt; Decimal((0, (3, 1, 4), -2))
91Decimal("3.14")
92&gt;&gt;&gt; Decimal(str(2.0 ** 0.5))
93Decimal("1.41421356237")
94&gt;&gt;&gt; Decimal("NaN")
95Decimal("NaN")
96&gt;&gt;&gt; Decimal("-Infinity")
97Decimal("-Infinity")
98</pre></div>
99
100<P>
101The significance of a new Decimal is determined solely by the number
102of digits input. Context precision and rounding only come into play during
103arithmetic operations.
104
105<P>
106<div class="verbatim"><pre>
107&gt;&gt;&gt; getcontext().prec = 6
108&gt;&gt;&gt; Decimal('3.0')
109Decimal("3.0")
110&gt;&gt;&gt; Decimal('3.1415926535')
111Decimal("3.1415926535")
112&gt;&gt;&gt; Decimal('3.1415926535') + Decimal('2.7182818285')
113Decimal("5.85987")
114&gt;&gt;&gt; getcontext().rounding = ROUND_UP
115&gt;&gt;&gt; Decimal('3.1415926535') + Decimal('2.7182818285')
116Decimal("5.85988")
117</pre></div>
118
119<P>
120Decimals interact well with much of the rest of python. Here is a small
121decimal floating point flying circus:
122
123<P>
124<div class="verbatim"><pre>
125&gt;&gt;&gt; data = map(Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split())
126&gt;&gt;&gt; max(data)
127Decimal("9.25")
128&gt;&gt;&gt; min(data)
129Decimal("0.03")
130&gt;&gt;&gt; sorted(data)
131[Decimal("0.03"), Decimal("1.00"), Decimal("1.34"), Decimal("1.87"),
132 Decimal("2.35"), Decimal("3.45"), Decimal("9.25")]
133&gt;&gt;&gt; sum(data)
134Decimal("19.29")
135&gt;&gt;&gt; a,b,c = data[:3]
136&gt;&gt;&gt; str(a)
137'1.34'
138&gt;&gt;&gt; float(a)
1391.3400000000000001
140&gt;&gt;&gt; round(a, 1) # round() first converts to binary floating point
1411.3
142&gt;&gt;&gt; int(a)
1431
144&gt;&gt;&gt; a * 5
145Decimal("6.70")
146&gt;&gt;&gt; a * b
147Decimal("2.5058")
148&gt;&gt;&gt; c % a
149Decimal("0.77")
150</pre></div>
151
152<P>
153The <tt class="method">quantize()</tt> method rounds a number to a fixed exponent. This
154method is useful for monetary applications that often round results to a fixed
155number of places:
156
157<P>
158<div class="verbatim"><pre>
159&gt;&gt;&gt; Decimal('7.325').quantize(Decimal('.01'), rounding=ROUND_DOWN)
160Decimal("7.32")
161&gt;&gt;&gt; Decimal('7.325').quantize(Decimal('1.'), rounding=ROUND_UP)
162Decimal("8")
163</pre></div>
164
165<P>
166As shown above, the <tt class="function">getcontext()</tt> function accesses the current
167context and allows the settings to be changed. This approach meets the
168needs of most applications.
169
170<P>
171For more advanced work, it may be useful to create alternate contexts using
172the Context() constructor. To make an alternate active, use the
173<tt class="function">setcontext()</tt> function.
174
175<P>
176In accordance with the standard, the <tt class="module">Decimal</tt> module provides two
177ready to use standard contexts, <tt class="constant">BasicContext</tt> and
178<tt class="constant">ExtendedContext</tt>. The former is especially useful for debugging
179because many of the traps are enabled:
180
181<P>
182<div class="verbatim"><pre>
183&gt;&gt;&gt; myothercontext = Context(prec=60, rounding=ROUND_HALF_DOWN)
184&gt;&gt;&gt; setcontext(myothercontext)
185&gt;&gt;&gt; Decimal(1) / Decimal(7)
186Decimal("0.142857142857142857142857142857142857142857142857142857142857")
187
188&gt;&gt;&gt; ExtendedContext
189Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,
190 capitals=1, flags=[], traps=[])
191&gt;&gt;&gt; setcontext(ExtendedContext)
192&gt;&gt;&gt; Decimal(1) / Decimal(7)
193Decimal("0.142857143")
194&gt;&gt;&gt; Decimal(42) / Decimal(0)
195Decimal("Infinity")
196
197&gt;&gt;&gt; setcontext(BasicContext)
198&gt;&gt;&gt; Decimal(42) / Decimal(0)
199Traceback (most recent call last):
200 File "&lt;pyshell#143&gt;", line 1, in -toplevel-
201 Decimal(42) / Decimal(0)
202DivisionByZero: x / 0
203</pre></div>
204
205<P>
206Contexts also have signal flags for monitoring exceptional conditions
207encountered during computations. The flags remain set until explicitly
208cleared, so it is best to clear the flags before each set of monitored
209computations by using the <tt class="method">clear_flags()</tt> method.
210
211<P>
212<div class="verbatim"><pre>
213&gt;&gt;&gt; setcontext(ExtendedContext)
214&gt;&gt;&gt; getcontext().clear_flags()
215&gt;&gt;&gt; Decimal(355) / Decimal(113)
216Decimal("3.14159292")
217&gt;&gt;&gt; getcontext()
218Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999,
219 capitals=1, flags=[Inexact, Rounded], traps=[])
220</pre></div>
221
222<P>
223The <var>flags</var> entry shows that the rational approximation to <tt class="constant">Pi</tt>
224was rounded (digits beyond the context precision were thrown away) and that
225the result is inexact (some of the discarded digits were non-zero).
226
227<P>
228Individual traps are set using the dictionary in the <tt class="member">traps</tt>
229field of a context:
230
231<P>
232<div class="verbatim"><pre>
233&gt;&gt;&gt; Decimal(1) / Decimal(0)
234Decimal("Infinity")
235&gt;&gt;&gt; getcontext().traps[DivisionByZero] = 1
236&gt;&gt;&gt; Decimal(1) / Decimal(0)
237Traceback (most recent call last):
238 File "&lt;pyshell#112&gt;", line 1, in -toplevel-
239 Decimal(1) / Decimal(0)
240DivisionByZero: x / 0
241</pre></div>
242
243<P>
244Most programs adjust the current context only once, at the beginning of the
245program. And, in many applications, data is converted to <tt class="class">Decimal</tt> with
246a single cast inside a loop. With context set and decimals created, the bulk
247of the program manipulates the data no differently than with other Python
248numeric types.
249
250<P>
251
252<DIV CLASS="navigation">
253<div class='online-navigation'>
254<p></p><hr />
255<table align="center" width="100%" cellpadding="0" cellspacing="2">
256<tr>
257<td class='online-navigation'><a rel="prev" title="5.6 decimal "
258 href="module-decimal.html"><img src='../icons/previous.png'
259 border='0' height='32' alt='Previous Page' width='32' /></A></td>
260<td class='online-navigation'><a rel="parent" title="5.6 decimal "
261 href="module-decimal.html"><img src='../icons/up.png'
262 border='0' height='32' alt='Up One Level' width='32' /></A></td>
263<td class='online-navigation'><a rel="next" title="5.6.2 Decimal objects"
264 href="node178.html"><img src='../icons/next.png'
265 border='0' height='32' alt='Next Page' width='32' /></A></td>
266<td align="center" width="100%">Python Library Reference</td>
267<td class='online-navigation'><a rel="contents" title="Table of Contents"
268 href="contents.html"><img src='../icons/contents.png'
269 border='0' height='32' alt='Contents' width='32' /></A></td>
270<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
271 border='0' height='32' alt='Module Index' width='32' /></a></td>
272<td class='online-navigation'><a rel="index" title="Index"
273 href="genindex.html"><img src='../icons/index.png'
274 border='0' height='32' alt='Index' width='32' /></A></td>
275</tr></table>
276<div class='online-navigation'>
277<b class="navlabel">Previous:</b>
278<a class="sectref" rel="prev" href="module-decimal.html">5.6 decimal </A>
279<b class="navlabel">Up:</b>
280<a class="sectref" rel="parent" href="module-decimal.html">5.6 decimal </A>
281<b class="navlabel">Next:</b>
282<a class="sectref" rel="next" href="node178.html">5.6.2 Decimal objects</A>
283</div>
284</div>
285<hr />
286<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
287</DIV>
288<!--End of Navigation Panel-->
289<ADDRESS>
290See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
291</ADDRESS>
292</BODY>
293</HTML>