This document describes how Perl internally handles numeric values.
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 . Native here means ``a format supported by the C compiler which was used to build perl''.
with the sequence 1100 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.
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 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.
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.
These conversions are governed by the following general rules:
If the source number can be represented in the target form, that representation is used.
If the source number is outside of the limits representable in the target form, a representation of the closest limit is used. (Loss of information)
If the source number is between two numbers representable in the target form, a representation of one of these numbers is used. (Loss of information)
In native floating point -- conversions the magnitude of the result is less than or equal to the magnitude of the source. (``Rounding to zero.'')
being rounded to 1.
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.
as sprintf .
Arithmetic operators except, no integer
force the argument into the floating point format.
Arithmetic operators except, use integer
Bitwise operators, no integer
force the argument into the integer format if it is not a string.
Bitwise operators, use integer
force the argument into the integer format
Operators which expect an integer
force the argument into the integer format. This is applicable to the third and fourth arguments of sysread, for example.
Operators which expect a string
force the argument into the string format. For example, this is applicable to printf .
Ilya Zakharevich email@example.com