extra code in pp_concat, Take 2
[p5sagit/p5-mst-13.2.git] / pod / perlnumber.pod
CommitLineData
ac65edd0 1=head1 NAME
2
3perlnumber - semantics of numbers and numeric operations in Perl
4
5=head1 SYNOPSIS
6
78594626 7 $n = 1234; # decimal integer
8 $n = 0b1110011; # binary integer
9 $n = 01234; # octal integer
10 $n = 0x1234; # hexadecimal integer
11 $n = 12.34e-56; # exponential notation
12 $n = "-12.34e56"; # number specified as a string
13 $n = "1234"; # number specified as a string
ac65edd0 14
15=head1 DESCRIPTION
16
17This document describes how Perl internally handles numeric values.
18
19Perl's operator overloading facility is completely ignored here. Operator
20overloading allows user-defined behaviors for numbers, such as operations
21over arbitrarily large integers, floating points numbers with arbitrary
22precision, operations over "exotic" numbers such as modular arithmetic or
055fd3a9 23p-adic arithmetic, and so on. See L<overload> for details.
ac65edd0 24
25=head1 Storing numbers
26
b38f6a39 27Perl can internally represent numbers in 3 different ways: as native
ac65edd0 28integers, as native floating point numbers, and as decimal strings.
29Decimal strings may have an exponential notation part, as in C<"12.34e-56">.
30I<Native> here means "a format supported by the C compiler which was used
31to build perl".
32
33The term "native" does not mean quite as much when we talk about native
34integers, as it does when native floating point numbers are involved.
35The only implication of the term "native" on integers is that the limits for
36the maximal and the minimal supported true integral quantities are close to
85add8c2 37powers of 2. However, "native" floats have a most fundamental
ac65edd0 38restriction: they may represent only those numbers which have a relatively
39"short" representation when converted to a binary fraction. For example,
4375e838 400.9 cannot be represented by a native float, since the binary fraction
ac65edd0 41for 0.9 is infinite:
42
43 binary0.1110011001100...
44
45with the sequence C<1100> repeating again and again. In addition to this
46limitation, the exponent of the binary number is also restricted when it
47is represented as a floating point number. On typical hardware, floating
48point values can store numbers with up to 53 binary digits, and with binary
49exponents between -1024 and 1024. In decimal representation this is close
50to 16 decimal digits and decimal exponents in the range of -304..304.
51The upshot of all this is that Perl cannot store a number like
5212345678901234567 as a floating point number on such architectures without
53loss of information.
54
b38f6a39 55Similarly, decimal strings can represent only those numbers which have a
ac65edd0 56finite decimal expansion. Being strings, and thus of arbitrary length, there
57is no practical limit for the exponent or number of decimal digits for these
58numbers. (But realize that what we are discussing the rules for just the
59I<storage> of these numbers. The fact that you can store such "large" numbers
106325ad 60does not mean that the I<operations> over these numbers will use all
ac65edd0 61of the significant digits.
4a4eefd0 62See L<"Numeric operators and numeric conversions"> for details.)
ac65edd0 63
64In fact numbers stored in the native integer format may be stored either
65in the signed native form, or in the unsigned native form. Thus the limits
66for Perl numbers stored as native integers would typically be -2**31..2**32-1,
67with appropriate modifications in the case of 64-bit integers. Again, this
68does not mean that Perl can do operations only over integers in this range:
69it is possible to store many more integers in floating point format.
70
71Summing up, Perl numeric values can store only those numbers which have
72a finite decimal expansion or a "short" binary expansion.
73
74=head1 Numeric operators and numeric conversions
75
76As mentioned earlier, Perl can store a number in any one of three formats,
77but most operators typically understand only one of those formats. When
78a numeric value is passed as an argument to such an operator, it will be
79converted to the format understood by the operator.
80
81Six such conversions are possible:
82
83 native integer --> native floating point (*)
84 native integer --> decimal string
85 native floating_point --> native integer (*)
86 native floating_point --> decimal string (*)
87 decimal string --> native integer
88 decimal string --> native floating point (*)
89
90These conversions are governed by the following general rules:
91
13a2d996 92=over 4
ac65edd0 93
94=item *
95
96If the source number can be represented in the target form, that
97representation is used.
98
99=item *
100
101If the source number is outside of the limits representable in the target form,
102a representation of the closest limit is used. (I<Loss of information>)
103
104=item *
105
106If the source number is between two numbers representable in the target form,
107a representation of one of these numbers is used. (I<Loss of information>)
108
109=item *
110
111In C<< native floating point --> native integer >> conversions the magnitude
112of the result is less than or equal to the magnitude of the source.
113(I<"Rounding to zero".>)
114
115=item *
116
117If the C<< decimal string --> native integer >> conversion cannot be done
118without loss of information, the result is compatible with the conversion
119sequence C<< decimal_string --> native_floating_point --> native_integer >>.
120In particular, rounding is strongly biased to 0, though a number like
121C<"0.99999999999999999999"> has a chance of being rounded to 1.
122
123=back
124
125B<RESTRICTION>: The conversions marked with C<(*)> above involve steps
126performed by the C compiler. In particular, bugs/features of the compiler
127used may lead to breakage of some of the above rules.
128
129=head1 Flavors of Perl numeric operations
130
131Perl operations which take a numeric argument treat that argument in one
132of four different ways: they may force it to one of the integer/floating/
133string formats, or they may behave differently depending on the format of
134the operand. Forcing a numeric value to a particular format does not
135change the number stored in the value.
136
137All the operators which need an argument in the integer format treat the
138argument as in modular arithmetic, e.g., C<mod 2**32> on a 32-bit
139architecture. C<sprintf "%u", -1> therefore provides the same result as
140C<sprintf "%u", ~0>.
141
13a2d996 142=over 4
ac65edd0 143
78594626 144=item Arithmetic operators
ac65edd0 145
78594626 146The binary operators C<+> C<-> C<*> C</> C<%> C<==> C<!=> C<E<gt>> C<E<lt>>
147C<E<gt>=> C<E<lt>=> and the unary operators C<-> C<abs> and C<--> will
148attempt to convert arguments to integers. If both conversions are possible
149without loss of precision, and the operation can be performed without
150loss of precision then the integer result is used. Otherwise arguments are
151converted to floating point format and the floating point result is used.
152The caching of conversions (as described above) means that the integer
153conversion does not throw away fractional parts on floating point numbers.
ac65edd0 154
78594626 155=item ++
ac65edd0 156
78594626 157C<++> behaves as the other operators above, except that if it is a string
158matching the format C</^[a-zA-Z]*[0-9]*\z/> the string increment described
159in L<perlop> is used.
ac65edd0 160
78594626 161=item Arithmetic operators during C<use integer>
ac65edd0 162
78594626 163In scopes where C<use integer;> is in force, nearly all the operators listed
164above will force their argument(s) into integer format, and return an integer
165result. The exceptions, C<abs>, C<++> and C<-->, do not change their
166behavior with C<use integer;>
ac65edd0 167
78594626 168=item Other mathematical operators
169
170Operators such as C<**>, C<sin> and C<exp> force arguments to floating point
171format.
172
173=item Bitwise operators
174
175Arguments are forced into the integer format if not strings.
176
177=item Bitwise operators during C<use integer>
178
179forces arguments to integer format. Also shift operations internally use
180signed integers rather than the default unsigned.
ac65edd0 181
182=item Operators which expect an integer
183
184force the argument into the integer format. This is applicable
185to the third and fourth arguments of C<sysread>, for example.
186
187=item Operators which expect a string
188
189force the argument into the string format. For example, this is
190applicable to C<printf "%s", $value>.
191
192=back
193
194Though forcing an argument into a particular form does not change the
195stored number, Perl remembers the result of such conversions. In
196particular, though the first such conversion may be time-consuming,
197repeated operations will not need to redo the conversion.
198
199=head1 AUTHOR
200
201Ilya Zakharevich C<ilya@math.ohio-state.edu>
202
203Editorial adjustments by Gurusamy Sarathy <gsar@ActiveState.com>
204
78594626 205Updates for 5.8.0 by Nicholas Clark <nick@ccl4.org>
206
ac65edd0 207=head1 SEE ALSO
208
78594626 209L<overload>, L<perlop>