1

I'm in my first year of CS and we use Python3. I managed to code properly and submitted it into gradescope, but it was incorrect because one decimal was missing (the 4 at the end). My professor told me to rearrange the equation, so I tried swapping the numbers around, but it still didn't give me the answer I want.

I swapped the multiplied numbers with the one beside it, but it only threw off the number more.

import math
import stdio
import sys

theta = float(sys.argv[1])
n1 = float(sys.argv[2])
n2 = float(sys.argv[3])

theta1_rad = theta1*math.pi/180
theta2_rad = math.asin(n1*math.sin(theta1_rad)/n2)
theta2 = theta2_rad*180/math.pi

print(theta2)

When I plug in $ python3 snell.py 58 1 1.52 into the terminal, I expect the output of 33.912513998258994, but the actual output I get is 33.91251399825899.

Kevin
  • 16,549
  • 8
  • 60
  • 74
soojung k
  • 13
  • 2

2 Answers2

0

As stated in this answer in a similar question, try using numpy.float128. Or you could go for Decimal datatype as stated in the accepted answer.

0
import math
import sys

theta = float(sys.argv[1])
n1 = float(sys.argv[2])
n2 = float(sys.argv[3])

theta1_rad = round(theta*math.pi/180,15)
theta2_rad = round(math.asin(n1*math.sin(theta1_rad)/n2),15)
theta2 = round(theta2_rad*180/math.pi,15)

print(theta2)

Try this hopefully it will work according to your expectation!

Vashdev Heerani
  • 660
  • 7
  • 21