Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "PERLNUMBER 1" | |
132 | .TH PERLNUMBER 1 "2006-01-07" "perl v5.8.8" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | perlnumber \- semantics of numbers and numeric operations in Perl | |
135 | .SH "SYNOPSIS" | |
136 | .IX Header "SYNOPSIS" | |
137 | .Vb 7 | |
138 | \& $n = 1234; # decimal integer | |
139 | \& $n = 0b1110011; # binary integer | |
140 | \& $n = 01234; # octal integer | |
141 | \& $n = 0x1234; # hexadecimal integer | |
142 | \& $n = 12.34e-56; # exponential notation | |
143 | \& $n = "-12.34e56"; # number specified as a string | |
144 | \& $n = "1234"; # number specified as a string | |
145 | .Ve | |
146 | .SH "DESCRIPTION" | |
147 | .IX Header "DESCRIPTION" | |
148 | This document describes how Perl internally handles numeric values. | |
149 | .PP | |
150 | Perl's operator overloading facility is completely ignored here. Operator | |
151 | overloading allows user-defined behaviors for numbers, such as operations | |
152 | over arbitrarily large integers, floating points numbers with arbitrary | |
153 | precision, operations over \*(L"exotic\*(R" numbers such as modular arithmetic or | |
154 | p\-adic arithmetic, and so on. See overload for details. | |
155 | .SH "Storing numbers" | |
156 | .IX Header "Storing numbers" | |
157 | Perl can internally represent numbers in 3 different ways: as native | |
158 | integers, as native floating point numbers, and as decimal strings. | |
159 | Decimal strings may have an exponential notation part, as in \f(CW"12.34e\-56"\fR. | |
160 | \&\fINative\fR here means \*(L"a format supported by the C compiler which was used | |
161 | to build perl\*(R". | |
162 | .PP | |
163 | The term \*(L"native\*(R" does not mean quite as much when we talk about native | |
164 | integers, as it does when native floating point numbers are involved. | |
165 | The only implication of the term \*(L"native\*(R" on integers is that the limits for | |
166 | the maximal and the minimal supported true integral quantities are close to | |
167 | powers of 2. However, \*(L"native\*(R" floats have a most fundamental | |
168 | restriction: they may represent only those numbers which have a relatively | |
169 | \&\*(L"short\*(R" representation when converted to a binary fraction. For example, | |
170 | 0.9 cannot be represented by a native float, since the binary fraction | |
171 | for 0.9 is infinite: | |
172 | .PP | |
173 | .Vb 1 | |
174 | \& binary0.1110011001100... | |
175 | .Ve | |
176 | .PP | |
177 | with the sequence \f(CW1100\fR repeating again and again. In addition to this | |
178 | limitation, the exponent of the binary number is also restricted when it | |
179 | is represented as a floating point number. On typical hardware, floating | |
180 | point values can store numbers with up to 53 binary digits, and with binary | |
181 | exponents between \-1024 and 1024. In decimal representation this is close | |
182 | to 16 decimal digits and decimal exponents in the range of \-304..304. | |
183 | The upshot of all this is that Perl cannot store a number like | |
184 | 12345678901234567 as a floating point number on such architectures without | |
185 | loss of information. | |
186 | .PP | |
187 | Similarly, decimal strings can represent only those numbers which have a | |
188 | finite decimal expansion. Being strings, and thus of arbitrary length, there | |
189 | is no practical limit for the exponent or number of decimal digits for these | |
190 | numbers. (But realize that what we are discussing the rules for just the | |
191 | \&\fIstorage\fR of these numbers. The fact that you can store such \*(L"large\*(R" numbers | |
192 | does not mean that the \fIoperations\fR over these numbers will use all | |
193 | of the significant digits. | |
194 | See \*(L"Numeric operators and numeric conversions\*(R" for details.) | |
195 | .PP | |
196 | In fact numbers stored in the native integer format may be stored either | |
197 | in the signed native form, or in the unsigned native form. Thus the limits | |
198 | for Perl numbers stored as native integers would typically be \-2**31..2**32\-1, | |
199 | with appropriate modifications in the case of 64\-bit integers. Again, this | |
200 | does not mean that Perl can do operations only over integers in this range: | |
201 | it is possible to store many more integers in floating point format. | |
202 | .PP | |
203 | Summing up, Perl numeric values can store only those numbers which have | |
204 | a finite decimal expansion or a \*(L"short\*(R" binary expansion. | |
205 | .SH "Numeric operators and numeric conversions" | |
206 | .IX Header "Numeric operators and numeric conversions" | |
207 | As mentioned earlier, Perl can store a number in any one of three formats, | |
208 | but most operators typically understand only one of those formats. When | |
209 | a numeric value is passed as an argument to such an operator, it will be | |
210 | converted to the format understood by the operator. | |
211 | .PP | |
212 | Six such conversions are possible: | |
213 | .PP | |
214 | .Vb 6 | |
215 | \& native integer --> native floating point (*) | |
216 | \& native integer --> decimal string | |
217 | \& native floating_point --> native integer (*) | |
218 | \& native floating_point --> decimal string (*) | |
219 | \& decimal string --> native integer | |
220 | \& decimal string --> native floating point (*) | |
221 | .Ve | |
222 | .PP | |
223 | These conversions are governed by the following general rules: | |
224 | .IP "\(bu" 4 | |
225 | If the source number can be represented in the target form, that | |
226 | representation is used. | |
227 | .IP "\(bu" 4 | |
228 | If the source number is outside of the limits representable in the target form, | |
229 | a representation of the closest limit is used. (\fILoss of information\fR) | |
230 | .IP "\(bu" 4 | |
231 | If the source number is between two numbers representable in the target form, | |
232 | a representation of one of these numbers is used. (\fILoss of information\fR) | |
233 | .IP "\(bu" 4 | |
234 | In \f(CW\*(C`native floating point \-\-> native integer\*(C'\fR conversions the magnitude | |
235 | of the result is less than or equal to the magnitude of the source. | |
236 | (\fI\*(L"Rounding to zero\*(R".\fR) | |
237 | .IP "\(bu" 4 | |
238 | If the \f(CW\*(C`decimal string \-\-> native integer\*(C'\fR conversion cannot be done | |
239 | without loss of information, the result is compatible with the conversion | |
240 | sequence \f(CW\*(C`decimal_string \-\-> native_floating_point \-\-> native_integer\*(C'\fR. | |
241 | In particular, rounding is strongly biased to 0, though a number like | |
242 | \&\f(CW"0.99999999999999999999"\fR has a chance of being rounded to 1. | |
243 | .PP | |
244 | \&\fB\s-1RESTRICTION\s0\fR: The conversions marked with \f(CW\*(C`(*)\*(C'\fR above involve steps | |
245 | performed by the C compiler. In particular, bugs/features of the compiler | |
246 | used may lead to breakage of some of the above rules. | |
247 | .SH "Flavors of Perl numeric operations" | |
248 | .IX Header "Flavors of Perl numeric operations" | |
249 | Perl operations which take a numeric argument treat that argument in one | |
250 | of four different ways: they may force it to one of the integer/floating/ | |
251 | string formats, or they may behave differently depending on the format of | |
252 | the operand. Forcing a numeric value to a particular format does not | |
253 | change the number stored in the value. | |
254 | .PP | |
255 | All the operators which need an argument in the integer format treat the | |
256 | argument as in modular arithmetic, e.g., \f(CW\*(C`mod 2**32\*(C'\fR on a 32\-bit | |
257 | architecture. \f(CW\*(C`sprintf "%u", \-1\*(C'\fR therefore provides the same result as | |
258 | \&\f(CW\*(C`sprintf "%u", ~0\*(C'\fR. | |
259 | .IP "Arithmetic operators" 4 | |
260 | .IX Item "Arithmetic operators" | |
261 | The binary operators \f(CW\*(C`+\*(C'\fR \f(CW\*(C`\-\*(C'\fR \f(CW\*(C`*\*(C'\fR \f(CW\*(C`/\*(C'\fR \f(CW\*(C`%\*(C'\fR \f(CW\*(C`==\*(C'\fR \f(CW\*(C`!=\*(C'\fR \f(CW\*(C`>\*(C'\fR \f(CW\*(C`<\*(C'\fR | |
262 | \&\f(CW\*(C`>=\*(C'\fR \f(CW\*(C`<=\*(C'\fR and the unary operators \f(CW\*(C`\-\*(C'\fR \f(CW\*(C`abs\*(C'\fR and \f(CW\*(C`\-\-\*(C'\fR will | |
263 | attempt to convert arguments to integers. If both conversions are possible | |
264 | without loss of precision, and the operation can be performed without | |
265 | loss of precision then the integer result is used. Otherwise arguments are | |
266 | converted to floating point format and the floating point result is used. | |
267 | The caching of conversions (as described above) means that the integer | |
268 | conversion does not throw away fractional parts on floating point numbers. | |
269 | .IP "++" 4 | |
270 | \&\f(CW\*(C`++\*(C'\fR behaves as the other operators above, except that if it is a string | |
271 | matching the format \f(CW\*(C`/^[a\-zA\-Z]*[0\-9]*\ez/\*(C'\fR the string increment described | |
272 | in perlop is used. | |
273 | .ie n .IP "Arithmetic operators during ""use integer""" 4 | |
274 | .el .IP "Arithmetic operators during \f(CWuse integer\fR" 4 | |
275 | .IX Item "Arithmetic operators during use integer" | |
276 | In scopes where \f(CW\*(C`use integer;\*(C'\fR is in force, nearly all the operators listed | |
277 | above will force their argument(s) into integer format, and return an integer | |
278 | result. The exceptions, \f(CW\*(C`abs\*(C'\fR, \f(CW\*(C`++\*(C'\fR and \f(CW\*(C`\-\-\*(C'\fR, do not change their | |
279 | behavior with \f(CW\*(C`use integer;\*(C'\fR | |
280 | .IP "Other mathematical operators" 4 | |
281 | .IX Item "Other mathematical operators" | |
282 | Operators such as \f(CW\*(C`**\*(C'\fR, \f(CW\*(C`sin\*(C'\fR and \f(CW\*(C`exp\*(C'\fR force arguments to floating point | |
283 | format. | |
284 | .IP "Bitwise operators" 4 | |
285 | .IX Item "Bitwise operators" | |
286 | Arguments are forced into the integer format if not strings. | |
287 | .ie n .IP "Bitwise operators during ""use integer""" 4 | |
288 | .el .IP "Bitwise operators during \f(CWuse integer\fR" 4 | |
289 | .IX Item "Bitwise operators during use integer" | |
290 | forces arguments to integer format. Also shift operations internally use | |
291 | signed integers rather than the default unsigned. | |
292 | .IP "Operators which expect an integer" 4 | |
293 | .IX Item "Operators which expect an integer" | |
294 | force the argument into the integer format. This is applicable | |
295 | to the third and fourth arguments of \f(CW\*(C`sysread\*(C'\fR, for example. | |
296 | .IP "Operators which expect a string" 4 | |
297 | .IX Item "Operators which expect a string" | |
298 | force the argument into the string format. For example, this is | |
299 | applicable to \f(CW\*(C`printf "%s", $value\*(C'\fR. | |
300 | .PP | |
301 | Though forcing an argument into a particular form does not change the | |
302 | stored number, Perl remembers the result of such conversions. In | |
303 | particular, though the first such conversion may be time\-consuming, | |
304 | repeated operations will not need to redo the conversion. | |
305 | .SH "AUTHOR" | |
306 | .IX Header "AUTHOR" | |
307 | Ilya Zakharevich \f(CW\*(C`ilya@math.ohio\-state.edu\*(C'\fR | |
308 | .PP | |
309 | Editorial adjustments by Gurusamy Sarathy <gsar@ActiveState.com> | |
310 | .PP | |
311 | Updates for 5.8.0 by Nicholas Clark <nick@ccl4.org> | |
312 | .SH "SEE ALSO" | |
313 | .IX Header "SEE ALSO" | |
314 | overload, perlop |