Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking integer overflow in python

Tags:

class Solution(object):
    def reverse(self, x):
        """
        :type x: int
        :rtype: int
        """
        negative = False
        if(x < 0):
            x = x * -1
            negative = True
        else:
            x = x
        sum = 0
        dig = 1
        strX = str(x)
        lst = list(strX)
        for i in lst:
            sum += int(i) * dig
            dig *= 10

        if(abs(sum) > 2 ** 32):
            return 0
        elif(negative == True):
            return sum * -1
        else:
            return sum

This is a leetcode problem that asks us to reverse an integer. I know it's a dirty code but it still works but it does not return 0 when the reversed integer overflows. I tried to check that on the line

        if(abs(sum) > 2 ** 32):
            return 0

but one of the test cases gives me:

Input: 1563847412

Output: 2147483651

Expected: 0

First, I am not sure why this overflows, and I am not sure how to fix this.

Thanks!

like image 292
Dukakus17 Avatar asked Aug 06 '17 04:08

Dukakus17


People also ask

How do you check if an integer is overflow in Python?

Write a “C” function, int addOvf(int* result, int a, int b) If there is no overflow, the function places the resultant = sum a+b in “result” and returns 0. Otherwise it returns -1.

Why is there no integer overflow in Python?

Arbitrary precision In python 2, there are actually two integers types: int and long , where int is the C-style fixed-precision integer and long is the arbitrary-precision integer. Operations are automatically promoted to long if int is not sufficient, so there's no risk of overflowing.

How do you check for overflow conditions?

Overflow Detection – So overflow can be detected by checking Most Significant Bit(MSB) of two operands and answer. But Instead of using 3-bit Comparator Overflow can also be detected using 2 Bit Comparator just by checking Carry-in(C-in) and Carry-Out(C-out) from MSB's. Consider N-Bit Addition of 2's Complement number.


2 Answers

change if(abs(sum) > 2 ** 32): to if(abs(sum) > (2 ** 31 - 1)): or abs(sum) > (1 << 31) - 1): The largest 32 bit signed interger is actually not 2^32 but (2 ^ (31)) -1). because we need one bit reserve as the sign bit.

Read about it here of why The number 2,147,483,647 (or hexadecimal 7FFF,FFFF) is the maximum positive value for a 32-bit signed binary integer

like image 143
OLIVER.KOO Avatar answered Sep 22 '22 22:09

OLIVER.KOO


I guess some thing light weight like below could perhaps achieve the same logic, For someone else looking , the main overflow check after reversed 32 bit int is

if(abs(n) > (2 ** 31 - 1)):
                    return 0    

Full code below

def reverse(self, x):

            neg = False
            if x < 0:
                neg = True
                x = x * -1

            s = str(x)[::-1]
            n = int(s)
            if neg:
                n = n*-1
            if(abs(n) > (2 ** 31 - 1)):
                return 0
            return n
like image 40
varun Avatar answered Sep 21 '22 22:09

varun