| 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> |
| 57 | 5.6.1 Quick-start Tutorial |
| 58 | </H2> |
| 59 | |
| 60 | <P> |
| 61 | The usual start to using decimals is importing the module, viewing the current |
| 62 | context with <tt class="function">getcontext()</tt> and, if necessary, setting new values |
| 63 | for precision, rounding, or enabled traps: |
| 64 | |
| 65 | <P> |
| 66 | <div class="verbatim"><pre> |
| 67 | >>> from decimal import * |
| 68 | >>> getcontext() |
| 69 | Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, |
| 70 | capitals=1, flags=[], traps=[Overflow, InvalidOperation, |
| 71 | DivisionByZero]) |
| 72 | |
| 73 | >>> getcontext().prec = 7 # Set a new precision |
| 74 | </pre></div> |
| 75 | |
| 76 | <P> |
| 77 | Decimal instances can be constructed from integers, strings or tuples. To |
| 78 | create a Decimal from a <tt class="class">float</tt>, first convert it to a string. This |
| 79 | serves as an explicit reminder of the details of the conversion (including |
| 80 | representation 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 | >>> Decimal(10) |
| 87 | Decimal("10") |
| 88 | >>> Decimal("3.14") |
| 89 | Decimal("3.14") |
| 90 | >>> Decimal((0, (3, 1, 4), -2)) |
| 91 | Decimal("3.14") |
| 92 | >>> Decimal(str(2.0 ** 0.5)) |
| 93 | Decimal("1.41421356237") |
| 94 | >>> Decimal("NaN") |
| 95 | Decimal("NaN") |
| 96 | >>> Decimal("-Infinity") |
| 97 | Decimal("-Infinity") |
| 98 | </pre></div> |
| 99 | |
| 100 | <P> |
| 101 | The significance of a new Decimal is determined solely by the number |
| 102 | of digits input. Context precision and rounding only come into play during |
| 103 | arithmetic operations. |
| 104 | |
| 105 | <P> |
| 106 | <div class="verbatim"><pre> |
| 107 | >>> getcontext().prec = 6 |
| 108 | >>> Decimal('3.0') |
| 109 | Decimal("3.0") |
| 110 | >>> Decimal('3.1415926535') |
| 111 | Decimal("3.1415926535") |
| 112 | >>> Decimal('3.1415926535') + Decimal('2.7182818285') |
| 113 | Decimal("5.85987") |
| 114 | >>> getcontext().rounding = ROUND_UP |
| 115 | >>> Decimal('3.1415926535') + Decimal('2.7182818285') |
| 116 | Decimal("5.85988") |
| 117 | </pre></div> |
| 118 | |
| 119 | <P> |
| 120 | Decimals interact well with much of the rest of python. Here is a small |
| 121 | decimal floating point flying circus: |
| 122 | |
| 123 | <P> |
| 124 | <div class="verbatim"><pre> |
| 125 | >>> data = map(Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split()) |
| 126 | >>> max(data) |
| 127 | Decimal("9.25") |
| 128 | >>> min(data) |
| 129 | Decimal("0.03") |
| 130 | >>> 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 | >>> sum(data) |
| 134 | Decimal("19.29") |
| 135 | >>> a,b,c = data[:3] |
| 136 | >>> str(a) |
| 137 | '1.34' |
| 138 | >>> float(a) |
| 139 | 1.3400000000000001 |
| 140 | >>> round(a, 1) # round() first converts to binary floating point |
| 141 | 1.3 |
| 142 | >>> int(a) |
| 143 | 1 |
| 144 | >>> a * 5 |
| 145 | Decimal("6.70") |
| 146 | >>> a * b |
| 147 | Decimal("2.5058") |
| 148 | >>> c % a |
| 149 | Decimal("0.77") |
| 150 | </pre></div> |
| 151 | |
| 152 | <P> |
| 153 | The <tt class="method">quantize()</tt> method rounds a number to a fixed exponent. This |
| 154 | method is useful for monetary applications that often round results to a fixed |
| 155 | number of places: |
| 156 | |
| 157 | <P> |
| 158 | <div class="verbatim"><pre> |
| 159 | >>> Decimal('7.325').quantize(Decimal('.01'), rounding=ROUND_DOWN) |
| 160 | Decimal("7.32") |
| 161 | >>> Decimal('7.325').quantize(Decimal('1.'), rounding=ROUND_UP) |
| 162 | Decimal("8") |
| 163 | </pre></div> |
| 164 | |
| 165 | <P> |
| 166 | As shown above, the <tt class="function">getcontext()</tt> function accesses the current |
| 167 | context and allows the settings to be changed. This approach meets the |
| 168 | needs of most applications. |
| 169 | |
| 170 | <P> |
| 171 | For more advanced work, it may be useful to create alternate contexts using |
| 172 | the Context() constructor. To make an alternate active, use the |
| 173 | <tt class="function">setcontext()</tt> function. |
| 174 | |
| 175 | <P> |
| 176 | In accordance with the standard, the <tt class="module">Decimal</tt> module provides two |
| 177 | ready to use standard contexts, <tt class="constant">BasicContext</tt> and |
| 178 | <tt class="constant">ExtendedContext</tt>. The former is especially useful for debugging |
| 179 | because many of the traps are enabled: |
| 180 | |
| 181 | <P> |
| 182 | <div class="verbatim"><pre> |
| 183 | >>> myothercontext = Context(prec=60, rounding=ROUND_HALF_DOWN) |
| 184 | >>> setcontext(myothercontext) |
| 185 | >>> Decimal(1) / Decimal(7) |
| 186 | Decimal("0.142857142857142857142857142857142857142857142857142857142857") |
| 187 | |
| 188 | >>> ExtendedContext |
| 189 | Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, |
| 190 | capitals=1, flags=[], traps=[]) |
| 191 | >>> setcontext(ExtendedContext) |
| 192 | >>> Decimal(1) / Decimal(7) |
| 193 | Decimal("0.142857143") |
| 194 | >>> Decimal(42) / Decimal(0) |
| 195 | Decimal("Infinity") |
| 196 | |
| 197 | >>> setcontext(BasicContext) |
| 198 | >>> Decimal(42) / Decimal(0) |
| 199 | Traceback (most recent call last): |
| 200 | File "<pyshell#143>", line 1, in -toplevel- |
| 201 | Decimal(42) / Decimal(0) |
| 202 | DivisionByZero: x / 0 |
| 203 | </pre></div> |
| 204 | |
| 205 | <P> |
| 206 | Contexts also have signal flags for monitoring exceptional conditions |
| 207 | encountered during computations. The flags remain set until explicitly |
| 208 | cleared, so it is best to clear the flags before each set of monitored |
| 209 | computations by using the <tt class="method">clear_flags()</tt> method. |
| 210 | |
| 211 | <P> |
| 212 | <div class="verbatim"><pre> |
| 213 | >>> setcontext(ExtendedContext) |
| 214 | >>> getcontext().clear_flags() |
| 215 | >>> Decimal(355) / Decimal(113) |
| 216 | Decimal("3.14159292") |
| 217 | >>> getcontext() |
| 218 | Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, |
| 219 | capitals=1, flags=[Inexact, Rounded], traps=[]) |
| 220 | </pre></div> |
| 221 | |
| 222 | <P> |
| 223 | The <var>flags</var> entry shows that the rational approximation to <tt class="constant">Pi</tt> |
| 224 | was rounded (digits beyond the context precision were thrown away) and that |
| 225 | the result is inexact (some of the discarded digits were non-zero). |
| 226 | |
| 227 | <P> |
| 228 | Individual traps are set using the dictionary in the <tt class="member">traps</tt> |
| 229 | field of a context: |
| 230 | |
| 231 | <P> |
| 232 | <div class="verbatim"><pre> |
| 233 | >>> Decimal(1) / Decimal(0) |
| 234 | Decimal("Infinity") |
| 235 | >>> getcontext().traps[DivisionByZero] = 1 |
| 236 | >>> Decimal(1) / Decimal(0) |
| 237 | Traceback (most recent call last): |
| 238 | File "<pyshell#112>", line 1, in -toplevel- |
| 239 | Decimal(1) / Decimal(0) |
| 240 | DivisionByZero: x / 0 |
| 241 | </pre></div> |
| 242 | |
| 243 | <P> |
| 244 | Most programs adjust the current context only once, at the beginning of the |
| 245 | program. And, in many applications, data is converted to <tt class="class">Decimal</tt> with |
| 246 | a single cast inside a loop. With context set and decimals created, the bulk |
| 247 | of the program manipulates the data no differently than with other Python |
| 248 | numeric 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> |
| 290 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 291 | </ADDRESS> |
| 292 | </BODY> |
| 293 | </HTML> |