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.
21 Operator 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
24 modular 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
37 limits for the maximal and the minimal supported true integral
38 quantities are close to powers of 2. However, "native" floats have a
39 most fundamental restriction: they may represent only those numbers
40 which have a relatively "short" representation when converted to a
41 binary fraction. For example, 0.9 cannot be represented by a native
42 float, since the 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
51 representation this is close to 16 decimal digits and decimal exponents
52 in the range of -304..304. The upshot of all this is that Perl cannot
53 store a number like 12345678901234567 as a floating point number on
54 such architectures 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
59 decimal digits for these numbers. (But realize that what we are
60 discussing the rules for just the storage of these numbers. The fact
61 that you can store such "large" numbers does not mean that the
62 operations over these numbers will use all of the significant digits.
63 See "Numeric operators 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
78 formats, but most operators typically understand only one of those
79 formats. When a numeric value is passed as an argument to such an
80 operator, it will be converted to the format understood by the
81 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 · If the source number can be represented in the target form, that
95 representation is used.
96
97 · If the source number is outside of the limits representable in the
98 target form, a representation of the closest limit is used. (Loss
99 of information)
100
101 · If the source number is between two numbers representable in the
102 target form, a representation of one of these numbers is used.
103 (Loss of information)
104
105 · In "native floating point --> native integer" conversions the
106 magnitude of the result is less than or equal to the magnitude of
107 the source. ("Rounding to zero".)
108
109 · If the "decimal string --> native integer" conversion cannot be
110 done without loss of information, the result is compatible with the
111 conversion sequence "decimal_string --> native_floating_point -->
112 native_integer". In particular, rounding is strongly biased to 0,
113 though a number like "0.99999999999999999999" has a chance of being
114 rounded to 1.
115
116 RESTRICTION: The conversions marked with "(*)" above involve steps
117 performed by the C compiler. In particular, bugs/features of the
118 compiler used may lead to breakage of some of the above rules.
119
121 Perl operations which take a numeric argument treat that argument in
122 one of four different ways: they may force it to one of the
123 integer/floating/ string formats, or they may behave differently
124 depending on the format of the operand. Forcing a numeric value to a
125 particular format does not change the number stored in the value.
126
127 All the operators which need an argument in the integer format treat
128 the argument as in modular arithmetic, e.g., "mod 2**32" on a 32-bit
129 architecture. "sprintf "%u", -1" therefore provides the same result as
130 "sprintf "%u", ~0".
131
132 Arithmetic operators
133 The binary operators "+" "-" "*" "/" "%" "==" "!=" ">" "<" ">="
134 "<=" and the unary operators "-" "abs" and "--" will attempt to
135 convert arguments to integers. If both conversions are possible
136 without loss of precision, and the operation can be performed
137 without loss of precision then the integer result is used.
138 Otherwise arguments are converted to floating point format and the
139 floating point result is used. The caching of conversions (as
140 described above) means that the integer conversion does not throw
141 away fractional parts on floating point numbers.
142
143 ++ "++" behaves as the other operators above, except that if it is a
144 string matching the format "/^[a-zA-Z]*[0-9]*\z/" the string
145 increment described in perlop is used.
146
147 Arithmetic operators during "use integer"
148 In scopes where "use integer;" is in force, nearly all the
149 operators listed above will force their argument(s) into integer
150 format, and return an integer result. The exceptions, "abs", "++"
151 and "--", do not change their behavior with "use integer;"
152
153 Other mathematical operators
154 Operators such as "**", "sin" and "exp" force arguments to floating
155 point format.
156
157 Bitwise operators
158 Arguments are forced into the integer format if not strings.
159
160 Bitwise operators during "use integer"
161 forces arguments to integer format. Also shift operations
162 internally use signed integers rather than the default unsigned.
163
164 Operators which expect an integer
165 force the argument into the integer format. This is applicable to
166 the third and fourth arguments of "sysread", for example.
167
168 Operators which expect a string
169 force the argument into the string format. For example, this is
170 applicable to "printf "%s", $value".
171
172 Though forcing an argument into a particular form does not change the
173 stored number, Perl remembers the result of such conversions. In
174 particular, though the first such conversion may be time-consuming,
175 repeated operations will not need to redo the conversion.
176
178 Ilya Zakharevich "ilya@math.ohio-state.edu"
179
180 Editorial adjustments by Gurusamy Sarathy <gsar@ActiveState.com>
181
182 Updates for 5.8.0 by Nicholas Clark <nick@ccl4.org>
183
185 overload, perlop
186
187
188
189perl v5.26.3 2018-03-23 PERLNUMBER(1)