0

I put together a program in Python to use the law of cosines to figure out missing sides or angles in a triangle. The portion of the program that calculates the missing angle for a side-side-side triangle goes like so:

usedegrees = True
opptheta = 4
sideb = 8
sidec = 9
asqrd = opptheta**2
bsqrd = sideb**2
csqrd = sidec**2
costheta = (bsqrd+csqrd-asqrd)/(2.0*sideb*sidec)
if usedegrees:
  print degrees(acos(costheta))
else: 
  print acos(costheta)

It was calculating incorrectly when I assigned the costheta with (bsqrd+csqrd-asqrd)/(2*sideb*sidec) (The costheta variable output 0).

So my question is, is why did the change of the 2 to 2.0 in the law of cosines formula fix the bug and make costheta have an output the program could work with to perform the calculation?

salad_bar_breath
  • 271
  • 4
  • 18
  • 1
    is this python 2? because if it is, if both the numerator and denominator are `int`s then it will automatically do an integer divide ie `5/2 == 2` instead of `5.0/2 == 2.5` – R Nar Nov 20 '15 at 21:14
  • 1
    This might help: http://stackoverflow.com/q/21768286/535275 – Scott Hunter Nov 20 '15 at 21:14
  • 1
    Python defaults to using integers unless otherwise "told" to use floating point numbers. The change from integer `2` to floating point `2.0` makes it switch. – Alderin Nov 20 '15 at 21:15
  • Thanks everyone for the explanation, and my apologies for the duplicate question – salad_bar_breath Nov 20 '15 at 21:16

0 Answers0