This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
YA resync with mainstem, including VMS patches from others
[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
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
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
ee8c7f54 25p-adic arithmetic, and so on. See L<overload> for details.
ac65edd0
GS
26
27=head1 Storing numbers
28
ee8c7f54 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
ee8c7f54 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,
ee8c7f54 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
ee8c7f54 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
ee8c7f54 62does not mean that the I<operations> over these numbers will use all
ac65edd0 63of the significant digits.
ee8c7f54 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
94=over
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
144=over
145
146=item Arithmetic operators except, C<no integer>
147
148force the argument into the floating point format.
149
150=item Arithmetic operators except, C<use integer>
151
152=item Bitwise operators, C<no integer>
153
154force the argument into the integer format if it is not a string.
155
156=item Bitwise operators, C<use integer>
157
158force the argument into the integer format
159
160=item Operators which expect an integer
161
162force the argument into the integer format. This is applicable
163to the third and fourth arguments of C<sysread>, for example.
164
165=item Operators which expect a string
166
167force the argument into the string format. For example, this is
168applicable to C<printf "%s", $value>.
169
170=back
171
172Though forcing an argument into a particular form does not change the
173stored number, Perl remembers the result of such conversions. In
174particular, though the first such conversion may be time-consuming,
175repeated operations will not need to redo the conversion.
176
177=head1 AUTHOR
178
179Ilya Zakharevich C<ilya@math.ohio-state.edu>
180
181Editorial adjustments by Gurusamy Sarathy <gsar@ActiveState.com>
182
183=head1 SEE ALSO
184
ee8c7f54 185L<overload>