簡體   English   中英

三角方程僅適用於特定輸入 (5) 不適用於其他輸入

[英]Trigonometric Equation only works with specific input ( 5 ) doesn't work with other inputs

我嘗試根據三角形的長度為計算角度編寫代碼。 公式為 cos(a)=b^2+c^2-a^2/2bc。 (三角形在這里)

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 輸入結果時。

angle one is 90.0018
angle two is nan
angle three is nan

更改順序無關緊要,只給 output 5。

你正在做:

angle1 = acosf((powf(length2,2) + powf(length3,2) - powf(length1,2)) / 2 * length2 * length3)* 180 / 3.14153;

你應該這樣做:

angle1 = acosf((powf(length2,2) + powf(length3,2) - powf(length1,2)) / (2 * length2 * length3))* 180 / 3.14153;

解釋:這個問題是由下面的公式引起的,實際上它寫得很糟糕:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM