Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7412425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:32:17+00:00 2026-05-29T06:32:17+00:00

When the value of an number goes beyond the integer range, python promotes it

  • 0

When the value of an number goes beyond the integer range, python promotes it to a long. But when the value comes back to the integer range, why doesn’t it get demoted to an int?

>>> i=2147483647
>>> type(i)
<type 'int'>
>>> i = i + 1
>>> type(i)
<type 'long'>
>>> i = i - 10
>>> type(i)
<type 'long'>
>>> i
2147483638L
>>> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:32:18+00:00Added an answer on May 29, 2026 at 6:32 am

    From the python source (in file Objects/longobject.c):

    static PyLongObject *
    x_add(PyLongObject *a, PyLongObject *b)
    {
        Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
        PyLongObject *z;
        int i;
        digit carry = 0;
    
        /* Ensure a is the larger of the two: */
        if (size_a < size_b) {
            { PyLongObject *temp = a; a = b; b = temp; }
            { Py_ssize_t size_temp = size_a;
              size_a = size_b;
              size_b = size_temp; }
        }
        z = _PyLong_New(size_a+1);
        if (z == NULL)
            return NULL;
        for (i = 0; i < size_b; ++i) {
            carry += a->ob_digit[i] + b->ob_digit[i];
            z->ob_digit[i] = carry & MASK;
            carry >>= SHIFT;
        }
        for (; i < size_a; ++i) {
            carry += a->ob_digit[i];
            z->ob_digit[i] = carry & MASK;
            carry >>= SHIFT;
        }
        z->ob_digit[i] = carry;
        return long_normalize(z);
    }
    
    /* Subtract the absolute values of two integers. */
    
    static PyLongObject *
    x_sub(PyLongObject *a, PyLongObject *b)
    {
        Py_ssize_t size_a = ABS(a->ob_size), size_b = ABS(b->ob_size);
        PyLongObject *z;
        Py_ssize_t i;
        int sign = 1;
        digit borrow = 0;
    
        /* Ensure a is the larger of the two: */
        if (size_a < size_b) {
            sign = -1;
            { PyLongObject *temp = a; a = b; b = temp; }
            { Py_ssize_t size_temp = size_a;
              size_a = size_b;
              size_b = size_temp; }
        }
        else if (size_a == size_b) {
            /* Find highest digit where a and b differ: */
            i = size_a;
            while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i])
                ;
            if (i < 0)
                return _PyLong_New(0);
            if (a->ob_digit[i] < b->ob_digit[i]) {
                sign = -1;
                { PyLongObject *temp = a; a = b; b = temp; }
            }
            size_a = size_b = i+1;
        }
        z = _PyLong_New(size_a);
        if (z == NULL)
            return NULL;
        for (i = 0; i < size_b; ++i) {
            /* The following assumes unsigned arithmetic
               works module 2**N for some N>SHIFT. */
            borrow = a->ob_digit[i] - b->ob_digit[i] - borrow;
            z->ob_digit[i] = borrow & MASK;
            borrow >>= SHIFT;
            borrow &= 1; /* Keep only one sign bit */
        }
        for (; i < size_a; ++i) {
            borrow = a->ob_digit[i] - borrow;
            z->ob_digit[i] = borrow & MASK;
            borrow >>= SHIFT;
            borrow &= 1; /* Keep only one sign bit */
        }
        assert(borrow == 0);
        if (sign < 0)
            z->ob_size = -(z->ob_size);
        return long_normalize(z);
    }
    

    Note that the return types of both procedures are PyLongObject *.

    What this shows is that addition and subtraction of longs yield more longs in python, regardless of whether the values could fit in ints.

    Example:

    >>> 3L + 4L
    7L
    

    And here are python’s coercion rules, specifically:

    For objects x and y, first x.__add__(y) is tried. If this is not
    implemented or returns NotImplemented, y.__add__(x) is tried. If this
    is also not implemented or returns NotImplemented, a TypeError
    exception is raised.

    So doing i - 10, where i is a long, results in another long.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the integer value of The number selected of the item.
How do you get the absolute value of a number in vb.net? Is there
I need to convert a number to another value based on a range: ie:
I know that question doesn't make much sense, but here goes: Times Table Authority
I have a generator (numbers) and a value (number). I would like to iterate
I want to pass a number value to a Timer. How do I do
I have a C# method that projects the value of a number from an
I have a number of code value tables that contain a code and a
If the value after the shift operator is greater than the number of bits
How do you compare a value from jQuery with a fixed number? I thought

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.