perlnumber - semantics of numbers and numeric operations in Perl
$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 $n = v126.96.36.199; # number specified as a string, which in # turn is specified in terms of numbers :-)
This document describes how Perl internally handles numeric values.
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 ``exotic'' numbers such as modular arithmetic or p-adic arithmetic, and so on. See the overload manpage for details.
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
The term ``native'' 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 ``native'' on integers is that the limits for the maximal and the minimal supported true integral quantities are close to powers of 2. However, ``native'' floats have a most fundamental restriction: they may represent only those numbers which have a relatively ``short'' representation when converted to a binary fraction. For example, 0.9 cannot be respresented by a native float, since the binary fraction for 0.9 is infinite:
with the sequence
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 storage of these numbers. The fact that you can store such ``large'' numbers does not mean that that the operations over these numbers will use all of the significant digits. See Numeric operators and numeric conversions for details.)
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.
Summing up, Perl numeric values can store only those numbers which have a finite decimal expansion or a ``short'' binary expansion.
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.
Six such conversions are possible:
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 (*)
These conversions are governed by the following general rules:
RESTRICTION: The conversions marked with
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.
All the operators which need an argument in the integer format treat the
argument as in modular arithmetic, e.g.,
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.
Editorial adjustments by Gurusamy Sarathy <gsar@ActiveState.com>