Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / lib / typesnumeric.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="typeiter.html" />
<link rel="prev" href="comparisons.html" />
<link rel="parent" href="types.html" />
<link rel="next" href="bitstring-ops.html" />
<meta name='aesop' content='information' />
<title>2.3.4 Numeric Types -- int, float, long, complex </title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="2.3.3 Comparisons"
href="comparisons.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="2.3 Built-in Types"
href="types.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="2.3.4.1 Bit-string Operations on"
href="bitstring-ops.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="comparisons.html">2.3.3 Comparisons</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="types.html">2.3 Built-in Types</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="bitstring-ops.html">2.3.4.1 Bit-string Operations on</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H2><A NAME="SECTION004340000000000000000"></A><A NAME="typesnumeric"></A>
<BR>
2.3.4 Numeric Types --
<tt class="class">int</tt>, <tt class="class">float</tt>, <tt class="class">long</tt>, <tt class="class">complex</tt>
</H2>
<P>
There are four distinct numeric types: <i class="dfn">plain integers</i>,
<i class="dfn">long integers</i>,
<i class="dfn">floating point numbers</i>, and <i class="dfn">complex numbers</i>.
In addition, Booleans are a subtype of plain integers.
Plain integers (also just called <i class="dfn">integers</i>)
are implemented using <tt class="ctype">long</tt> in C, which gives them at least 32
bits of precision. Long integers have unlimited precision. Floating
point numbers are implemented using <tt class="ctype">double</tt> in C. All bets on
their precision are off unless you happen to know the machine you are
working with.
<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>
<P>
Complex numbers have a real and imaginary part, which are each
implemented using <tt class="ctype">double</tt> in C. To extract these parts from
a complex number <var>z</var>, use <code><var>z</var>.real</code> and <code><var>z</var>.imag</code>.
<P>
Numbers are created by numeric literals or as the result of built-in
functions and operators. Unadorned integer literals (including hex
and octal numbers) yield plain integers unless the value they denote
is too large to be represented as a plain integer, in which case
they yield a long integer. Integer literals with an
"<tt class="character">L</tt>" or "<tt class="character">l</tt>" suffix yield long integers
("<tt class="character">L</tt>" is preferred because "<tt class="samp">1l</tt>" looks too much like
eleven!). Numeric literals containing a decimal point or an exponent
sign yield floating point numbers. Appending "<tt class="character">j</tt>" or
"<tt class="character">J</tt>" to a numeric literal yields a complex number with a
zero real part. A complex numeric literal is the sum of a real and
an imaginary part.
<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>
<P>
Python fully supports mixed arithmetic: when a binary arithmetic
operator has operands of different numeric types, the operand with the
``narrower'' type is widened to that of the other, where plain
integer is narrower than long integer is narrower than floating point is
narrower than complex.
Comparisons between numbers of mixed type use the same rule.<A NAME="tex2html7"
HREF="#foot3444"><SUP>2.6</SUP></A> The constructors <tt class="function">int()</tt>, <tt class="function">long()</tt>, <tt class="function">float()</tt>,
and <tt class="function">complex()</tt> can be used
to produce numbers of a specific type.
<a id='l2h-146' xml:id='l2h-146'></a>
<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>
<P>
All numeric types (except complex) support the following operations,
sorted by ascending priority (operations in the same box have the same
priority; all numeric operations have a higher priority than
comparison operations):
<P>
<div class="center"><table class="realtable">
<thead>
<tr>
<th class="center">Operation</th>
<th class="left" >Result</th>
<th class="center">Notes</th>
</tr>
</thead>
<tbody>
<tr><td class="center" valign="baseline"><code><var>x</var> + <var>y</var></code></td>
<td class="left" >sum of <var>x</var> and <var>y</var></td>
<td class="center"></td></tr>
<tr><td class="center" valign="baseline"><code><var>x</var> - <var>y</var></code></td>
<td class="left" >difference of <var>x</var> and <var>y</var></td>
<td class="center"></td></tr>
<tr><td class="center" valign="baseline"><code><var>x</var> * <var>y</var></code></td>
<td class="left" >product of <var>x</var> and <var>y</var></td>
<td class="center"></td></tr>
<tr><td class="center" valign="baseline"><code><var>x</var> / <var>y</var></code></td>
<td class="left" >quotient of <var>x</var> and <var>y</var></td>
<td class="center">(1)</td></tr>
<tr><td class="center" valign="baseline"><code><var>x</var> % <var>y</var></code></td>
<td class="left" >remainder of <code><var>x</var> / <var>y</var></code></td>
<td class="center">(4)</td></tr>
<tr><td class="center" valign="baseline"><code>-<var>x</var></code></td>
<td class="left" ><var>x</var> negated</td>
<td class="center"></td></tr>
<tr><td class="center" valign="baseline"><code>+<var>x</var></code></td>
<td class="left" ><var>x</var> unchanged</td>
<td class="center"></td></tr>
<tr><td class="center" valign="baseline"><code>abs(<var>x</var>)</code></td>
<td class="left" >absolute value or magnitude of <var>x</var></td>
<td class="center"></td></tr>
<tr><td class="center" valign="baseline"><code>int(<var>x</var>)</code></td>
<td class="left" ><var>x</var> converted to integer</td>
<td class="center">(2)</td></tr>
<tr><td class="center" valign="baseline"><code>long(<var>x</var>)</code></td>
<td class="left" ><var>x</var> converted to long integer</td>
<td class="center">(2)</td></tr>
<tr><td class="center" valign="baseline"><code>float(<var>x</var>)</code></td>
<td class="left" ><var>x</var> converted to floating point</td>
<td class="center"></td></tr>
<tr><td class="center" valign="baseline"><code>complex(<var>re</var>,<var>im</var>)</code></td>
<td class="left" >a complex number with real part <var>re</var>, imaginary part <var>im</var>. <var>im</var> defaults to zero.</td>
<td class="center"></td></tr>
<tr><td class="center" valign="baseline"><code><var>c</var>.conjugate()</code></td>
<td class="left" >conjugate of the complex number <var>c</var></td>
<td class="center"></td></tr>
<tr><td class="center" valign="baseline"><code>divmod(<var>x</var>, <var>y</var>)</code></td>
<td class="left" >the pair <code>(<var>x</var> // <var>y</var>, <var>x</var> % <var>y</var>)</code></td>
<td class="center">(3)(4)</td></tr>
<tr><td class="center" valign="baseline"><code>pow(<var>x</var>, <var>y</var>)</code></td>
<td class="left" ><var>x</var> to the power <var>y</var></td>
<td class="center"></td></tr>
<tr><td class="center" valign="baseline"><code><var>x</var> ** <var>y</var></code></td>
<td class="left" ><var>x</var> to the power <var>y</var></td>
<td class="center"></td></tr></tbody>
</table></div>
<a id='l2h-137' xml:id='l2h-137'></a><a id='l2h-139' xml:id='l2h-139'></a>
<P>
Notes:
<DL>
<DT><STRONG>(1)</STRONG></DT>
<DD>For (plain or long) integer division, the result is an integer.
The result is always rounded towards minus infinity: 1/2 is 0,
(-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0. Note that the result
is a long integer if either operand is a long integer, regardless of
the numeric value.
<a id='l2h-140' xml:id='l2h-140'></a><a id='l2h-141' xml:id='l2h-141'></a>
<P>
</DD>
<DT><STRONG>(2)</STRONG></DT>
<DD>Conversion from floating point to (long or plain) integer may round or
truncate as in C; see functions <tt class="function">floor()</tt> and
<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
for well-defined conversions.
<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>
<P>
</DD>
<DT><STRONG>(3)</STRONG></DT>
<DD>See section <A href="built-in-funcs.html#built-in-funcs">2.1</A>, ``Built-in Functions,'' for a full
description.
<P>
</DD>
<DT><STRONG>(4)</STRONG></DT>
<DD>Complex floor division operator, modulo operator, and <tt class="function">divmod()</tt>.
<P>
<div class="versionnote"><b>Deprecated since release 2.3.</b>
Instead convert to float using <tt class="function">abs()</tt>
if appropriate.</div><p></p>
<P>
</DD>
</DL>
<P>
<BR><HR><H4>Footnotes</H4>
<DL>
<DT><A NAME="foot3444">... rule.</A><A
href="typesnumeric.html#tex2html7"><SUP>2.6</SUP></A></DT>
<DD>
As a consequence, the list <code>[1, 2]</code> is considered equal
to <code>[1.0, 2.0]</code>, and similarly for tuples.
</DD>
</DL>
<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
<UL CLASS="ChildLinks">
<LI><A href="bitstring-ops.html">2.3.4.1 Bit-string Operations on Integer Types</a>
</ul>
<!--End of Table of Child-Links-->
</div>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="2.3.3 Comparisons"
href="comparisons.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="2.3 Built-in Types"
href="types.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="2.3.4.1 Bit-string Operations on"
href="bitstring-ops.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="comparisons.html">2.3.3 Comparisons</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="types.html">2.3 Built-in Types</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="bitstring-ops.html">2.3.4.1 Bit-string Operations on</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>