{
    "mode": "perldoc",
    "parameter": "number",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/number/json",
    "generated": "2026-06-11T15:25:57Z",
    "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, floating\npoints numbers with arbitrary precision, operations over \"exotic\" numbers such as modular\narithmetic or p-adic arithmetic, and so on. See overload for details.\n",
            "subsections": []
        },
        "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 notation\npart, as in \"12.34e-56\". *Native* here means \"a format supported by the C compiler which was\nused 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\" on\nintegers is that the limits for the maximal and the minimal supported true integral quantities\nare close to powers of 2. However, \"native\" floats have a most fundamental restriction: they may\nrepresent only those numbers which have a relatively \"short\" representation when converted to a\nbinary fraction. For example, 0.9 cannot be represented by a native float, since the binary\nfraction for 0.9 is infinite:\n\nbinary0.1110011001100...\n\nwith the sequence 1100 repeating again and again. In addition to this limitation, the exponent\nof the binary number is also restricted when it is represented as a floating point number. On\ntypical hardware, floating point values can store numbers with up to 53 binary digits, and with\nbinary exponents between -1024 and 1024. In decimal representation this is close to 16 decimal\ndigits and decimal exponents in the range of -304..304. The upshot of all this is that Perl\ncannot store a number like 12345678901234567 as a floating point number on such architectures\nwithout 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 discussing\nthe rules for just the *storage* of these numbers. The fact that you can store such \"large\"\nnumbers does not mean that the *operations* over these numbers will use all of the significant\ndigits. 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 integers\nwould typically be -231..232-1, with appropriate modifications in the case of 64-bit\nintegers. Again, this does not mean that Perl can do operations only over integers in this\nrange: 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",
            "subsections": []
        },
        "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 argument to\nsuch 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 0,\nthough 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 compiler.\nIn particular, bugs/features of the compiler used may lead to breakage of some of the above\nrules.\n",
            "subsections": []
        },
        "Flavors of Perl numeric operations": {
            "content": "Perl operations which take a numeric argument treat that argument in one of four different ways:\nthey may force it to one of the integer/floating/ string formats, or they may behave differently\ndepending on the format of the operand. Forcing a numeric value to a particular format does not\nchange the number stored in the value.\n\nAll the operators which need an argument in the integer format treat the argument as in modular\narithmetic, e.g., \"mod 232\" on a 32-bit architecture. \"sprintf \"%u\", -1\" therefore provides\nthe same result as \"sprintf \"%u\", ~0\".\n\nArithmetic operators\nThe binary operators \"+\" \"-\" \"*\" \"/\" \"%\" \"==\" \"!=\" \">\" \"<\" \">=\" \"<=\" and the unary operators\n\"-\" \"abs\" and \"--\" will attempt to convert arguments to integers. If both conversions are\npossible without loss of precision, and the operation can be performed without loss of\nprecision then the integer result is used. Otherwise arguments are converted to floating\npoint format and the floating point result is used. The caching of conversions (as described\nabove) means that the integer conversion does not throw away fractional parts on floating\npoint numbers.\n\n++  \"++\" behaves as the other operators above, except that if it is a string matching the format\n\"/^[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 force\ntheir argument(s) into integer format, and return an integer result. The exceptions, \"abs\",\n\"++\" 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 \"%s\",\n$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 may be\ntime-consuming, repeated operations will not need to redo the conversion.\n",
            "subsections": []
        },
        "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",
            "subsections": []
        }
    },
    "summary": "perlnumber - semantics of numbers and numeric operations in Perl",
    "flags": [],
    "examples": [],
    "see_also": []
}