Any programmers willing to help me solve a triangle?

Asked by gorillapaws (20653) January 20th, 2015

My trig is a little rusty, and I’m trying to solve a triangle using the side-angle-side method using the law of cosines.

My code is in Swift but is very simple and should be obvious to anyone with any programming experience:

var sideA = 13.128
var sideB = 40.224
var angleC = 91.733

var sideC = sqrt( ((sideA * sideA) + (sideB * sideB)) – (((2 * sideA) * sideB) * cos( angleC )) )

println( sideC )

I’m just using some random variables being generated elsewhere in my code for this example. With these inputs I get sideC = 51.436. If I plug in these same inputs to an online triangle solver I get sideC = 42.688. I get the same answer with other online calculators, so the problem is definitely in my code. I’ve even tried breaking the math into multiple steps to clean it up a bit, and I always get the same, incorrect result. What the heck am I doing wrong? It’s driving me crazy.

Observing members: 0 Composing members: 0

Are you sure whether your language is using degrees vs. radians for the angle?

Mariah (24420)

Objective C uses radians. I would assume that Swift uses the same.

johnpowell (14734)

Thanks guys… I’m an idiot.

gorillapaws (20653)

Happens to the best of us!

Mariah (24420)

or