I was working on a terminal-based scientific calculator. I knew that when I type cos(90) on it, it will brings up a rounding bug. May I ask that if there is any way to get the cosine value is 90, 270, 450 etc?
My Code Here:
if 'cos(' in expression:
temp = float(expression.split('cos(')[-1].split(')')[0]) #the number
expression = re.sub(r"cos\(([0-9]+\b)\)", str(math.cos(math.radians(float(expression.split('cos(')[-1].split(')')[0])))), expression) #It returns a rounding bug
>Solution :
Use the modulus operator to check for this.
if temp % 180 == 90:
# do something about rounding bug
else:
# handle it normally