home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / n / numeric_ty < prev   
Encoding:
Text File  |  1996-11-14  |  3.9 KB  |  68 lines

  1. <TITLE>Numeric Types -- Python library reference</TITLE>
  2. Next: <A HREF="../s/sequence_types" TYPE="Next">Sequence Types</A>  
  3. Prev: <A HREF="../c/comparisons" TYPE="Prev">Comparisons</A>  
  4. Up: <A HREF="../t/types" TYPE="Up">Types</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H2>2.1.4. Numeric Types</H2>
  7. There are three numeric types: <DFN>plain integers</DFN>, <DFN>long integers</DFN>, and
  8. <DFN>floating point numbers</DFN>.  Plain integers (also just called <DFN>integers</DFN>)
  9. are implemented using <CODE>long</CODE> in C, which gives them at least 32
  10. bits of precision.  Long integers have unlimited precision.  Floating
  11. point numbers are implemented using <CODE>double</CODE> in C.  All bets on
  12. their precision are off unless you happen to know the machine you are
  13. working with.
  14. Numbers are created by numeric literals or as the result of built-in
  15. functions and operators.  Unadorned integer literals (including hex
  16. and octal numbers) yield plain integers.  Integer literals with an `<SAMP>L</SAMP>'
  17. or `<SAMP>l</SAMP>' suffix yield long integers
  18. (`<SAMP>L</SAMP>' is preferred because <CODE>1l</CODE> looks too much like eleven!).
  19. Numeric literals containing a decimal point or an exponent sign yield
  20. floating point numbers.
  21. Python fully supports mixed arithmetic: when a binary arithmetic
  22. operator has operands of different numeric types, the operand with the
  23. ``smaller'' type is converted to that of the other, where plain
  24. integer is smaller than long integer is smaller than floating point.
  25. Comparisons between numbers of mixed type use the same rule.<A NAME="footnoteref1" HREF="#footnotetext1">(1)</A>
  26. The functions <CODE>int()</CODE>, <CODE>long()</CODE> and <CODE>float()</CODE> can be used
  27. to coerce numbers to a specific type.
  28. All numeric types support the following operations, sorted by
  29. ascending priority (operations in the same box have the same
  30. priority; all numeric operations have a higher priority than
  31. comparison operations):
  32. <P>
  33. <DL>
  34. <DT><I>Operation</I><DD><I>Result</I>  ---  <I>Notes</I>
  35. <P>
  36. <DT><CODE><VAR>x</VAR> + <VAR>y</VAR></CODE><DD>sum of <VAR>x</VAR> and <VAR>y</VAR>
  37. <DT><CODE><VAR>x</VAR> - <VAR>y</VAR></CODE><DD>difference of <VAR>x</VAR> and <VAR>y</VAR>
  38. <DT><CODE><VAR>x</VAR> * <VAR>y</VAR></CODE><DD>product of <VAR>x</VAR> and <VAR>y</VAR>
  39. <DT><CODE><VAR>x</VAR> / <VAR>y</VAR></CODE><DD>quotient of <VAR>x</VAR> and <VAR>y</VAR>  ---  (1)
  40. <DT><CODE><VAR>x</VAR> % <VAR>y</VAR></CODE><DD>remainder of <CODE><VAR>x</VAR> / <VAR>y</VAR></CODE>
  41. <DT><CODE>-<VAR>x</VAR></CODE><DD><VAR>x</VAR> negated
  42. <DT><CODE>+<VAR>x</VAR></CODE><DD><VAR>x</VAR> unchanged
  43. <DT><CODE>abs(<VAR>x</VAR>)</CODE><DD>absolute value of <VAR>x</VAR>
  44. <DT><CODE>int(<VAR>x</VAR>)</CODE><DD><VAR>x</VAR> converted to integer  ---  (2)
  45. <DT><CODE>long(<VAR>x</VAR>)</CODE><DD><VAR>x</VAR> converted to long integer  ---  (2)
  46. <DT><CODE>float(<VAR>x</VAR>)</CODE><DD><VAR>x</VAR> converted to floating point
  47. <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)
  48. <DT><CODE>pow(<VAR>x</VAR>, <VAR>y</VAR>)</CODE><DD><VAR>x</VAR> to the power <VAR>y</VAR>
  49. </DL>
  50.  Notes:
  51. <DL>
  52. <DT><B>(1)</B><DD>For (plain or long) integer division, the result is an integer.
  53. The result is always rounded towards minus infinity: 1/2 is 0, 
  54. (-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0.
  55. <DT><B>(2)</B><DD>Conversion from floating point to (long or plain) integer may round or
  56. truncate as in C; see functions <CODE>floor()</CODE> and <CODE>ceil()</CODE> in
  57. module <CODE>math</CODE> for well-defined conversions.
  58. <DT><B>(3)</B><DD>See the section on built-in functions for an exact definition.
  59. <P>
  60. </DL>
  61. <H2>Menu</H2><DL COMPACT>
  62. <DT><A HREF="../b/bit-string_operations" TYPE=Menu>Bit-string Operations</A>
  63. <DD></DL>
  64. <H2>---------- Footnotes ----------</H2>
  65. <A NAME="footnotetext1" HREF="#footnoteref1">(1)</A>
  66. As a consequence, the list <CODE>[1, 2]</CODE> is considered equal
  67. to <CODE>[1.0, 2.0]</CODE>, and similar for tuples.<P>
  68.