Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
7.3 KiB

  1. =head1 NAME
  2. perlnumber - semantics of numbers and numeric operations in Perl
  3. =head1 SYNOPSIS
  4. $n = 1234; # decimal integer
  5. $n = 0b1110011; # binary integer
  6. $n = 01234; # octal integer
  7. $n = 0x1234; # hexadecimal integer
  8. $n = 12.34e-56; # exponential notation
  9. $n = "-12.34e56"; # number specified as a string
  10. $n = "1234"; # number specified as a string
  11. $n = v49.50.51.52; # number specified as a string, which in
  12. # turn is specified in terms of numbers :-)
  13. =head1 DESCRIPTION
  14. This document describes how Perl internally handles numeric values.
  15. Perl's operator overloading facility is completely ignored here. Operator
  16. overloading allows user-defined behaviors for numbers, such as operations
  17. over arbitrarily large integers, floating points numbers with arbitrary
  18. precision, operations over "exotic" numbers such as modular arithmetic or
  19. p-adic arithmetic, and so on. See L<overload> for details.
  20. =head1 Storing numbers
  21. Perl can internally represent numbers in 3 different ways: as native
  22. integers, as native floating point numbers, and as decimal strings.
  23. Decimal strings may have an exponential notation part, as in C<"12.34e-56">.
  24. I<Native> here means "a format supported by the C compiler which was used
  25. to build perl".
  26. The term "native" does not mean quite as much when we talk about native
  27. integers, as it does when native floating point numbers are involved.
  28. The only implication of the term "native" on integers is that the limits for
  29. the maximal and the minimal supported true integral quantities are close to
  30. powers of 2. However, "native" floats have a most fundamental
  31. restriction: they may represent only those numbers which have a relatively
  32. "short" representation when converted to a binary fraction. For example,
  33. 0.9 cannot be represented by a native float, since the binary fraction
  34. for 0.9 is infinite:
  35. binary0.1110011001100...
  36. with the sequence C<1100> repeating again and again. In addition to this
  37. limitation, the exponent of the binary number is also restricted when it
  38. is represented as a floating point number. On typical hardware, floating
  39. point values can store numbers with up to 53 binary digits, and with binary
  40. exponents between -1024 and 1024. In decimal representation this is close
  41. to 16 decimal digits and decimal exponents in the range of -304..304.
  42. The upshot of all this is that Perl cannot store a number like
  43. 12345678901234567 as a floating point number on such architectures without
  44. loss of information.
  45. Similarly, decimal strings can represent only those numbers which have a
  46. finite decimal expansion. Being strings, and thus of arbitrary length, there
  47. is no practical limit for the exponent or number of decimal digits for these
  48. numbers. (But realize that what we are discussing the rules for just the
  49. I<storage> of these numbers. The fact that you can store such "large" numbers
  50. does not mean that the I<operations> over these numbers will use all
  51. of the significant digits.
  52. See L<"Numeric operators and numeric conversions"> for details.)
  53. In fact numbers stored in the native integer format may be stored either
  54. in the signed native form, or in the unsigned native form. Thus the limits
  55. for Perl numbers stored as native integers would typically be -2**31..2**32-1,
  56. with appropriate modifications in the case of 64-bit integers. Again, this
  57. does not mean that Perl can do operations only over integers in this range:
  58. it is possible to store many more integers in floating point format.
  59. Summing up, Perl numeric values can store only those numbers which have
  60. a finite decimal expansion or a "short" binary expansion.
  61. =head1 Numeric operators and numeric conversions
  62. As mentioned earlier, Perl can store a number in any one of three formats,
  63. but most operators typically understand only one of those formats. When
  64. a numeric value is passed as an argument to such an operator, it will be
  65. converted to the format understood by the operator.
  66. Six such conversions are possible:
  67. native integer --> native floating point (*)
  68. native integer --> decimal string
  69. native floating_point --> native integer (*)
  70. native floating_point --> decimal string (*)
  71. decimal string --> native integer
  72. decimal string --> native floating point (*)
  73. These conversions are governed by the following general rules:
  74. =over 4
  75. =item *
  76. If the source number can be represented in the target form, that
  77. representation is used.
  78. =item *
  79. If the source number is outside of the limits representable in the target form,
  80. a representation of the closest limit is used. (I<Loss of information>)
  81. =item *
  82. If the source number is between two numbers representable in the target form,
  83. a representation of one of these numbers is used. (I<Loss of information>)
  84. =item *
  85. In C<< native floating point --> native integer >> conversions the magnitude
  86. of the result is less than or equal to the magnitude of the source.
  87. (I<"Rounding to zero".>)
  88. =item *
  89. If the C<< decimal string --> native integer >> conversion cannot be done
  90. without loss of information, the result is compatible with the conversion
  91. sequence C<< decimal_string --> native_floating_point --> native_integer >>.
  92. In particular, rounding is strongly biased to 0, though a number like
  93. C<"0.99999999999999999999"> has a chance of being rounded to 1.
  94. =back
  95. B<RESTRICTION>: The conversions marked with C<(*)> above involve steps
  96. performed by the C compiler. In particular, bugs/features of the compiler
  97. used may lead to breakage of some of the above rules.
  98. =head1 Flavors of Perl numeric operations
  99. Perl operations which take a numeric argument treat that argument in one
  100. of four different ways: they may force it to one of the integer/floating/
  101. string formats, or they may behave differently depending on the format of
  102. the operand. Forcing a numeric value to a particular format does not
  103. change the number stored in the value.
  104. All the operators which need an argument in the integer format treat the
  105. argument as in modular arithmetic, e.g., C<mod 2**32> on a 32-bit
  106. architecture. C<sprintf "%u", -1> therefore provides the same result as
  107. C<sprintf "%u", ~0>.
  108. =over 4
  109. =item Arithmetic operators except, C<no integer>
  110. force the argument into the floating point format.
  111. =item Arithmetic operators except, C<use integer>
  112. =item Bitwise operators, C<no integer>
  113. force the argument into the integer format if it is not a string.
  114. =item Bitwise operators, C<use integer>
  115. force the argument into the integer format
  116. =item Operators which expect an integer
  117. force the argument into the integer format. This is applicable
  118. to the third and fourth arguments of C<sysread>, for example.
  119. =item Operators which expect a string
  120. force the argument into the string format. For example, this is
  121. applicable to C<printf "%s", $value>.
  122. =back
  123. Though forcing an argument into a particular form does not change the
  124. stored number, Perl remembers the result of such conversions. In
  125. particular, though the first such conversion may be time-consuming,
  126. repeated operations will not need to redo the conversion.
  127. =head1 AUTHOR
  128. Ilya Zakharevich C<[email protected]>
  129. Editorial adjustments by Gurusamy Sarathy <[email protected]>
  130. =head1 SEE ALSO
  131. L<overload>