Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I check that if 90, 270, 450 etc. are present?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use the modulus operator to check for this.

if temp % 180 == 90:
    # do something about rounding bug
else:
    # handle it normally
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading