Debian Codegraph @

upstream/opensuse-tumbleweed/man1/perlnumber.1

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "PERLNUMBER 1"
61 .TH PERLNUMBER 1 2026-03-09 "perl v5.42.1" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 perlnumber \- semantics of numbers and numeric operations in Perl
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 9
71 \& $n = 1234; # decimal integer
72 \& $n = 0b1110011; # binary integer
73 \& $n = 01234; # octal integer
74 \& $n = 0x1234; # hexadecimal integer
75 \& $n = 12.34e\-56; # exponential notation
76 \& $n = "\-12.34e56"; # number specified as a string
77 \& $n = "1234"; # number specified as a string
78 \& $n = "01234"; # Same as "1234"; a leading 0 in a string
79 \& # does not indicate that it is octal
80 .Ve
81 .SH DESCRIPTION
82 .IX Header "DESCRIPTION"
83 This document describes how Perl internally handles numeric values.
84 .PP
85 Perl\*(Aqs operator overloading facility is completely ignored here. Operator
86 overloading allows user\-defined behaviors for numbers, such as operations
87 over arbitrarily large integers, floating points numbers with arbitrary
88 precision, operations over "exotic" numbers such as modular arithmetic or
89 p\-adic arithmetic, and so on. See overload for details.
90 .SH "Storing numbers"
91 .IX Header "Storing numbers"
92 Perl can internally represent numbers in 3 different ways: as native
93 integers, as native floating point numbers, and as decimal strings.
94 Decimal strings may have an exponential notation part, as in \f(CW"12.34e\-56"\fR.
95 \&\fINative\fR here means "a format supported by the C compiler which was used
96 to build perl".
97 .PP
98 The term "native" does not mean quite as much when we talk about native
99 integers, as it does when native floating point numbers are involved.
100 The only implication of the term "native" on integers is that the limits for
101 the maximal and the minimal supported true integral quantities are close to
102 powers of 2. However, "native" floats have a most fundamental
103 restriction: they may represent only those numbers which have a relatively
104 "short" representation when converted to a binary fraction. For example,
105 0.9 cannot be represented by a native float, since the binary fraction
106 for 0.9 is infinite:
107 .PP
108 .Vb 1
109 \& binary0.1110011001100...
110 .Ve
111 .PP
112 with the sequence \f(CW1100\fR repeating again and again. In addition to this
113 limitation, the exponent of the binary number is also restricted when it
114 is represented as a floating point number. On typical hardware, floating
115 point values can store numbers with up to 53 binary digits, and with binary
116 exponents between \-1024 and 1024. In decimal representation this is close
117 to 16 decimal digits and decimal exponents in the range of \-304..304.
118 The upshot of all this is that Perl cannot store a number like
119 12345678901234567 as a floating point number on such architectures without
120 loss of information.
121 .PP
122 Similarly, decimal strings can represent only those numbers which have a
123 finite decimal expansion. Being strings, and thus of arbitrary length, there
124 is no practical limit for the exponent or number of decimal digits for these
125 numbers. (But realize that what we are discussing the rules for just the
126 \&\fIstorage\fR of these numbers. The fact that you can store such "large" numbers
127 does not mean that the \fIoperations\fR over these numbers will use all
128 of the significant digits.
129 See "Numeric operators and numeric conversions" for details.)
130 .PP
131 In fact numbers stored in the native integer format may be stored either
132 in the signed native form, or in the unsigned native form. Thus the limits
133 for Perl numbers stored as native integers would typically be \-2**31..2**32\-1,
134 with appropriate modifications in the case of 64\-bit integers. Again, this
135 does not mean that Perl can do operations only over integers in this range:
136 it is possible to store many more integers in floating point format.
137 .PP
138 Summing up, Perl numeric values can store only those numbers which have
139 a finite decimal expansion or a "short" binary expansion.
140 .SH "Numeric operators and numeric conversions"
141 .IX Header "Numeric operators and numeric conversions"
142 As mentioned earlier, Perl can store a number in any one of three formats,
143 but most operators typically understand only one of those formats. When
144 a numeric value is passed as an argument to such an operator, it will be
145 converted to the format understood by the operator.
146 .PP
147 Six such conversions are possible:
148 .PP
149 .Vb 6
150 \& native integer \-\-> native floating point (*)
151 \& native integer \-\-> decimal string
152 \& native floating_point \-\-> native integer (*)
153 \& native floating_point \-\-> decimal string (*)
154 \& decimal string \-\-> native integer
155 \& decimal string \-\-> native floating point (*)
156 .Ve
157 .PP
158 These conversions are governed by the following general rules:
159 .IP \(bu 4
160 If the source number can be represented in the target form, that
161 representation is used.
162 .IP \(bu 4
163 If the source number is outside of the limits representable in the target form,
164 a representation of the closest limit is used. (\fILoss of information\fR)
165 .IP \(bu 4
166 If the source number is between two numbers representable in the target form,
167 a representation of one of these numbers is used. (\fILoss of information\fR)
168 .IP \(bu 4
169 In \f(CW\*(C`native floating point \-\-> native integer\*(C'\fR conversions the magnitude
170 of the result is less than or equal to the magnitude of the source.
171 (\fI"Rounding to zero".\fR)
172 .IP \(bu 4
173 If the \f(CW\*(C`decimal string \-\-> native integer\*(C'\fR conversion cannot be done
174 without loss of information, the result is compatible with the conversion
175 sequence \f(CW\*(C`decimal_string \-\-> native_floating_point \-\-> native_integer\*(C'\fR.
176 In particular, rounding is strongly biased to 0, though a number like
177 \&\f(CW"0.99999999999999999999"\fR has a chance of being rounded to 1.
178 .PP
179 \&\fBRESTRICTION\fR: The conversions marked with \f(CW\*(C`(*)\*(C'\fR above involve steps
180 performed by the C compiler. In particular, bugs/features of the compiler
181 used may lead to breakage of some of the above rules.
182 .SH "Flavors of Perl numeric operations"
183 .IX Header "Flavors of Perl numeric operations"
184 Perl operations which take a numeric argument treat that argument in one of
185 four different ways: they may force it to one of the integer, floating, or
186 string formats; or they may behave differently depending on the format of the
187 operand. Forcing a numeric value to a particular format does not change the
188 number stored in the value.
189 .PP
190 All the operators which need an argument in the integer format treat the
191 argument as in modular arithmetic, e.g., \f(CW\*(C`mod 2**32\*(C'\fR on a 32\-bit
192 architecture. \f(CW\*(C`sprintf "%u", \-1\*(C'\fR therefore provides the same result as
193 \&\f(CW\*(C`sprintf "%u", ~0\*(C'\fR.
194 .IP "Arithmetic operators" 4
195 .IX Item "Arithmetic operators"
196 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
197 \&\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
198 attempt to convert arguments to integers. If both conversions are possible
199 without loss of precision, and the operation can be performed without
200 loss of precision then the integer result is used. Otherwise arguments are
201 converted to floating point format and the floating point result is used.
202 The caching of conversions (as described above) means that the integer
203 conversion does not throw away fractional parts on floating point numbers.
204 .IP ++ 4
205 \&\f(CW\*(C`++\*(C'\fR behaves as the other operators above, except that if it is a string
206 matching the format \f(CW\*(C`/^[a\-zA\-Z]*[0\-9]*\ez/\*(C'\fR the string increment described
207 in perlop is used.
208 .ie n .IP "Arithmetic operators during ""use integer""" 4
209 .el .IP "Arithmetic operators during \f(CWuse integer\fR" 4
210 .IX Item "Arithmetic operators during use integer"
211 In scopes where \f(CW\*(C`use integer;\*(C'\fR is in force, nearly all the operators listed
212 above will force their argument(s) into integer format, and return an integer
213 result. The exceptions, \f(CW\*(C`abs\*(C'\fR, \f(CW\*(C`++\*(C'\fR and \f(CW\*(C`\-\-\*(C'\fR, do not change their
214 behavior with \f(CW\*(C`use integer;\*(C'\fR
215 .IP "Other mathematical operators" 4
216 .IX Item "Other mathematical operators"
217 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
218 format.
219 .IP "Bitwise operators" 4
220 .IX Item "Bitwise operators"
221 Arguments are forced into the integer format if not strings.
222 .ie n .IP "Bitwise operators during ""use integer""" 4
223 .el .IP "Bitwise operators during \f(CWuse integer\fR" 4
224 .IX Item "Bitwise operators during use integer"
225 forces arguments to integer format. Also shift operations internally use
226 signed integers rather than the default unsigned.
227 .IP "Operators which expect an integer" 4
228 .IX Item "Operators which expect an integer"
229 force the argument into the integer format. This is applicable
230 to the third and fourth arguments of \f(CW\*(C`sysread\*(C'\fR, for example.
231 .IP "Operators which expect a string" 4
232 .IX Item "Operators which expect a string"
233 force the argument into the string format. For example, this is
234 applicable to \f(CW\*(C`printf "%s", $value\*(C'\fR.
235 .PP
236 Though forcing an argument into a particular form does not change the
237 stored number, Perl remembers the result of such conversions. In
238 particular, though the first such conversion may be time\-consuming,
239 repeated operations will not need to redo the conversion.
240 .SH AUTHOR
241 .IX Header "AUTHOR"
242 Ilya Zakharevich \f(CW\*(C`[email protected]\-state.edu\*(C'\fR
243 .PP
244 Editorial adjustments by Gurusamy Sarathy <[email protected]>
245 .PP
246 Updates for 5.8.0 by Nicholas Clark <[email protected]>
247 .SH "SEE ALSO"
248 .IX Header "SEE ALSO"
249 overload, perlop