Home > Software engineering >  How do I make my function work on negative numbers?
How do I make my function work on negative numbers?

Time:11-21

repeat = "y"
while repeat == "y":
    #First get the two integers from the user
    a = int(input("Enter the first integer: "))
    b = int(input("Enter the second integer: "))
    #Start the answer with 0
    answer = 0

    print("A", "B")
    print("---")
    print(a, b)
    #run loop until b is not zero
    while b != 0: 
        #loop while 'b' is odd number
        if (b % 2 != 0):
            answer = answer   a 
            print(a*2, b//2) 
            a = a*2 #double every 'a' integers
            b = b//2 #halve the 'b' integers
        #loop while 'b' is even number
        elif (b % 2 == 0):
            print(a*2, b//2)
            a = a*2 #double every 'a' integers
            b = b//2 #halve the 'b' integers
    print("The product is {}.".format(answer))
    repeat = input("Would you like to repeat? (y/n)")
print("Goodbye!")

I am writing a program that uses Ancient Egyptian method to multiply. My program works for positive numbers but not negative. How do I fix it so that if both inputted values of user are negative. My result should give the product of any two positive, negative or one negative and positive number. My current program gives the product for any two positive values, or negative a value and positive b value. However, when user enters a negative b value, it produces infinite outputs.

CodePudding user response:

Welcome to Stack Overflow!!

This is what the program returned when I used -2 on the second integer (b).

A B
---
1 -2
2 -1
4 -1
8 -1
16 -1

If you notice, B stays on -1, while A starts increasing *2 and it does indifinitely because the while loop doesn't break. Let's look at the code

while b != 0: 
        #loop while 'b' is odd number
        if (b % 2 != 0):
            answer = answer   a 
            print(a*2, b//2) 
            a = a*2 #double every 'a' integers
            b = b//2 #halve the 'b' integers
        #loop while 'b' is even number
        elif (b % 2 == 0):
            print(a*2, b//2)
            a = a*2 #double every 'a' integers
            b = b//2 #halve the 'b' integers

So on the if section, you do a=a*2 and b=b//2 since b%2!=0 (-1%2 is -1). However, for the while to break, you need to get b=0, and you get that by doing b=b//2. The problem, as Guy said before, is that when you get b=-1 (as you saw on my example), doing -1//2 will give you -1, instead of the supposed 0 that you'll get if you do 1//2. Since you won't get b=0, the program never stops multiplying.

The solution is simple, as guy mentioned, use b=int(b/2)

CodePudding user response:

The issue is with the floor division b//2, when b is negative the result will be the lower integer, so -0.5 will be rounded to -1. To avoid it cast to an int a regular division b = int(b / 2).

After removing duplicate code the while loop looks like that

while b != 0:
    if b % 2 != 0:
        answer = answer   a
    print(a * 2, int(b / 2))
    a = a * 2
    b = int(b / 2)

Edit

To get the correct sign you can check the expected sign after getting the numbers and multiple by 1 or -1 at the end. Since you only check a for the answer you need to work on a positive a

....
answer = 0
sign = 1 if a * b > 0 else -1
a = abs(a)

while b != 0:
    ....

print("The product is {}.".format(answer * sign))
  • Related