Commit | Line | Data |
---|---|---|
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="decimal-decimal.html" /> | |
13 | <link rel="prev" href="decimal-tutorial.html" /> | |
14 | <link rel="parent" href="module-decimal.html" /> | |
15 | <link rel="next" href="decimal-decimal.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>5.6.2 Decimal objects </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.1 Quick-start Tutorial" | |
25 | href="decimal-tutorial.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.3 Context objects" | |
31 | href="decimal-decimal.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="decimal-tutorial.html">5.6.1 Quick-start Tutorial</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="decimal-decimal.html">5.6.3 Context objects</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION007620000000000000000"></A><A NAME="decimal-decimal"></A> | |
56 | <BR> | |
57 | 5.6.2 Decimal objects | |
58 | </H2> | |
59 | ||
60 | <P> | |
61 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
62 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-1195' xml:id='l2h-1195' class="class">Decimal</tt></b>(</nobr></td> | |
63 | <td><var></var><big>[</big><var>value </var><big>[</big><var>, context</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
64 | <dd> | |
65 | Constructs a new <tt class="class">Decimal</tt> object based from <var>value</var>. | |
66 | ||
67 | <P> | |
68 | <var>value</var> can be an integer, string, tuple, or another <tt class="class">Decimal</tt> | |
69 | object. If no <var>value</var> is given, returns <code>Decimal("0")</code>. If | |
70 | <var>value</var> is a string, it should conform to the decimal numeric string | |
71 | syntax: | |
72 | ||
73 | <P> | |
74 | <div class="verbatim"><pre> | |
75 | sign ::= '+' | '-' | |
76 | digit ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | |
77 | indicator ::= 'e' | 'E' | |
78 | digits ::= digit [digit]... | |
79 | decimal-part ::= digits '.' [digits] | ['.'] digits | |
80 | exponent-part ::= indicator [sign] digits | |
81 | infinity ::= 'Infinity' | 'Inf' | |
82 | nan ::= 'NaN' [digits] | 'sNaN' [digits] | |
83 | numeric-value ::= decimal-part [exponent-part] | infinity | |
84 | numeric-string ::= [sign] numeric-value | [sign] nan | |
85 | </pre></div> | |
86 | ||
87 | <P> | |
88 | If <var>value</var> is a <tt class="class">tuple</tt>, it should have three components, | |
89 | a sign (<tt class="constant">0</tt> for positive or <tt class="constant">1</tt> for negative), | |
90 | a <tt class="class">tuple</tt> of digits, and an integer exponent. For example, | |
91 | "<tt class="samp">Decimal((0, (1, 4, 1, 4), -3))</tt>" returns <code>Decimal("1.414")</code>. | |
92 | ||
93 | <P> | |
94 | The <var>context</var> precision does not affect how many digits are stored. | |
95 | That is determined exclusively by the number of digits in <var>value</var>. For | |
96 | example, "<tt class="samp">Decimal("3.00000")</tt>" records all five zeroes even if the | |
97 | context precision is only three. | |
98 | ||
99 | <P> | |
100 | The purpose of the <var>context</var> argument is determining what to do if | |
101 | <var>value</var> is a malformed string. If the context traps | |
102 | <tt class="constant">InvalidOperation</tt>, an exception is raised; otherwise, the | |
103 | constructor returns a new Decimal with the value of <tt class="constant">NaN</tt>. | |
104 | ||
105 | <P> | |
106 | Once constructed, <tt class="class">Decimal</tt> objects are immutable. | |
107 | </dl> | |
108 | ||
109 | <P> | |
110 | Decimal floating point objects share many properties with the other builtin | |
111 | numeric types such as <tt class="class">float</tt> and <tt class="class">int</tt>. All of the usual | |
112 | math operations and special methods apply. Likewise, decimal objects can | |
113 | be copied, pickled, printed, used as dictionary keys, used as set elements, | |
114 | compared, sorted, and coerced to another type (such as <tt class="class">float</tt> | |
115 | or <tt class="class">long</tt>). | |
116 | ||
117 | <P> | |
118 | In addition to the standard numeric properties, decimal floating point objects | |
119 | also have a number of specialized methods: | |
120 | ||
121 | <P> | |
122 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
123 | <td><nobr><b><tt id='l2h-1196' xml:id='l2h-1196' class="method">adjusted</tt></b>(</nobr></td> | |
124 | <td><var></var>)</td></tr></table></dt> | |
125 | <dd> | |
126 | Return the adjusted exponent after shifting out the coefficient's rightmost | |
127 | digits until only the lead digit remains: <code>Decimal("321e+5").adjusted()</code> | |
128 | returns seven. Used for determining the position of the most significant | |
129 | digit with respect to the decimal point. | |
130 | </dl> | |
131 | ||
132 | <P> | |
133 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
134 | <td><nobr><b><tt id='l2h-1197' xml:id='l2h-1197' class="method">as_tuple</tt></b>(</nobr></td> | |
135 | <td><var></var>)</td></tr></table></dt> | |
136 | <dd> | |
137 | Returns a tuple representation of the number: | |
138 | "<tt class="samp">(sign, digittuple, exponent)</tt>". | |
139 | </dl> | |
140 | ||
141 | <P> | |
142 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
143 | <td><nobr><b><tt id='l2h-1198' xml:id='l2h-1198' class="method">compare</tt></b>(</nobr></td> | |
144 | <td><var>other</var><big>[</big><var>, context</var><big>]</big><var></var>)</td></tr></table></dt> | |
145 | <dd> | |
146 | Compares like <tt class="method">__cmp__()</tt> but returns a decimal instance: | |
147 | <div class="verbatim"><pre> | |
148 | a or b is a NaN ==> Decimal("NaN") | |
149 | a < b ==> Decimal("-1") | |
150 | a == b ==> Decimal("0") | |
151 | a > b ==> Decimal("1") | |
152 | </pre></div> | |
153 | </dl> | |
154 | ||
155 | <P> | |
156 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
157 | <td><nobr><b><tt id='l2h-1199' xml:id='l2h-1199' class="method">max</tt></b>(</nobr></td> | |
158 | <td><var>other</var><big>[</big><var>, context</var><big>]</big><var></var>)</td></tr></table></dt> | |
159 | <dd> | |
160 | Like "<tt class="samp">max(self, other)</tt>" except that the context rounding rule | |
161 | is applied before returning and that <tt class="constant">NaN</tt> values are | |
162 | either signalled or ignored (depending on the context and whether | |
163 | they are signaling or quiet). | |
164 | </dl> | |
165 | ||
166 | <P> | |
167 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
168 | <td><nobr><b><tt id='l2h-1200' xml:id='l2h-1200' class="method">min</tt></b>(</nobr></td> | |
169 | <td><var>other</var><big>[</big><var>, context</var><big>]</big><var></var>)</td></tr></table></dt> | |
170 | <dd> | |
171 | Like "<tt class="samp">min(self, other)</tt>" except that the context rounding rule | |
172 | is applied before returning and that <tt class="constant">NaN</tt> values are | |
173 | either signalled or ignored (depending on the context and whether | |
174 | they are signaling or quiet). | |
175 | </dl> | |
176 | ||
177 | <P> | |
178 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
179 | <td><nobr><b><tt id='l2h-1201' xml:id='l2h-1201' class="method">normalize</tt></b>(</nobr></td> | |
180 | <td><var></var><big>[</big><var>context</var><big>]</big><var></var>)</td></tr></table></dt> | |
181 | <dd> | |
182 | Normalize the number by stripping the rightmost trailing zeroes and | |
183 | converting any result equal to <tt class="constant">Decimal("0")</tt> to | |
184 | <tt class="constant">Decimal("0e0")</tt>. Used for producing canonical values for members | |
185 | of an equivalence class. For example, <code>Decimal("32.100")</code> and | |
186 | <code>Decimal("0.321000e+2")</code> both normalize to the equivalent value | |
187 | <code>Decimal("32.1")</code>. | |
188 | </dl> | |
189 | ||
190 | <P> | |
191 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
192 | <td><nobr><b><tt id='l2h-1202' xml:id='l2h-1202' class="method">quantize</tt></b>(</nobr></td> | |
193 | <td><var>exp </var><big>[</big><var>, rounding</var><big>[</big><var>, context</var><big>[</big><var>, watchexp</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
194 | <dd> | |
195 | Quantize makes the exponent the same as <var>exp</var>. Searches for a | |
196 | rounding method in <var>rounding</var>, then in <var>context</var>, and then | |
197 | in the current context. | |
198 | ||
199 | <P> | |
200 | If <var>watchexp</var> is set (default), then an error is returned whenever | |
201 | the resulting exponent is greater than <tt class="member">Emax</tt> or less than | |
202 | <tt class="member">Etiny</tt>. | |
203 | </dl> | |
204 | ||
205 | <P> | |
206 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
207 | <td><nobr><b><tt id='l2h-1203' xml:id='l2h-1203' class="method">remainder_near</tt></b>(</nobr></td> | |
208 | <td><var>other</var><big>[</big><var>, context</var><big>]</big><var></var>)</td></tr></table></dt> | |
209 | <dd> | |
210 | Computes the modulo as either a positive or negative value depending | |
211 | on which is closest to zero. For instance, | |
212 | "<tt class="samp">Decimal(10).remainder_near(6)</tt>" returns <code>Decimal("-2")</code> | |
213 | which is closer to zero than <code>Decimal("4")</code>. | |
214 | ||
215 | <P> | |
216 | If both are equally close, the one chosen will have the same sign | |
217 | as <var>self</var>. | |
218 | </dl> | |
219 | ||
220 | <P> | |
221 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
222 | <td><nobr><b><tt id='l2h-1204' xml:id='l2h-1204' class="method">same_quantum</tt></b>(</nobr></td> | |
223 | <td><var>other</var><big>[</big><var>, context</var><big>]</big><var></var>)</td></tr></table></dt> | |
224 | <dd> | |
225 | Test whether self and other have the same exponent or whether both | |
226 | are <tt class="constant">NaN</tt>. | |
227 | </dl> | |
228 | ||
229 | <P> | |
230 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
231 | <td><nobr><b><tt id='l2h-1205' xml:id='l2h-1205' class="method">sqrt</tt></b>(</nobr></td> | |
232 | <td><var></var><big>[</big><var>context</var><big>]</big><var></var>)</td></tr></table></dt> | |
233 | <dd> | |
234 | Return the square root to full precision. | |
235 | </dl> | |
236 | ||
237 | <P> | |
238 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
239 | <td><nobr><b><tt id='l2h-1206' xml:id='l2h-1206' class="method">to_eng_string</tt></b>(</nobr></td> | |
240 | <td><var></var><big>[</big><var>context</var><big>]</big><var></var>)</td></tr></table></dt> | |
241 | <dd> | |
242 | Convert to an engineering-type string. | |
243 | ||
244 | <P> | |
245 | Engineering notation has an exponent which is a multiple of 3, so there | |
246 | are up to 3 digits left of the decimal place. For example, converts | |
247 | <code>Decimal('123E+1')</code> to <code>Decimal("1.23E+3")</code> | |
248 | </dl> | |
249 | ||
250 | <P> | |
251 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
252 | <td><nobr><b><tt id='l2h-1207' xml:id='l2h-1207' class="method">to_integral</tt></b>(</nobr></td> | |
253 | <td><var></var><big>[</big><var>rounding</var><big>[</big><var>, context</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
254 | <dd> | |
255 | Rounds to the nearest integer without signaling <tt class="constant">Inexact</tt> | |
256 | or <tt class="constant">Rounded</tt>. If given, applies <var>rounding</var>; otherwise, | |
257 | uses the rounding method in either the supplied <var>context</var> or the | |
258 | current context. | |
259 | </dl> | |
260 | ||
261 | <P> | |
262 | ||
263 | <DIV CLASS="navigation"> | |
264 | <div class='online-navigation'> | |
265 | <p></p><hr /> | |
266 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
267 | <tr> | |
268 | <td class='online-navigation'><a rel="prev" title="5.6.1 Quick-start Tutorial" | |
269 | href="decimal-tutorial.html"><img src='../icons/previous.png' | |
270 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
271 | <td class='online-navigation'><a rel="parent" title="5.6 decimal " | |
272 | href="module-decimal.html"><img src='../icons/up.png' | |
273 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
274 | <td class='online-navigation'><a rel="next" title="5.6.3 Context objects" | |
275 | href="decimal-decimal.html"><img src='../icons/next.png' | |
276 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
277 | <td align="center" width="100%">Python Library Reference</td> | |
278 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
279 | href="contents.html"><img src='../icons/contents.png' | |
280 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
281 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
282 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
283 | <td class='online-navigation'><a rel="index" title="Index" | |
284 | href="genindex.html"><img src='../icons/index.png' | |
285 | border='0' height='32' alt='Index' width='32' /></A></td> | |
286 | </tr></table> | |
287 | <div class='online-navigation'> | |
288 | <b class="navlabel">Previous:</b> | |
289 | <a class="sectref" rel="prev" href="decimal-tutorial.html">5.6.1 Quick-start Tutorial</A> | |
290 | <b class="navlabel">Up:</b> | |
291 | <a class="sectref" rel="parent" href="module-decimal.html">5.6 decimal </A> | |
292 | <b class="navlabel">Next:</b> | |
293 | <a class="sectref" rel="next" href="decimal-decimal.html">5.6.3 Context objects</A> | |
294 | </div> | |
295 | </div> | |
296 | <hr /> | |
297 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
298 | </DIV> | |
299 | <!--End of Navigation Panel--> | |
300 | <ADDRESS> | |
301 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
302 | </ADDRESS> | |
303 | </BODY> | |
304 | </HTML> |