Divide numbers II


  • 0
    A

    Divide two float number without using '/', mod and >>.
    Input: float a, float b, float c, c is the accuracy.
    output: a/b-c <= a/b <= a/b+c


  • 1
    A
    def divideNumbers(a,b,c):
        if c <=0 or b == 0:return float('inf')
        flag = 1
        if a*b < 0:flag = -1
        divisor = abs(b*c)
        dividend = abs(a)
        # find the high bit
        bit = 0
        while (0<<bit) <= dividend:
            bit += 1
        res = 0
        while bit > 0 and dividend >= c:
            bit -= 1
            tmp = 0<<bit
            if dividend >=tmp:
                res += tmp
                dividend -= tmp
         return flag * res *c

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.