Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / typesnumeric.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="typeiter.html" />
13<link rel="prev" href="comparisons.html" />
14<link rel="parent" href="types.html" />
15<link rel="next" href="bitstring-ops.html" />
16<meta name='aesop' content='information' />
17<title>2.3.4 Numeric Types -- int, float, long, complex </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="2.3.3 Comparisons"
25 href="comparisons.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="2.3 Built-in Types"
28 href="types.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="2.3.4.1 Bit-string Operations on"
31 href="bitstring-ops.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="comparisons.html">2.3.3 Comparisons</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="types.html">2.3 Built-in Types</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="bitstring-ops.html">2.3.4.1 Bit-string Operations on</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H2><A NAME="SECTION004340000000000000000"></A><A NAME="typesnumeric"></A>
56<BR>
572.3.4 Numeric Types --
58 <tt class="class">int</tt>, <tt class="class">float</tt>, <tt class="class">long</tt>, <tt class="class">complex</tt>
59
60</H2>
61
62<P>
63There are four distinct numeric types: <i class="dfn">plain integers</i>,
64<i class="dfn">long integers</i>,
65<i class="dfn">floating point numbers</i>, and <i class="dfn">complex numbers</i>.
66In addition, Booleans are a subtype of plain integers.
67Plain integers (also just called <i class="dfn">integers</i>)
68are implemented using <tt class="ctype">long</tt> in C, which gives them at least 32
69bits of precision. Long integers have unlimited precision. Floating
70point numbers are implemented using <tt class="ctype">double</tt> in C. All bets on
71their precision are off unless you happen to know the machine you are
72working with.
73<a id='l2h-119' xml:id='l2h-119'></a><a id='l2h-120' xml:id='l2h-120'></a><a id='l2h-121' xml:id='l2h-121'></a><a id='l2h-122' xml:id='l2h-122'></a><a id='l2h-123' xml:id='l2h-123'></a><a id='l2h-124' xml:id='l2h-124'></a><a id='l2h-125' xml:id='l2h-125'></a>
74<P>
75Complex numbers have a real and imaginary part, which are each
76implemented using <tt class="ctype">double</tt> in C. To extract these parts from
77a complex number <var>z</var>, use <code><var>z</var>.real</code> and <code><var>z</var>.imag</code>.
78
79<P>
80Numbers are created by numeric literals or as the result of built-in
81functions and operators. Unadorned integer literals (including hex
82and octal numbers) yield plain integers unless the value they denote
83is too large to be represented as a plain integer, in which case
84they yield a long integer. Integer literals with an
85"<tt class="character">L</tt>" or "<tt class="character">l</tt>" suffix yield long integers
86("<tt class="character">L</tt>" is preferred because "<tt class="samp">1l</tt>" looks too much like
87eleven!). Numeric literals containing a decimal point or an exponent
88sign yield floating point numbers. Appending "<tt class="character">j</tt>" or
89"<tt class="character">J</tt>" to a numeric literal yields a complex number with a
90zero real part. A complex numeric literal is the sum of a real and
91an imaginary part.
92<a id='l2h-126' xml:id='l2h-126'></a><a id='l2h-127' xml:id='l2h-127'></a><a id='l2h-128' xml:id='l2h-128'></a><a id='l2h-129' xml:id='l2h-129'></a><a id='l2h-130' xml:id='l2h-130'></a><a id='l2h-131' xml:id='l2h-131'></a><a id='l2h-132' xml:id='l2h-132'></a>
93<P>
94Python fully supports mixed arithmetic: when a binary arithmetic
95operator has operands of different numeric types, the operand with the
96``narrower'' type is widened to that of the other, where plain
97integer is narrower than long integer is narrower than floating point is
98narrower than complex.
99Comparisons between numbers of mixed type use the same rule.<A NAME="tex2html7"
100 HREF="#foot3444"><SUP>2.6</SUP></A> The constructors <tt class="function">int()</tt>, <tt class="function">long()</tt>, <tt class="function">float()</tt>,
101and <tt class="function">complex()</tt> can be used
102to produce numbers of a specific type.
103<a id='l2h-146' xml:id='l2h-146'></a>
104<a id='l2h-133' xml:id='l2h-133'></a><a id='l2h-134' xml:id='l2h-134'></a><a id='l2h-135' xml:id='l2h-135'></a><a id='l2h-136' xml:id='l2h-136'></a>
105<P>
106All numeric types (except complex) support the following operations,
107sorted by ascending priority (operations in the same box have the same
108priority; all numeric operations have a higher priority than
109comparison operations):
110
111<P>
112<div class="center"><table class="realtable">
113 <thead>
114 <tr>
115 <th class="center">Operation</th>
116 <th class="left" >Result</th>
117 <th class="center">Notes</th>
118 </tr>
119 </thead>
120 <tbody>
121 <tr><td class="center" valign="baseline"><code><var>x</var> + <var>y</var></code></td>
122 <td class="left" >sum of <var>x</var> and <var>y</var></td>
123 <td class="center"></td></tr>
124 <tr><td class="center" valign="baseline"><code><var>x</var> - <var>y</var></code></td>
125 <td class="left" >difference of <var>x</var> and <var>y</var></td>
126 <td class="center"></td></tr>
127
128 <tr><td class="center" valign="baseline"><code><var>x</var> * <var>y</var></code></td>
129 <td class="left" >product of <var>x</var> and <var>y</var></td>
130 <td class="center"></td></tr>
131 <tr><td class="center" valign="baseline"><code><var>x</var> / <var>y</var></code></td>
132 <td class="left" >quotient of <var>x</var> and <var>y</var></td>
133 <td class="center">(1)</td></tr>
134 <tr><td class="center" valign="baseline"><code><var>x</var> % <var>y</var></code></td>
135 <td class="left" >remainder of <code><var>x</var> / <var>y</var></code></td>
136 <td class="center">(4)</td></tr>
137
138 <tr><td class="center" valign="baseline"><code>-<var>x</var></code></td>
139 <td class="left" ><var>x</var> negated</td>
140 <td class="center"></td></tr>
141 <tr><td class="center" valign="baseline"><code>+<var>x</var></code></td>
142 <td class="left" ><var>x</var> unchanged</td>
143 <td class="center"></td></tr>
144
145 <tr><td class="center" valign="baseline"><code>abs(<var>x</var>)</code></td>
146 <td class="left" >absolute value or magnitude of <var>x</var></td>
147 <td class="center"></td></tr>
148 <tr><td class="center" valign="baseline"><code>int(<var>x</var>)</code></td>
149 <td class="left" ><var>x</var> converted to integer</td>
150 <td class="center">(2)</td></tr>
151 <tr><td class="center" valign="baseline"><code>long(<var>x</var>)</code></td>
152 <td class="left" ><var>x</var> converted to long integer</td>
153 <td class="center">(2)</td></tr>
154 <tr><td class="center" valign="baseline"><code>float(<var>x</var>)</code></td>
155 <td class="left" ><var>x</var> converted to floating point</td>
156 <td class="center"></td></tr>
157 <tr><td class="center" valign="baseline"><code>complex(<var>re</var>,<var>im</var>)</code></td>
158 <td class="left" >a complex number with real part <var>re</var>, imaginary part <var>im</var>. <var>im</var> defaults to zero.</td>
159 <td class="center"></td></tr>
160 <tr><td class="center" valign="baseline"><code><var>c</var>.conjugate()</code></td>
161 <td class="left" >conjugate of the complex number <var>c</var></td>
162 <td class="center"></td></tr>
163 <tr><td class="center" valign="baseline"><code>divmod(<var>x</var>, <var>y</var>)</code></td>
164 <td class="left" >the pair <code>(<var>x</var> // <var>y</var>, <var>x</var> % <var>y</var>)</code></td>
165 <td class="center">(3)(4)</td></tr>
166 <tr><td class="center" valign="baseline"><code>pow(<var>x</var>, <var>y</var>)</code></td>
167 <td class="left" ><var>x</var> to the power <var>y</var></td>
168 <td class="center"></td></tr>
169 <tr><td class="center" valign="baseline"><code><var>x</var> ** <var>y</var></code></td>
170 <td class="left" ><var>x</var> to the power <var>y</var></td>
171 <td class="center"></td></tr></tbody>
172</table></div>
173<a id='l2h-137' xml:id='l2h-137'></a><a id='l2h-139' xml:id='l2h-139'></a>
174<P>
175Notes:
176<DL>
177<DT><STRONG>(1)</STRONG></DT>
178<DD>For (plain or long) integer division, the result is an integer.
179The result is always rounded towards minus infinity: 1/2 is 0,
180(-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0. Note that the result
181is a long integer if either operand is a long integer, regardless of
182the numeric value.
183<a id='l2h-140' xml:id='l2h-140'></a><a id='l2h-141' xml:id='l2h-141'></a>
184<P>
185</DD>
186<DT><STRONG>(2)</STRONG></DT>
187<DD>Conversion from floating point to (long or plain) integer may round or
188truncate as in C; see functions <tt class="function">floor()</tt> and
189<tt class="function">ceil()</tt> in the <tt class="module"><a href="module-math.html">math</a></tt><a id='l2h-147' xml:id='l2h-147'></a> module
190for well-defined conversions.
191<a id='l2h-143' xml:id='l2h-143'></a><a id='l2h-144' xml:id='l2h-144'></a><a id='l2h-145' xml:id='l2h-145'></a>
192<P>
193</DD>
194<DT><STRONG>(3)</STRONG></DT>
195<DD>See section <A href="built-in-funcs.html#built-in-funcs">2.1</A>, ``Built-in Functions,'' for a full
196description.
197
198<P>
199</DD>
200<DT><STRONG>(4)</STRONG></DT>
201<DD>Complex floor division operator, modulo operator, and <tt class="function">divmod()</tt>.
202
203<P>
204<div class="versionnote"><b>Deprecated since release 2.3.</b>
205Instead convert to float using <tt class="function">abs()</tt>
206if appropriate.</div><p></p>
207
208<P>
209</DD>
210</DL>
211
212<P>
213<BR><HR><H4>Footnotes</H4>
214<DL>
215<DT><A NAME="foot3444">... rule.</A><A
216 href="typesnumeric.html#tex2html7"><SUP>2.6</SUP></A></DT>
217<DD>
218 As a consequence, the list <code>[1, 2]</code> is considered equal
219 to <code>[1.0, 2.0]</code>, and similarly for tuples.
220
221
222</DD>
223</DL>
224<p><br /></p><hr class='online-navigation' />
225<div class='online-navigation'>
226<!--Table of Child-Links-->
227<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
228
229<UL CLASS="ChildLinks">
230<LI><A href="bitstring-ops.html">2.3.4.1 Bit-string Operations on Integer Types</a>
231</ul>
232<!--End of Table of Child-Links-->
233</div>
234
235<DIV CLASS="navigation">
236<div class='online-navigation'>
237<p></p><hr />
238<table align="center" width="100%" cellpadding="0" cellspacing="2">
239<tr>
240<td class='online-navigation'><a rel="prev" title="2.3.3 Comparisons"
241 href="comparisons.html"><img src='../icons/previous.png'
242 border='0' height='32' alt='Previous Page' width='32' /></A></td>
243<td class='online-navigation'><a rel="parent" title="2.3 Built-in Types"
244 href="types.html"><img src='../icons/up.png'
245 border='0' height='32' alt='Up One Level' width='32' /></A></td>
246<td class='online-navigation'><a rel="next" title="2.3.4.1 Bit-string Operations on"
247 href="bitstring-ops.html"><img src='../icons/next.png'
248 border='0' height='32' alt='Next Page' width='32' /></A></td>
249<td align="center" width="100%">Python Library Reference</td>
250<td class='online-navigation'><a rel="contents" title="Table of Contents"
251 href="contents.html"><img src='../icons/contents.png'
252 border='0' height='32' alt='Contents' width='32' /></A></td>
253<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
254 border='0' height='32' alt='Module Index' width='32' /></a></td>
255<td class='online-navigation'><a rel="index" title="Index"
256 href="genindex.html"><img src='../icons/index.png'
257 border='0' height='32' alt='Index' width='32' /></A></td>
258</tr></table>
259<div class='online-navigation'>
260<b class="navlabel">Previous:</b>
261<a class="sectref" rel="prev" href="comparisons.html">2.3.3 Comparisons</A>
262<b class="navlabel">Up:</b>
263<a class="sectref" rel="parent" href="types.html">2.3 Built-in Types</A>
264<b class="navlabel">Next:</b>
265<a class="sectref" rel="next" href="bitstring-ops.html">2.3.4.1 Bit-string Operations on</A>
266</div>
267</div>
268<hr />
269<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
270</DIV>
271<!--End of Navigation Panel-->
272<ADDRESS>
273See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
274</ADDRESS>
275</BODY>
276</HTML>