Sign in to follow this  

Stupid Calculation Bug

This topic is 4108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to find the 3rd side of a triangle given 2 sides and an angle. However, I think I'm getting the wrong values. I don't see where the problem is located. Equation: c^2 = a^2 + b^2 - 2abcos(@) float r1, r2, ang, T1, T2, d; r1 = 3.0f; r2 = 5.0f; ang = 1.0f; T1 = (r1 * r1) + (r2 * r2); T2 = (2.0f * r1 * r2); d = sqrtf( T1 - (T2*cosf(ang)) ); I get d = 4.22. This value seems wrong to me. I think it should be around 2. What am I doing wrong?

Share this post


Link to post
Share on other sites
4.22 sounds correct. Imagine that this was a right triangle and you were trying to find the hypotenuse. Then the value of the third side would be sqrt(9 + 25) = sqrt(34) = ~5.83. Since the angle is a little smaller than 60 degrees (1 radian), we'd expect the actual value to be somewhat smaller than 5.83, which 4.22 certainly is.

There is a theorem called the triangle theorem which algebraically and graphically gives the range in which the length of a third side of a triangle may fall, given the other two sides. If the angle between sides a and b is small (~0), the length of c is close to |a - b|. If the angle is large (~pi), the length is close to |a + b|.

The angle between the two sides would have to be small (a couple of degrees or so) before it would approach 2, and it would never be exactly 2.

[edit]: I just thought of something; are you thinking that the "angle" parameter is in degrees? If so, that probably explains the confusion. A value of 1 degree would certainly produce a result close to 2, but the value of "1.0f" you're supplying to cosf() is actually specifying one radian, which is just about 57 degrees.

hope that helps,

Share this post


Link to post
Share on other sites

This topic is 4108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this