Debian Codegraph @

upstream/debian-unstable/man1/perlnumber.1

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