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