PERLNUMBER(1)
π perlnumber β bestie's guide to how Perl does numbers π
okay so this is perlnumber, from the Perl Programmers Reference Guide. it's literally just the tea on how Perl handles numbers and numeric operations internally. stay with me, it's giving very "math class but make it cute" π
β¨ SYNOPSIS β¨
$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 = "01234"; # Same as "1234"; a leading 0 in a string
# does not indicate that it is octal
look at all those number formats bestie, she contains multitudes π
πΈ DESCRIPTION πΈ
so this whole doc is about how Perl internally handles numeric values. the lore. the mechanics. π§
heads up: Perl's operator overloading facility is completely ignored here. like, we're not talking about it at all.
operator overloading is the thing that lets you define your own behaviors for numbers β think operations over arbitrarily large integers, floating point numbers with arbitrary precision, operations over "exotic" numbers like modular arithmetic or p-adic arithmetic, and so on. if you want the details on that, go read overload. not our problem today π«Ά
π Storing numbers π
Perl can internally represent numbers in 3 different ways:
- as native integers
- as native floating point numbers
- as decimal strings
decimal strings may have an exponential notation part, as in "12.34e-56". and when we say "native" here, we mean "a format supported by the C compiler which was used to build perl". iykyk π¦
so, 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. that's it. chill.
but "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 represented by a native float, since the binary fraction for 0.9 is infinite:
binary0.1110011001100...
with the sequence 1100 repeating again and again. it literally never stops. iconic behavior tbh π
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. rip π
similarly, decimal strings can represent only those numbers which have a finite decimal expansion. but 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 here is 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. so 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. she's flexible like that β¨
summing up: Perl numeric values can store only those numbers which have a finite decimal expansion or a "short" binary expansion. periodt.
π¦ 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. so when a numeric value is passed as an argument to such an operator, it will be converted to the format understood by the operator. no choice, bestie, it's the rules π
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:
- 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 --> native integerconversions the magnitude of the result is less than or equal to the magnitude of the source. ("rounding to zero.") - if the
decimal string --> native integerconversion cannot be done without loss of information, the result is compatible with the conversion sequencedecimal_string --> native_floating_point --> native_integer. in particular, rounding is strongly biased to 0, though a number like"0.99999999999999999999"has a chance of being rounded to1. sneakyyyy π
RESTRICTION: the conversions marked with "(*)" 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. not us, not our fault, blame the compiler fr fr π
π 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, or 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. important!! don't forget that!! β¨
all the operators which need an argument in the integer format treat the argument as in modular arithmetic, e.g., "mod 2**32" on a 32-bit architecture. so sprintf "%u", -1 therefore provides the same result as sprintf "%u", ~0. slay π
β Arithmetic operators
the binary operators "+" "-" "*" "/" "%" "==" "!=" ">" "<" ">=" "<=" and the unary operators "-" "abs" and "--" 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. cute, right? π
β ++
"++" behaves as the other operators above, except that if it is a string matching the format /^[a-zA-Z]*[0-9]*\z/ the string increment described in perlop is used. special little guy π¦
β Arithmetic operators during use integer
in scopes where "use integer;" is in force, nearly all the operators listed above will force their argument(s) into integer format, and return an integer result. the exceptions, "abs", "++" and "--", do not change their behavior with "use integer;". they simply refuse to be influenced, and honestly? respect π
β Other mathematical operators
operators such as "**", "sin" and "exp" force arguments to floating point format. no integer era for them π«
β Bitwise operators
arguments are forced into the integer format if not strings.
β Bitwise operators during use integer
forces arguments to integer format. also shift operations internally use signed integers rather than the default unsigned. plot twist π³
β 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 "%s", $value". π
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. she learned it the first time and she's not doing it again bestie β¨
π AUTHOR π
Ilya Zakharevich "ilya@math.ohio-state.edu"
editorial adjustments by Gurusamy Sarathy <gsar@ActiveState.com>
updates for 5.8.0 by Nicholas Clark <nick@ccl4.org>
π SEE ALSO π
overload, perlop
okay that's the whole page bestie, you're officially a Perl numbers girlie now π«Ά
this was perlnumber, from perl v5.42.3, dated 2026-08-03 β Perl Programmers Reference Guide energy only πβ¨
π¦ where this page comes from
- fedora 43: perl-doc 5.42.3-524.fc43 Β· (GPL-1.0-or-later OR Artistic-1.0-Perl) AND Unicode-3.0 AND LicenseRef-Fedora-UltraPermissive
- fedora 44: perl-doc 5.42.3-525.fc44 Β· (GPL-1.0-or-later OR Artistic-1.0-Perl) AND Unicode-3.0 AND LicenseRef-Fedora-UltraPermissive
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms