home *** CD-ROM | disk | FTP | other *** search
- <TITLE>Numeric Types -- Python library reference</TITLE>
- Next: <A HREF="../s/sequence_types" TYPE="Next">Sequence Types</A>
- Prev: <A HREF="../c/comparisons" TYPE="Prev">Comparisons</A>
- Up: <A HREF="../t/types" TYPE="Up">Types</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H2>2.1.4. Numeric Types</H2>
- There are three numeric types: <DFN>plain integers</DFN>, <DFN>long integers</DFN>, and
- <DFN>floating point numbers</DFN>. Plain integers (also just called <DFN>integers</DFN>)
- are implemented using <CODE>long</CODE> in C, which gives them at least 32
- bits of precision. Long integers have unlimited precision. Floating
- point numbers are implemented using <CODE>double</CODE> in C. All bets on
- their precision are off unless you happen to know the machine you are
- working with.
- 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. Integer literals with an `<SAMP>L</SAMP>'
- or `<SAMP>l</SAMP>' suffix yield long integers
- (`<SAMP>L</SAMP>' is preferred because <CODE>1l</CODE> looks too much like eleven!).
- Numeric literals containing a decimal point or an exponent sign yield
- floating point numbers.
- Python fully supports mixed arithmetic: when a binary arithmetic
- operator has operands of different numeric types, the operand with the
- ``smaller'' type is converted to that of the other, where plain
- integer is smaller than long integer is smaller than floating point.
- Comparisons between numbers of mixed type use the same rule.<A NAME="footnoteref1" HREF="#footnotetext1">(1)</A>
- The functions <CODE>int()</CODE>, <CODE>long()</CODE> and <CODE>float()</CODE> can be used
- to coerce numbers to a specific type.
- All numeric types 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>
- <DL>
- <DT><I>Operation</I><DD><I>Result</I> --- <I>Notes</I>
- <P>
- <DT><CODE><VAR>x</VAR> + <VAR>y</VAR></CODE><DD>sum of <VAR>x</VAR> and <VAR>y</VAR>
- <DT><CODE><VAR>x</VAR> - <VAR>y</VAR></CODE><DD>difference of <VAR>x</VAR> and <VAR>y</VAR>
- <DT><CODE><VAR>x</VAR> * <VAR>y</VAR></CODE><DD>product of <VAR>x</VAR> and <VAR>y</VAR>
- <DT><CODE><VAR>x</VAR> / <VAR>y</VAR></CODE><DD>quotient of <VAR>x</VAR> and <VAR>y</VAR> --- (1)
- <DT><CODE><VAR>x</VAR> % <VAR>y</VAR></CODE><DD>remainder of <CODE><VAR>x</VAR> / <VAR>y</VAR></CODE>
- <DT><CODE>-<VAR>x</VAR></CODE><DD><VAR>x</VAR> negated
- <DT><CODE>+<VAR>x</VAR></CODE><DD><VAR>x</VAR> unchanged
- <DT><CODE>abs(<VAR>x</VAR>)</CODE><DD>absolute value of <VAR>x</VAR>
- <DT><CODE>int(<VAR>x</VAR>)</CODE><DD><VAR>x</VAR> converted to integer --- (2)
- <DT><CODE>long(<VAR>x</VAR>)</CODE><DD><VAR>x</VAR> converted to long integer --- (2)
- <DT><CODE>float(<VAR>x</VAR>)</CODE><DD><VAR>x</VAR> converted to floating point
- <DT><CODE>divmod(<VAR>x</VAR>, <VAR>y</VAR>)</CODE><DD>the pair <CODE>(<VAR>x</VAR> / <VAR>y</VAR>, <VAR>x</VAR> % <VAR>y</VAR>)</CODE> --- (3)
- <DT><CODE>pow(<VAR>x</VAR>, <VAR>y</VAR>)</CODE><DD><VAR>x</VAR> to the power <VAR>y</VAR>
- </DL>
- Notes:
- <DL>
- <DT><B>(1)</B><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.
- <DT><B>(2)</B><DD>Conversion from floating point to (long or plain) integer may round or
- truncate as in C; see functions <CODE>floor()</CODE> and <CODE>ceil()</CODE> in
- module <CODE>math</CODE> for well-defined conversions.
- <DT><B>(3)</B><DD>See the section on built-in functions for an exact definition.
- <P>
- </DL>
- <H2>Menu</H2><DL COMPACT>
- <DT><A HREF="../b/bit-string_operations" TYPE=Menu>Bit-string Operations</A>
- <DD></DL>
- <H2>---------- Footnotes ----------</H2>
- <A NAME="footnotetext1" HREF="#footnoteref1">(1)</A>
- As a consequence, the list <CODE>[1, 2]</CODE> is considered equal
- to <CODE>[1.0, 2.0]</CODE>, and similar for tuples.<P>
-