0
我想在C中做一个函数,它使用余弦定律返回与给定角度相反的三角形边的长度。编写一个余弦定律计算
现在我得到了在Excel中工作的公式,它给出了正确的结果。然而,当我在C中尝试它时,我得到了错误的结果,并且我无法弄清楚为什么。
对于测试,我有sideA为21.1,sideB为19,它们之间的角度为40度。现在答案应该是14.9,就像我在Excel中获得的一样。然而在C中我得到了23.735。请有人帮我找出我出错的地方
// Find the length of a side of a triangle that is oppisit a given angle using the Law Of Cosine
// for example using an triangle that is 21.1cm on one side, 19 cm on the other and an angle of 40 degreese inbetween then....
// in excel it worked and the formuler was =SQRT(POWER(23.1;2)+POWER(19;2)-2*(23.1)*(19)*COS(40*(3.14159/180))) = 14.9 cm
float my_Trig_LawOfCos_OppSideLength(float centerAngle, float sideA, float sideB)
{
float sideLengthPow2= (pow(sideA,2) + pow(sideB,2))) - ((2*sideA*sideB)*cos(centerAngle*(3.14159/180));
float sideLength = sqrt(sideLengthPow2);
return sideLength;
}
你的括号不平衡(当我复制粘贴你的示例时,我的编译器只是抱怨)。所以我不相信这是你的实际代码。 – StoryTeller
[当我修复*问题时,我得到了您期望的结果](http://ideone.com/Gd4oqO)。 – StoryTeller
注意,如果代码使用'float',不妨使用'float'函数:'sqrtf()','cosf()'等 – chux