首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >三角方程只适用于特定输入(5)不适用于其他输入

三角方程只适用于特定输入(5)不适用于其他输入
EN

Stack Overflow用户
提问于 2022-08-17 11:21:50
回答 1查看 49关注 0票数 0

我试着用三角形的长度来编写计算角度的代码。公式为cos(a)=b^2+c^2-a^2/2bc。(三角形在这里)

代码语言:javascript
复制
angle1 = acosf((powf(length2,2) + powf(length3,2) - powf(length1,2)) / 2 * length2 * length3)* 180 / 3.14153;
angle2 = acosf((powf(length1,2) + powf(length3,2) - powf(length2,2)) / 2 * length1 * length3)* 180 / 3.14153;
angle3 = 180 - (angle2 + angle1);

一切都是浮动的。当输入5-4-3输入时,结果如下。

代码语言:javascript
复制
angle one is 90.0018
angle two is nan
angle three is nan

改变顺序并不重要,只给出5的输出。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-17 11:48:25

你所做的是:

代码语言:javascript
复制
angle1 = acosf((powf(length2,2) + powf(length3,2) - powf(length1,2)) / 2 * length2 * length3)* 180 / 3.14153;

你应该做的是:

代码语言:javascript
复制
angle1 = acosf((powf(length2,2) + powf(length3,2) - powf(length1,2)) / (2 * length2 * length3))* 180 / 3.14153;

解释:问题是由以下公式引起的,这个公式实际上写得很糟糕:

代码语言:javascript
复制
cos(a)=b^2+c^2-a^2/2bc
// This, obviously, is wrong because
//   you need to group the firt three terms together.
// Next to that, everybody understands that the last "b" and "c" are divisors, 
//   yet it would be better to write it as:

cos(a)=(b^2+c^2-a^2)/(2bc)

我在代码中添加的方括号类似于用/2bc替换/(2bc)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73387598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档