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