.\" Automatically generated by Pod::Man 2.22 (Pod::Simple 3.13)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  \*(C+ will
.\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
.\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.ie \nF \{\
.    de IX
.    tm Index:\\$1\t\\n%\t"\\$2"
..
.    nr % 0
.    rr F
.\}
.el \{\
.    de IX
..
.\}
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear.  Run.  Save yourself.  No user-serviceable parts.
.    \" fudge factors for nroff and troff
.if n \{\
.    ds #H 0
.    ds #V .8m
.    ds #F .3m
.    ds #[ \f1
.    ds #] \fP
.\}
.if t \{\
.    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
.    ds #V .6m
.    ds #F 0
.    ds #[ \&
.    ds #] \&
.\}
.    \" simple accents for nroff and troff
.if n \{\
.    ds ' \&
.    ds ` \&
.    ds ^ \&
.    ds , \&
.    ds ~ ~
.    ds /
.\}
.if t \{\
.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
.    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
.    \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
.    \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
.    \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
.    ds : e
.    ds 8 ss
.    ds o a
.    ds d- d\h'-1'\(ga
.    ds D- D\h'-1'\(hy
.    ds th \o'bp'
.    ds Th \o'LP'
.    ds ae ae
.    ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "PERLNUMBER 1"
.TH PERLNUMBER 1 "2009-02-12" "perl v5.10.1" "Perl Programmers Reference Guide"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
perlnumber \- semantics of numbers and numeric operations in Perl
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 7
\&    $n = 1234;              # decimal integer
\&    $n = 0b1110011;         # binary integer
\&    $n = 01234;             # octal integer
\&    $n = 0x1234;            # hexadecimal integer
\&    $n = 12.34e\-56;         # exponential notation
\&    $n = "\-12.34e56";       # number specified as a string
\&    $n = "1234";            # number specified as a string
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
This document describes how Perl internally handles numeric values.
.PP
Perl's operator overloading facility is completely ignored here.  Operator
overloading allows user-defined behaviors for numbers, such as operations
over arbitrarily large integers, floating points numbers with arbitrary
precision, operations over \*(L"exotic\*(R" numbers such as modular arithmetic or
p\-adic arithmetic, and so on.  See overload for details.
.SH "Storing numbers"
.IX Header "Storing numbers"
Perl can internally represent numbers in 3 different ways: as native
integers, as native floating point numbers, and as decimal strings.
Decimal strings may have an exponential notation part, as in \f(CW"12.34e\-56"\fR.
\&\fINative\fR here means \*(L"a format supported by the C compiler which was used
to build perl\*(R".
.PP
The term \*(L"native\*(R" does not mean quite as much when we talk about native
integers, as it does when native floating point numbers are involved.
The only implication of the term \*(L"native\*(R" on integers is that the limits for
the maximal and the minimal supported true integral quantities are close to
powers of 2.  However, \*(L"native\*(R" floats have a most fundamental
restriction: they may represent only those numbers which have a relatively
\&\*(L"short\*(R" representation when converted to a binary fraction.  For example,
0.9 cannot be represented by a native float, since the binary fraction
for 0.9 is infinite:
.PP
.Vb 1
\&  binary0.1110011001100...
.Ve
.PP
with the sequence \f(CW1100\fR repeating again and again.  In addition to this
limitation,  the exponent of the binary number is also restricted when it
is represented as a floating point number.  On typical hardware, floating
point values can store numbers with up to 53 binary digits, and with binary
exponents between \-1024 and 1024.  In decimal representation this is close
to 16 decimal digits and decimal exponents in the range of \-304..304.
The upshot of all this is that Perl cannot store a number like
12345678901234567 as a floating point number on such architectures without
loss of information.
.PP
Similarly, decimal strings can represent only those numbers which have a
finite decimal expansion.  Being strings, and thus of arbitrary length, there
is no practical limit for the exponent or number of decimal digits for these
numbers.  (But realize that what we are discussing the rules for just the
\&\fIstorage\fR of these numbers.  The fact that you can store such \*(L"large\*(R" numbers
does not mean that the \fIoperations\fR over these numbers will use all
of the significant digits.
See \*(L"Numeric operators and numeric conversions\*(R" for details.)
.PP
In fact numbers stored in the native integer format may be stored either
in the signed native form, or in the unsigned native form.  Thus the limits
for Perl numbers stored as native integers would typically be \-2**31..2**32\-1,
with appropriate modifications in the case of 64\-bit integers.  Again, this
does not mean that Perl can do operations only over integers in this range:
it is possible to store many more integers in floating point format.
.PP
Summing up, Perl numeric values can store only those numbers which have
a finite decimal expansion or a \*(L"short\*(R" binary expansion.
.SH "Numeric operators and numeric conversions"
.IX Header "Numeric operators and numeric conversions"
As mentioned earlier, Perl can store a number in any one of three formats,
but most operators typically understand only one of those formats.  When
a numeric value is passed as an argument to such an operator, it will be
converted to the format understood by the operator.
.PP
Six such conversions are possible:
.PP
.Vb 6
\&  native integer        \-\-> native floating point       (*)
\&  native integer        \-\-> decimal string
\&  native floating_point \-\-> native integer              (*)
\&  native floating_point \-\-> decimal string              (*)
\&  decimal string        \-\-> native integer
\&  decimal string        \-\-> native floating point       (*)
.Ve
.PP
These conversions are governed by the following general rules:
.IP "\(bu" 4
If the source number can be represented in the target form, that
representation is used.
.IP "\(bu" 4
If the source number is outside of the limits representable in the target form,
a representation of the closest limit is used.  (\fILoss of information\fR)
.IP "\(bu" 4
If the source number is between two numbers representable in the target form,
a representation of one of these numbers is used.  (\fILoss of information\fR)
.IP "\(bu" 4
In \f(CW\*(C`native floating point \-\-> native integer\*(C'\fR conversions the magnitude
of the result is less than or equal to the magnitude of the source.
(\fI\*(L"Rounding to zero\*(R".\fR)
.IP "\(bu" 4
If the \f(CW\*(C`decimal string \-\-> native integer\*(C'\fR conversion cannot be done
without loss of information, the result is compatible with the conversion
sequence \f(CW\*(C`decimal_string \-\-> native_floating_point \-\-> native_integer\*(C'\fR.
In particular, rounding is strongly biased to 0, though a number like
\&\f(CW"0.99999999999999999999"\fR has a chance of being rounded to 1.
.PP
\&\fB\s-1RESTRICTION\s0\fR: The conversions marked with \f(CW\*(C`(*)\*(C'\fR above involve steps
performed by the C compiler.  In particular, bugs/features of the compiler
used may lead to breakage of some of the above rules.
.SH "Flavors of Perl numeric operations"
.IX Header "Flavors of Perl numeric operations"
Perl operations which take a numeric argument treat that argument in one
of four different ways: they may force it to one of the integer/floating/
string formats, or they may behave differently depending on the format of
the operand.  Forcing a numeric value to a particular format does not
change the number stored in the value.
.PP
All the operators which need an argument in the integer format treat the
argument as in modular arithmetic, e.g., \f(CW\*(C`mod 2**32\*(C'\fR on a 32\-bit
architecture.  \f(CW\*(C`sprintf "%u", \-1\*(C'\fR therefore provides the same result as
\&\f(CW\*(C`sprintf "%u", ~0\*(C'\fR.
.IP "Arithmetic operators" 4
.IX Item "Arithmetic operators"
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
\&\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
attempt to convert arguments to integers.  If both conversions are possible
without loss of precision, and the operation can be performed without
loss of precision then the integer result is used.  Otherwise arguments are
converted to floating point format and the floating point result is used.
The caching of conversions (as described above) means that the integer
conversion does not throw away fractional parts on floating point numbers.
.IP "++" 4
\&\f(CW\*(C`++\*(C'\fR behaves as the other operators above, except that if it is a string
matching the format \f(CW\*(C`/^[a\-zA\-Z]*[0\-9]*\ez/\*(C'\fR the string increment described
in perlop is used.
.ie n .IP "Arithmetic operators during ""use integer""" 4
.el .IP "Arithmetic operators during \f(CWuse integer\fR" 4
.IX Item "Arithmetic operators during use integer"
In scopes where \f(CW\*(C`use integer;\*(C'\fR is in force, nearly all the operators listed
above will force their argument(s) into integer format, and return an integer
result.  The exceptions, \f(CW\*(C`abs\*(C'\fR, \f(CW\*(C`++\*(C'\fR and \f(CW\*(C`\-\-\*(C'\fR, do not change their
behavior with \f(CW\*(C`use integer;\*(C'\fR
.IP "Other mathematical operators" 4
.IX Item "Other mathematical operators"
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
format.
.IP "Bitwise operators" 4
.IX Item "Bitwise operators"
Arguments are forced into the integer format if not strings.
.ie n .IP "Bitwise operators during ""use integer""" 4
.el .IP "Bitwise operators during \f(CWuse integer\fR" 4
.IX Item "Bitwise operators during use integer"
forces arguments to integer format. Also shift operations internally use
signed integers rather than the default unsigned.
.IP "Operators which expect an integer" 4
.IX Item "Operators which expect an integer"
force the argument into the integer format.  This is applicable
to the third and fourth arguments of \f(CW\*(C`sysread\*(C'\fR, for example.
.IP "Operators which expect a string" 4
.IX Item "Operators which expect a string"
force the argument into the string format.  For example, this is
applicable to \f(CW\*(C`printf "%s", $value\*(C'\fR.
.PP
Though forcing an argument into a particular form does not change the
stored number, Perl remembers the result of such conversions.  In
particular, though the first such conversion may be time-consuming,
repeated operations will not need to redo the conversion.
.SH "AUTHOR"
.IX Header "AUTHOR"
Ilya Zakharevich \f(CW\*(C`ilya@math.ohio\-state.edu\*(C'\fR
.PP
Editorial adjustments by Gurusamy Sarathy <gsar@ActiveState.com>
.PP
Updates for 5.8.0 by Nicholas Clark <nick@ccl4.org>
.SH "SEE ALSO"
.IX Header "SEE ALSO"
overload, perlop
