Fix negative root calculation

This commit is contained in:
Stanislav Mykhailenko 2022-10-19 20:51:38 +03:00
parent 434c7bec09
commit 538ddc2eea
GPG key ID: 1E95E66A9C9D6A36

View file

@ -10,14 +10,12 @@
# 4 - Attempting to extract a root of a negative number with an even degree
# 5 - Invalid operation entered
import sys
try:
numberA = float(input("Enter number A: "))
numberB = float(input("Enter number B: "))
except ValueError:
print("Invalid numbers entered.")
sys.exit(1)
exit(1)
print(
'''
@ -41,20 +39,23 @@ elif operation == "*" or operation == "×": # ASCII asterisk or Unicode multipli
elif operation == "/" or operation == "÷": # ASCII slash or Unicode division sign
if numberB == 0:
print("Division by zero.")
sys.exit(2)
exit(2)
else:
print(numberA / numberB)
elif operation == "root" or operation == "": # Unicode radical symbol
if numberB < 1 or not numberB.is_integer():
print("Can't extract a root with non-natural degree.")
sys.exit(3)
exit(3)
elif (numberB % 2) == 0 and numberA < 0:
print("Can't extract a root of a negative number with an even degree.")
sys.exit(4)
exit(4)
else:
if numberA < 0:
print(-abs(numberA ** (1 / numberB)))
else:
print(numberA ** (1 / numberB))
elif operation == "square" or operation == "²": # Unicode superscript 2
print("Number A square: " + str(numberA ** 2) + "\nNumber B square: " + str(numberB ** 2))
else:
print("Invalid operation.")
sys.exit(5)
exit(5)