| 1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
| 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 "2002-06-08" "perl v5.8.0" "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 9 |
| 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 | \& $n = v49.50.51.52; # number specified as a string, which in |
| 146 | \& # turn is specified in terms of numbers :-) |
| 147 | .Ve |
| 148 | .SH "DESCRIPTION" |
| 149 | .IX Header "DESCRIPTION" |
| 150 | This document describes how Perl internally handles numeric values. |
| 151 | .PP |
| 152 | Perl's operator overloading facility is completely ignored here. Operator |
| 153 | overloading allows user-defined behaviors for numbers, such as operations |
| 154 | over arbitrarily large integers, floating points numbers with arbitrary |
| 155 | precision, operations over \*(L"exotic\*(R" numbers such as modular arithmetic or |
| 156 | p\-adic arithmetic, and so on. See overload for details. |
| 157 | .SH "Storing numbers" |
| 158 | .IX Header "Storing numbers" |
| 159 | Perl can internally represent numbers in 3 different ways: as native |
| 160 | integers, as native floating point numbers, and as decimal strings. |
| 161 | Decimal strings may have an exponential notation part, as in \f(CW"12.34e\-56"\fR. |
| 162 | \&\fINative\fR here means \*(L"a format supported by the C compiler which was used |
| 163 | to build perl\*(R". |
| 164 | .PP |
| 165 | The term \*(L"native\*(R" does not mean quite as much when we talk about native |
| 166 | integers, as it does when native floating point numbers are involved. |
| 167 | The only implication of the term \*(L"native\*(R" on integers is that the limits for |
| 168 | the maximal and the minimal supported true integral quantities are close to |
| 169 | powers of 2. However, \*(L"native\*(R" floats have a most fundamental |
| 170 | restriction: they may represent only those numbers which have a relatively |
| 171 | \&\*(L"short\*(R" representation when converted to a binary fraction. For example, |
| 172 | 0.9 cannot be represented by a native float, since the binary fraction |
| 173 | for 0.9 is infinite: |
| 174 | .PP |
| 175 | .Vb 1 |
| 176 | \& binary0.1110011001100... |
| 177 | .Ve |
| 178 | .PP |
| 179 | with the sequence \f(CW1100\fR repeating again and again. In addition to this |
| 180 | limitation, the exponent of the binary number is also restricted when it |
| 181 | is represented as a floating point number. On typical hardware, floating |
| 182 | point values can store numbers with up to 53 binary digits, and with binary |
| 183 | exponents between \-1024 and 1024. In decimal representation this is close |
| 184 | to 16 decimal digits and decimal exponents in the range of \-304..304. |
| 185 | The upshot of all this is that Perl cannot store a number like |
| 186 | 12345678901234567 as a floating point number on such architectures without |
| 187 | loss of information. |
| 188 | .PP |
| 189 | Similarly, decimal strings can represent only those numbers which have a |
| 190 | finite decimal expansion. Being strings, and thus of arbitrary length, there |
| 191 | is no practical limit for the exponent or number of decimal digits for these |
| 192 | numbers. (But realize that what we are discussing the rules for just the |
| 193 | \&\fIstorage\fR of these numbers. The fact that you can store such \*(L"large\*(R" numbers |
| 194 | does not mean that the \fIoperations\fR over these numbers will use all |
| 195 | of the significant digits. |
| 196 | See \*(L"Numeric operators and numeric conversions\*(R" for details.) |
| 197 | .PP |
| 198 | In fact numbers stored in the native integer format may be stored either |
| 199 | in the signed native form, or in the unsigned native form. Thus the limits |
| 200 | for Perl numbers stored as native integers would typically be \-2**31..2**32\-1, |
| 201 | with appropriate modifications in the case of 64\-bit integers. Again, this |
| 202 | does not mean that Perl can do operations only over integers in this range: |
| 203 | it is possible to store many more integers in floating point format. |
| 204 | .PP |
| 205 | Summing up, Perl numeric values can store only those numbers which have |
| 206 | a finite decimal expansion or a \*(L"short\*(R" binary expansion. |
| 207 | .SH "Numeric operators and numeric conversions" |
| 208 | .IX Header "Numeric operators and numeric conversions" |
| 209 | As mentioned earlier, Perl can store a number in any one of three formats, |
| 210 | but most operators typically understand only one of those formats. When |
| 211 | a numeric value is passed as an argument to such an operator, it will be |
| 212 | converted to the format understood by the operator. |
| 213 | .PP |
| 214 | Six such conversions are possible: |
| 215 | .PP |
| 216 | .Vb 6 |
| 217 | \& native integer --> native floating point (*) |
| 218 | \& native integer --> decimal string |
| 219 | \& native floating_point --> native integer (*) |
| 220 | \& native floating_point --> decimal string (*) |
| 221 | \& decimal string --> native integer |
| 222 | \& decimal string --> native floating point (*) |
| 223 | .Ve |
| 224 | .PP |
| 225 | These conversions are governed by the following general rules: |
| 226 | .IP "\(bu" 4 |
| 227 | If the source number can be represented in the target form, that |
| 228 | representation is used. |
| 229 | .IP "\(bu" 4 |
| 230 | If the source number is outside of the limits representable in the target form, |
| 231 | a representation of the closest limit is used. (\fILoss of information\fR) |
| 232 | .IP "\(bu" 4 |
| 233 | If the source number is between two numbers representable in the target form, |
| 234 | a representation of one of these numbers is used. (\fILoss of information\fR) |
| 235 | .IP "\(bu" 4 |
| 236 | In \f(CW\*(C`native floating point \-\-> native integer\*(C'\fR conversions the magnitude |
| 237 | of the result is less than or equal to the magnitude of the source. |
| 238 | (\fI\*(L"Rounding to zero\*(R".\fR) |
| 239 | .IP "\(bu" 4 |
| 240 | If the \f(CW\*(C`decimal string \-\-> native integer\*(C'\fR conversion cannot be done |
| 241 | without loss of information, the result is compatible with the conversion |
| 242 | sequence \f(CW\*(C`decimal_string \-\-> native_floating_point \-\-> native_integer\*(C'\fR. |
| 243 | In particular, rounding is strongly biased to 0, though a number like |
| 244 | \&\f(CW"0.99999999999999999999"\fR has a chance of being rounded to 1. |
| 245 | .PP |
| 246 | \&\fB\s-1RESTRICTION\s0\fR: The conversions marked with \f(CW\*(C`(*)\*(C'\fR above involve steps |
| 247 | performed by the C compiler. In particular, bugs/features of the compiler |
| 248 | used may lead to breakage of some of the above rules. |
| 249 | .SH "Flavors of Perl numeric operations" |
| 250 | .IX Header "Flavors of Perl numeric operations" |
| 251 | Perl operations which take a numeric argument treat that argument in one |
| 252 | of four different ways: they may force it to one of the integer/floating/ |
| 253 | string formats, or they may behave differently depending on the format of |
| 254 | the operand. Forcing a numeric value to a particular format does not |
| 255 | change the number stored in the value. |
| 256 | .PP |
| 257 | All the operators which need an argument in the integer format treat the |
| 258 | argument as in modular arithmetic, e.g., \f(CW\*(C`mod 2**32\*(C'\fR on a 32\-bit |
| 259 | architecture. \f(CW\*(C`sprintf "%u", \-1\*(C'\fR therefore provides the same result as |
| 260 | \&\f(CW\*(C`sprintf "%u", ~0\*(C'\fR. |
| 261 | .IP "Arithmetic operators" 4 |
| 262 | .IX Item "Arithmetic operators" |
| 263 | 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 |
| 264 | \&\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 |
| 265 | attempt to convert arguments to integers. If both conversions are possible |
| 266 | without loss of precision, and the operation can be performed without |
| 267 | loss of precision then the integer result is used. Otherwise arguments are |
| 268 | converted to floating point format and the floating point result is used. |
| 269 | The caching of conversions (as described above) means that the integer |
| 270 | conversion does not throw away fractional parts on floating point numbers. |
| 271 | .IP "++" 4 |
| 272 | \&\f(CW\*(C`++\*(C'\fR behaves as the other operators above, except that if it is a string |
| 273 | matching the format \f(CW\*(C`/^[a\-zA\-Z]*[0\-9]*\ez/\*(C'\fR the string increment described |
| 274 | in perlop is used. |
| 275 | .ie n .IP "Arithmetic operators during ""use integer""" 4 |
| 276 | .el .IP "Arithmetic operators during \f(CWuse integer\fR" 4 |
| 277 | .IX Item "Arithmetic operators during use integer" |
| 278 | In scopes where \f(CW\*(C`use integer;\*(C'\fR is in force, nearly all the operators listed |
| 279 | above will force their argument(s) into integer format, and return an integer |
| 280 | result. The exceptions, \f(CW\*(C`abs\*(C'\fR, \f(CW\*(C`++\*(C'\fR and \f(CW\*(C`\-\-\*(C'\fR, do not change their |
| 281 | behavior with \f(CW\*(C`use integer;\*(C'\fR |
| 282 | .IP "Other mathematical operators" 4 |
| 283 | .IX Item "Other mathematical operators" |
| 284 | 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 |
| 285 | format. |
| 286 | .IP "Bitwise operators" 4 |
| 287 | .IX Item "Bitwise operators" |
| 288 | Arguments are forced into the integer format if not strings. |
| 289 | .ie n .IP "Bitwise operators during ""use integer""" 4 |
| 290 | .el .IP "Bitwise operators during \f(CWuse integer\fR" 4 |
| 291 | .IX Item "Bitwise operators during use integer" |
| 292 | forces arguments to integer format. Also shift operations internally use |
| 293 | signed integers rather than the default unsigned. |
| 294 | .IP "Operators which expect an integer" 4 |
| 295 | .IX Item "Operators which expect an integer" |
| 296 | force the argument into the integer format. This is applicable |
| 297 | to the third and fourth arguments of \f(CW\*(C`sysread\*(C'\fR, for example. |
| 298 | .IP "Operators which expect a string" 4 |
| 299 | .IX Item "Operators which expect a string" |
| 300 | force the argument into the string format. For example, this is |
| 301 | applicable to \f(CW\*(C`printf "%s", $value\*(C'\fR. |
| 302 | .PP |
| 303 | Though forcing an argument into a particular form does not change the |
| 304 | stored number, Perl remembers the result of such conversions. In |
| 305 | particular, though the first such conversion may be time\-consuming, |
| 306 | repeated operations will not need to redo the conversion. |
| 307 | .SH "AUTHOR" |
| 308 | .IX Header "AUTHOR" |
| 309 | Ilya Zakharevich \f(CW\*(C`ilya@math.ohio\-state.edu\*(C'\fR |
| 310 | .PP |
| 311 | Editorial adjustments by Gurusamy Sarathy <gsar@ActiveState.com> |
| 312 | .PP |
| 313 | Updates for 5.8.0 by Nicholas Clark <nick@ccl4.org> |
| 314 | .SH "SEE ALSO" |
| 315 | .IX Header "SEE ALSO" |
| 316 | overload, perlop |