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