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