1N/Aperlnumber - semantics of numbers and numeric operations in Perl
1N/A $n = 1234; # decimal integer
1N/A $n = 0b1110011; # binary integer
1N/A $n = 01234; # octal integer
1N/A $n = 0x1234; # hexadecimal integer
1N/A $n = 12.34e-56; # exponential notation
1N/A $n = "-12.34e56"; # number specified as a string
1N/A $n = "1234"; # number specified as a string
1N/AThis document describes how Perl internally handles numeric values.
1N/APerl's operator overloading facility is completely ignored here. Operator
1N/Aoverloading allows user-defined behaviors for numbers, such as operations
1N/Aover arbitrarily large integers, floating points numbers with arbitrary
1N/Aprecision, operations over "exotic" numbers such as modular arithmetic or
1N/Ap-adic arithmetic, and so on. See L<overload> for details.
1N/A=head1 Storing numbers
1N/APerl can internally represent numbers in 3 different ways: as native
1N/Aintegers, as native floating point numbers, and as decimal strings.
1N/ADecimal strings may have an exponential notation part, as in C<"12.34e-56">.
1N/AI<Native> here means "a format supported by the C compiler which was used
1N/AThe term "native" does not mean quite as much when we talk about native
1N/Aintegers, as it does when native floating point numbers are involved.
1N/AThe only implication of the term "native" on integers is that the limits for
1N/Athe maximal and the minimal supported true integral quantities are close to
1N/Apowers of 2. However, "native" floats have a most fundamental
1N/Arestriction: they may represent only those numbers which have a relatively
1N/A"short" representation when converted to a binary fraction. For example,
1N/A0.9 cannot be represented by a native float, since the binary fraction
1N/A binary0.1110011001100...
1N/Awith the sequence C<1100> repeating again and again. In addition to this
1N/Alimitation, the exponent of the binary number is also restricted when it
1N/Ais represented as a floating point number. On typical hardware, floating
1N/Apoint values can store numbers with up to 53 binary digits, and with binary
1N/Aexponents between -1024 and 1024. In decimal representation this is close
1N/Ato 16 decimal digits and decimal exponents in the range of -304..304.
1N/AThe upshot of all this is that Perl cannot store a number like
1N/A12345678901234567 as a floating point number on such architectures without
1N/ASimilarly, decimal strings can represent only those numbers which have a
1N/Afinite decimal expansion. Being strings, and thus of arbitrary length, there
1N/Ais no practical limit for the exponent or number of decimal digits for these
1N/Anumbers. (But realize that what we are discussing the rules for just the
1N/AI<storage> of these numbers. The fact that you can store such "large" numbers
1N/Adoes not mean that the I<operations> over these numbers will use all
1N/Aof the significant digits.
1N/ASee L<"Numeric operators and numeric conversions"> for details.)
1N/AIn fact numbers stored in the native integer format may be stored either
1N/Ain the signed native form, or in the unsigned native form. Thus the limits
1N/Afor Perl numbers stored as native integers would typically be -2**31..2**32-1,
1N/Awith appropriate modifications in the case of 64-bit integers. Again, this
1N/Adoes not mean that Perl can do operations only over integers in this range:
1N/Ait is possible to store many more integers in floating point format.
1N/ASumming up, Perl numeric values can store only those numbers which have
1N/Aa finite decimal expansion or a "short" binary expansion.
1N/A=head1 Numeric operators and numeric conversions
1N/AAs mentioned earlier, Perl can store a number in any one of three formats,
1N/Abut most operators typically understand only one of those formats. When
1N/Aa numeric value is passed as an argument to such an operator, it will be
1N/Aconverted to the format understood by the operator.
1N/ASix such conversions are possible:
1N/A native integer --> native floating point (*)
1N/A native integer --> decimal string
1N/A native floating_point --> native integer (*)
1N/A native floating_point --> decimal string (*)
1N/A decimal string --> native integer
1N/A decimal string --> native floating point (*)
1N/AThese conversions are governed by the following general rules:
1N/AIf the source number can be represented in the target form, that
1N/Arepresentation is used.
1N/AIf the source number is outside of the limits representable in the target form,
1N/Aa representation of the closest limit is used. (I<Loss of information>)
1N/AIf the source number is between two numbers representable in the target form,
1N/Aa representation of one of these numbers is used. (I<Loss of information>)
1N/AIn C<< native floating point --> native integer >> conversions the magnitude
1N/Aof the result is less than or equal to the magnitude of the source.
1N/A(I<"Rounding to zero".>)
1N/AIf the C<< decimal string --> native integer >> conversion cannot be done
1N/Awithout loss of information, the result is compatible with the conversion
1N/Asequence C<< decimal_string --> native_floating_point --> native_integer >>.
1N/AIn particular, rounding is strongly biased to 0, though a number like
1N/AC<"0.99999999999999999999"> has a chance of being rounded to 1.
1N/AB<RESTRICTION>: The conversions marked with C<(*)> above involve steps
1N/Aused may lead to breakage of some of the above rules.
1N/A=head1 Flavors of Perl numeric operations
1N/APerl operations which take a numeric argument treat that argument in one
1N/Astring formats, or they may behave differently depending on the format of
1N/Athe operand. Forcing a numeric value to a particular format does not
1N/Achange the number stored in the value.
1N/AAll the operators which need an argument in the integer format treat the
1N/Aargument as in modular arithmetic,
e.g., C<mod 2**32> on a 32-bit
1N/Aarchitecture. C<sprintf "%u", -1> therefore provides the same result as
1N/A=item Arithmetic operators
1N/AThe binary operators C<+> C<-> C<*> C</> C<%> C<==> C<!=> C<E<gt>> C<E<lt>>
1N/AC<E<gt>=> C<E<lt>=> and the unary operators C<-> C<abs> and C<--> will
1N/Aattempt to convert arguments to integers. If both conversions are possible
1N/Awithout loss of precision, and the operation can be performed without
1N/Aloss of precision then the integer result is used. Otherwise arguments are
1N/Aconverted to floating point format and the floating point result is used.
1N/AThe caching of conversions (as described above) means that the integer
1N/Aconversion does not throw away fractional parts on floating point numbers.
1N/AC<++> behaves as the other operators above, except that if it is a string
1N/Amatching the format C</^[a-zA-Z]*[0-9]*\z/> the string increment described
1N/Ain L<perlop> is used.
1N/A=item Arithmetic operators during C<use integer>
1N/AIn scopes where C<use integer;> is in force, nearly all the operators listed
1N/Aabove will force their argument(s) into integer format, and return an integer
1N/Aresult. The exceptions, C<abs>, C<++> and C<-->, do not change their
1N/Abehavior with C<use integer;>
1N/A=item Other mathematical operators
1N/AOperators such as C<**>, C<sin> and C<exp> force arguments to floating point
1N/A=item Bitwise operators
1N/AArguments are forced into the integer format if not strings.
1N/A=item Bitwise operators during C<use integer>
1N/Aforces arguments to integer format. Also shift operations internally use
1N/Asigned integers rather than the default unsigned.
1N/A=item Operators which expect an integer
1N/Aforce the argument into the integer format. This is applicable
1N/Ato the third and fourth arguments of C<sysread>, for example.
1N/A=item Operators which expect a string
1N/Aforce the argument into the string format. For example, this is
1N/Aapplicable to C<printf "%s", $value>.
1N/AThough forcing an argument into a particular form does not change the
1N/Astored number, Perl remembers the result of such conversions. In
1N/Aparticular, though the first such conversion may be time-consuming,
1N/Arepeated operations will not need to redo the conversion.
1N/AIlya Zakharevich C<ilya@math.ohio-state.edu>
1N/AEditorial adjustments by Gurusamy Sarathy <gsar@ActiveState.com>
1N/AUpdates for 5.8.0 by Nicholas Clark <nick@ccl4.org>
1N/AL<overload>, L<perlop>