home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / zkuste / Perl / ActivePerl-5.6.0.613.msi / 䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥 / _524cf1f888bf68aa0b365d01c1b082da < prev    next >
Text File  |  2000-03-23  |  10KB  |  204 lines

  1. <HTML>
  2. <HEAD>
  3. <TITLE>perlnumber - semantics of numbers and numeric operations in Perl</TITLE>
  4. <LINK REL="stylesheet" HREF="../../Active.css" TYPE="text/css">
  5. <LINK REV="made" HREF="mailto:">
  6. </HEAD>
  7.  
  8. <BODY>
  9. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  10. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  11. <STRONG><P CLASS=block> perlnumber - semantics of numbers and numeric operations in Perl</P></STRONG>
  12. </TD></TR>
  13. </TABLE>
  14.  
  15. <A NAME="__index__"></A>
  16. <!-- INDEX BEGIN -->
  17.  
  18. <UL>
  19.  
  20.     <LI><A HREF="#name">NAME</A></LI>
  21.     <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
  22.     <LI><A HREF="#description">DESCRIPTION</A></LI>
  23.     <LI><A HREF="#storing numbers">Storing numbers</A></LI>
  24.     <LI><A HREF="#numeric operators and numeric conversions">Numeric operators and numeric conversions</A></LI>
  25.     <LI><A HREF="#flavors of perl numeric operations">Flavors of Perl numeric operations</A></LI>
  26.     <LI><A HREF="#author">AUTHOR</A></LI>
  27.     <LI><A HREF="#see also">SEE ALSO</A></LI>
  28. </UL>
  29. <!-- INDEX END -->
  30.  
  31. <HR>
  32. <P>
  33. <H1><A NAME="name">NAME</A></H1>
  34. <P>perlnumber - semantics of numbers and numeric operations in Perl</P>
  35. <P>
  36. <HR>
  37. <H1><A NAME="synopsis">SYNOPSIS</A></H1>
  38. <PRE>
  39.     $n = 1234;                  # decimal integer
  40.     $n = 0b1110011;             # binary integer
  41.     $n = 01234;                 # octal integer
  42.     $n = 0x1234;                # hexadecimal integer
  43.     $n = 12.34e-56;             # exponential notation
  44.     $n = "-12.34e56";           # number specified as a string
  45.     $n = "1234";                # number specified as a string
  46.     $n = v49.50.51.52;          # number specified as a string, which in
  47.                                 # turn is specified in terms of numbers :-)</PRE>
  48. <P>
  49. <HR>
  50. <H1><A NAME="description">DESCRIPTION</A></H1>
  51. <P>This document describes how Perl internally handles numeric values.</P>
  52. <P>Perl's operator overloading facility is completely ignored here.  Operator
  53. overloading allows user-defined behaviors for numbers, such as operations
  54. over arbitrarily large integers, floating points numbers with arbitrary
  55. precision, operations over ``exotic'' numbers such as modular arithmetic or
  56. p-adic arithmetic, and so on.  See <A HREF="../../lib/overload.html">the overload manpage</A> for details.</P>
  57. <P>
  58. <HR>
  59. <H1><A NAME="storing numbers">Storing numbers</A></H1>
  60. <P>Perl can internally represent numbers in 3 different ways: as native
  61. integers, as native floating point numbers, and as decimal strings.
  62. Decimal strings may have an exponential notation part, as in <CODE>"12.34e-56"</CODE>.
  63. <EM>Native</EM> here means ``a format supported by the C compiler which was used
  64. to build perl''.</P>
  65. <P>The term ``native'' does not mean quite as much when we talk about native
  66. integers, as it does when native floating point numbers are involved.
  67. The only implication of the term ``native'' on integers is that the limits for
  68. the maximal and the minimal supported true integral quantities are close to
  69. powers of 2.  However, ``native'' floats have a most fundamental
  70. restriction: they may represent only those numbers which have a relatively
  71. ``short'' representation when converted to a binary fraction.  For example,
  72. 0.9 cannot be respresented by a native float, since the binary fraction
  73. for 0.9 is infinite:</P>
  74. <PRE>
  75.   binary0.1110011001100...</PRE>
  76. <P>with the sequence <CODE>1100</CODE> repeating again and again.  In addition to this
  77. limitation,  the exponent of the binary number is also restricted when it
  78. is represented as a floating point number.  On typical hardware, floating
  79. point values can store numbers with up to 53 binary digits, and with binary
  80. exponents between -1024 and 1024.  In decimal representation this is close
  81. to 16 decimal digits and decimal exponents in the range of -304..304.
  82. The upshot of all this is that Perl cannot store a number like
  83. 12345678901234567 as a floating point number on such architectures without
  84. loss of information.</P>
  85. <P>Similarly, decimal strings can represent only those numbers which have a
  86. finite decimal expansion.  Being strings, and thus of arbitrary length, there
  87. is no practical limit for the exponent or number of decimal digits for these
  88. numbers.  (But realize that what we are discussing the rules for just the
  89. <EM>storage</EM> of these numbers.  The fact that you can store such ``large'' numbers
  90. does not mean that that the <EM>operations</EM> over these numbers will use all
  91. of the significant digits.
  92. See <A HREF="#numeric operators and numeric conversions">Numeric operators and numeric conversions</A> for details.)</P>
  93. <P>In fact numbers stored in the native integer format may be stored either
  94. in the signed native form, or in the unsigned native form.  Thus the limits
  95. for Perl numbers stored as native integers would typically be -2**31..2**32-1,
  96. with appropriate modifications in the case of 64-bit integers.  Again, this
  97. does not mean that Perl can do operations only over integers in this range:
  98. it is possible to store many more integers in floating point format.</P>
  99. <P>Summing up, Perl numeric values can store only those numbers which have
  100. a finite decimal expansion or a ``short'' binary expansion.</P>
  101. <P>
  102. <HR>
  103. <H1><A NAME="numeric operators and numeric conversions">Numeric operators and numeric conversions</A></H1>
  104. <P>As mentioned earlier, Perl can store a number in any one of three formats,
  105. but most operators typically understand only one of those formats.  When
  106. a numeric value is passed as an argument to such an operator, it will be
  107. converted to the format understood by the operator.</P>
  108. <P>Six such conversions are possible:</P>
  109. <PRE>
  110.   native integer        --> native floating point       (*)
  111.   native integer        --> decimal string
  112.   native floating_point --> native integer              (*)
  113.   native floating_point --> decimal string              (*)
  114.   decimal string        --> native integer
  115.   decimal string        --> native floating point       (*)</PRE>
  116. <P>These conversions are governed by the following general rules:</P>
  117. <UL>
  118. <LI>
  119. If the source number can be represented in the target form, that
  120. representation is used.
  121. <P></P>
  122. <LI>
  123. If the source number is outside of the limits representable in the target form,
  124. a representation of the closest limit is used.  (<EM>Loss of information</EM>)
  125. <P></P>
  126. <LI>
  127. If the source number is between two numbers representable in the target form,
  128. a representation of one of these numbers is used.  (<EM>Loss of information</EM>)
  129. <P></P>
  130. <LI>
  131. In <CODE>native floating point --> native integer</CODE> conversions the magnitude
  132. of the result is less than or equal to the magnitude of the source.
  133. (<EM>``Rounding to zero''.</EM>)
  134. <P></P>
  135. <LI>
  136. If the <CODE>decimal string --> native integer</CODE> conversion cannot be done
  137. without loss of information, the result is compatible with the conversion
  138. sequence <CODE>decimal_string --> native_floating_point --> native_integer</CODE>.
  139. In particular, rounding is strongly biased to 0, though a number like
  140. <CODE>"0.99999999999999999999"</CODE> has a chance of being rounded to 1.
  141. <P></P></UL>
  142. <P><STRONG>RESTRICTION</STRONG>: The conversions marked with <CODE>(*)</CODE> above involve steps
  143. performed by the C compiler.  In particular, bugs/features of the compiler
  144. used may lead to breakage of some of the above rules.</P>
  145. <P>
  146. <HR>
  147. <H1><A NAME="flavors of perl numeric operations">Flavors of Perl numeric operations</A></H1>
  148. <P>Perl operations which take a numeric argument treat that argument in one
  149. of four different ways: they may force it to one of the integer/floating/
  150. string formats, or they may behave differently depending on the format of
  151. the operand.  Forcing a numeric value to a particular format does not
  152. change the number stored in the value.</P>
  153. <P>All the operators which need an argument in the integer format treat the
  154. argument as in modular arithmetic, e.g., <CODE>mod 2**32</CODE> on a 32-bit
  155. architecture.  <CODE>sprintf "%u", -1</CODE> therefore provides the same result as
  156. <CODE>sprintf "%u", ~0</CODE>.</P>
  157. <DL>
  158. <DT><STRONG><A NAME="item_Arithmetic_operators_except%2C_no_integer">Arithmetic operators except, <CODE>no integer</CODE></A></STRONG><BR>
  159. <DD>
  160. force the argument into the floating point format.
  161. <P></P>
  162. <DT><STRONG><A NAME="item_Arithmetic_operators_except%2C_use_integer">Arithmetic operators except, <CODE>use integer</CODE></A></STRONG><BR>
  163. <DD>
  164. <DT><STRONG><A NAME="item_Bitwise_operators%2C_no_integer">Bitwise operators, <CODE>no integer</CODE></A></STRONG><BR>
  165. <DD>
  166. force the argument into the integer format if it is not a string.
  167. <P></P>
  168. <DT><STRONG><A NAME="item_Bitwise_operators%2C_use_integer">Bitwise operators, <CODE>use integer</CODE></A></STRONG><BR>
  169. <DD>
  170. force the argument into the integer format
  171. <P></P>
  172. <DT><STRONG><A NAME="item_Operators_which_expect_an_integer">Operators which expect an integer</A></STRONG><BR>
  173. <DD>
  174. force the argument into the integer format.  This is applicable
  175. to the third and fourth arguments of <A HREF="../../lib/Pod/perlfunc.html#item_sysread"><CODE>sysread</CODE></A>, for example.
  176. <P></P>
  177. <DT><STRONG><A NAME="item_Operators_which_expect_a_string">Operators which expect a string</A></STRONG><BR>
  178. <DD>
  179. force the argument into the string format.  For example, this is
  180. applicable to <CODE>printf "%s", $value</CODE>.
  181. <P></P></DL>
  182. <P>Though forcing an argument into a particular form does not change the
  183. stored number, Perl remembers the result of such conversions.  In
  184. particular, though the first such conversion may be time-consuming,
  185. repeated operations will not need to redo the conversion.</P>
  186. <P>
  187. <HR>
  188. <H1><A NAME="author">AUTHOR</A></H1>
  189. <P>Ilya Zakharevich <CODE>ilya@math.ohio-state.edu</CODE></P>
  190. <P>Editorial adjustments by Gurusamy Sarathy <<A HREF="mailto:gsar@ActiveState.com">gsar@ActiveState.com</A>></P>
  191. <P>
  192. <HR>
  193. <H1><A NAME="see also">SEE ALSO</A></H1>
  194. <P><A HREF="../../lib/overload.html">the overload manpage</A></P>
  195. <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
  196. <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
  197. <STRONG><P CLASS=block> perlnumber - semantics of numbers and numeric operations in Perl</P></STRONG>
  198. </TD></TR>
  199. </TABLE>
  200.  
  201. </BODY>
  202.  
  203. </HTML>
  204.