{
    "mode": "man",
    "parameter": "perlnumber",
    "section": "1",
    "url": "https://www.chedong.com/phpMan.php/man/perlnumber/1/json",
    "generated": "2026-05-30T07:09:37Z",
    "synopsis": "$n = 1234;              # decimal integer\n$n = 0b1110011;         # binary integer\n$n = 01234;             # octal integer\n$n = 0x1234;            # hexadecimal integer\n$n = 12.34e-56;         # exponential notation\n$n = \"-12.34e56\";       # number specified as a string\n$n = \"1234\";            # number specified as a string",
    "sections": {
        "NAME": {
            "content": "perlnumber - semantics of numbers and numeric operations in Perl\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "$n = 1234;              # decimal integer\n$n = 0b1110011;         # binary integer\n$n = 01234;             # octal integer\n$n = 0x1234;            # hexadecimal integer\n$n = 12.34e-56;         # exponential notation\n$n = \"-12.34e56\";       # number specified as a string\n$n = \"1234\";            # number specified as a string\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This document describes how Perl internally handles numeric values.\n\nPerl's operator overloading facility is completely ignored here.  Operator overloading allows\nuser-defined behaviors for numbers, such as operations over arbitrarily large integers,\nfloating points numbers with arbitrary precision, operations over \"exotic\" numbers such as\nmodular arithmetic or p-adic arithmetic, and so on.  See overload for details.\n",
            "subsections": [
                {
                    "name": "Storing numbers",
                    "content": "Perl can internally represent numbers in 3 different ways: as native integers, as native\nfloating point numbers, and as decimal strings.  Decimal strings may have an exponential\nnotation part, as in \"12.34e-56\".  Native here means \"a format supported by the C compiler\nwhich was used to build perl\".\n\nThe term \"native\" does not mean quite as much when we talk about native integers, as it does\nwhen native floating point numbers are involved.  The only implication of the term \"native\"\non integers is that the limits for the maximal and the minimal supported true integral\nquantities are close to powers of 2.  However, \"native\" floats have a most fundamental\nrestriction: they may represent only those numbers which have a relatively \"short\"\nrepresentation when converted to a binary fraction.  For example, 0.9 cannot be represented\nby a native float, since the binary fraction for 0.9 is infinite:\n\nbinary0.1110011001100...\n\nwith the sequence 1100 repeating again and again.  In addition to this limitation,  the\nexponent of the binary number is also restricted when it is represented as a floating point\nnumber.  On typical hardware, floating point values can store numbers with up to 53 binary\ndigits, and with binary exponents between -1024 and 1024.  In decimal representation this is\nclose to 16 decimal digits and decimal exponents in the range of -304..304.  The upshot of\nall this is that Perl cannot store a number like 12345678901234567 as a floating point number\non such architectures without loss of information.\n\nSimilarly, decimal strings can represent only those numbers which have a finite decimal\nexpansion.  Being strings, and thus of arbitrary length, there is no practical limit for the\nexponent or number of decimal digits for these numbers.  (But realize that what we are\ndiscussing the rules for just the storage of these numbers.  The fact that you can store such\n\"large\" numbers does not mean that the operations over these numbers will use all of the\nsignificant digits.  See \"Numeric operators and numeric conversions\" for details.)\n\nIn fact numbers stored in the native integer format may be stored either in the signed native\nform, or in the unsigned native form.  Thus the limits for Perl numbers stored as native\nintegers would typically be -231..232-1, with appropriate modifications in the case of\n64-bit integers.  Again, this does not mean that Perl can do operations only over integers in\nthis range: it is possible to store many more integers in floating point format.\n\nSumming up, Perl numeric values can store only those numbers which have a finite decimal\nexpansion or a \"short\" binary expansion.\n"
                },
                {
                    "name": "Numeric operators and numeric conversions",
                    "content": "As mentioned earlier, Perl can store a number in any one of three formats, but most operators\ntypically understand only one of those formats.  When a numeric value is passed as an\nargument to such an operator, it will be converted to the format understood by the operator.\n\nSix such conversions are possible:\n\nnative integer        --> native floating point       (*)\nnative integer        --> decimal string\nnative floatingpoint --> native integer              (*)\nnative floatingpoint --> decimal string              (*)\ndecimal string        --> native integer\ndecimal string        --> native floating point       (*)\n\nThese conversions are governed by the following general rules:\n\n•   If the source number can be represented in the target form, that representation is used.\n\n•   If the source number is outside of the limits representable in the target form, a\nrepresentation of the closest limit is used.  (Loss of information)\n\n•   If the source number is between two numbers representable in the target form, a\nrepresentation of one of these numbers is used.  (Loss of information)\n\n•   In \"native floating point --> native integer\" conversions the magnitude of the result is\nless than or equal to the magnitude of the source.  (\"Rounding to zero\".)\n\n•   If the \"decimal string --> native integer\" conversion cannot be done without loss of\ninformation, the result is compatible with the conversion sequence \"decimalstring -->\nnativefloatingpoint --> nativeinteger\".  In particular, rounding is strongly biased to\n0, though a number like \"0.99999999999999999999\" has a chance of being rounded to 1.\n\nRESTRICTION: The conversions marked with \"(*)\" above involve steps performed by the C\ncompiler.  In particular, bugs/features of the compiler used may lead to breakage of some of\nthe above rules.\n"
                },
                {
                    "name": "Flavors of Perl numeric operations",
                    "content": "Perl operations which take a numeric argument treat that argument in one of four different\nways: they may force it to one of the integer/floating/ string formats, or they may behave\ndifferently depending on the format of the operand.  Forcing a numeric value to a particular\nformat does not change the number stored in the value.\n\nAll the operators which need an argument in the integer format treat the argument as in\nmodular arithmetic, e.g., \"mod 232\" on a 32-bit architecture.  \"sprintf \"%u\", -1\" therefore\nprovides the same result as \"sprintf \"%u\", ~0\".\n\nArithmetic operators\nThe binary operators \"+\" \"-\" \"*\" \"/\" \"%\" \"==\" \"!=\" \">\" \"<\" \">=\" \"<=\" and the unary\noperators \"-\" \"abs\" and \"--\" will attempt to convert arguments to integers.  If both\nconversions are possible without loss of precision, and the operation can be performed\nwithout loss of precision then the integer result is used.  Otherwise arguments are\nconverted to floating point format and the floating point result is used.  The caching of\nconversions (as described above) means that the integer conversion does not throw away\nfractional parts on floating point numbers.\n\n++  \"++\" behaves as the other operators above, except that if it is a string matching the\nformat \"/^[a-zA-Z]*[0-9]*\\z/\" the string increment described in perlop is used.\n\nArithmetic operators during \"use integer\"\nIn scopes where \"use integer;\" is in force, nearly all the operators listed above will\nforce their argument(s) into integer format, and return an integer result.  The\nexceptions, \"abs\", \"++\" and \"--\", do not change their behavior with \"use integer;\"\n\nOther mathematical operators\nOperators such as \"\", \"sin\" and \"exp\" force arguments to floating point format.\n\nBitwise operators\nArguments are forced into the integer format if not strings.\n\nBitwise operators during \"use integer\"\nforces arguments to integer format. Also shift operations internally use signed integers\nrather than the default unsigned.\n\nOperators which expect an integer\nforce the argument into the integer format.  This is applicable to the third and fourth\narguments of \"sysread\", for example.\n\nOperators which expect a string\nforce the argument into the string format.  For example, this is applicable to \"printf\n\"%s\", $value\".\n\nThough forcing an argument into a particular form does not change the stored number, Perl\nremembers the result of such conversions.  In particular, though the first such conversion\nmay be time-consuming, repeated operations will not need to redo the conversion.\n"
                }
            ]
        },
        "AUTHOR": {
            "content": "Ilya Zakharevich \"ilya@math.ohio-state.edu\"\n\nEditorial adjustments by Gurusamy Sarathy <gsar@ActiveState.com>\n\nUpdates for 5.8.0 by Nicholas Clark <nick@ccl4.org>\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "overload, perlop\n\n\n\nperl v5.34.0                                 2025-07-25                                PERLNUMBER(1)",
            "subsections": []
        }
    },
    "summary": "perlnumber - semantics of numbers and numeric operations in Perl",
    "flags": [],
    "examples": [],
    "see_also": []
}