對編程完全陌生,我只有 excel 經驗,所以任何幫助表示贊賞。運算式計算給定您的太陽的高度和方位,一天中latitude的小時t和一年中的一天day。
高度計算作業正常,但方位計算在輸出中有零星的無意義負值。
這些存盤在各自陣列中的值稍后將用于其他計算。
這是我的代碼塊:
#include <iostream>
#include <cmath>
using namespace std;
float degrees(float radians) { // gets degrees
return (radians * (180 / 3.14));
}
float radians(float degrees) { //gets radians
return (degrees * (3.14 / 180));
}
int main()
{
float latitude = 54;
// cout << "Enter latitude (in degrees): ";
// cin >> latitude;
float const sin_thi = sin(radians(latitude));
float const cos_thi = cos(radians(latitude));
// cout << "sin_thi: " << sin_thi << endl;
// cout << "cos_thi: " << cos_thi << endl;
float cos_omega[24];
// required for later calculation
for (int t = 0; t <= 23; t )
{
cos_omega[t] = cos(radians((15 * (t - 12))));
// cout << cos_omega[t] << endl;
}
// gathering sun altitude and bearing data, storing into heap multidimentional arrays "alt_sun[][]" and "bearing_sun[][]".
auto alt_sun = new int [365][24];
auto bearing_sun = new int [365][24];
for (int day = 0; day <= 364; day )
{
for (int t = 0; t <= 23; t )
{
alt_sun[day][t] = degrees(asin(((0.4 * cos(radians(0.99 * (day - 173)))) * sin_thi) ((sin(((0.4 * cos(radians(0.99 * (day - 173)))) - 1.57)))) * (cos_omega[t] * cos_thi)));
if (cos_omega[t] > 0)
bearing_sun[day][t] = 360 - degrees(acos((((0.4 * cos(radians(0.99 * (day - 173)))) * cos_thi) - ((sin(((0.4 * cos(radians(0.99 * (day - 173)))) - 1.57))) * (cos_omega[t] * sin_thi))) / (cos(radians(alt_sun[day][t])))));
else
bearing_sun[day][t] = degrees(acos((((0.4 * cos(radians(0.99 * (day - 173)))) * cos_thi) - ((sin(((0.4 * cos(radians(0.99 * (day - 173)))) - 1.57))) * (cos_omega[t] * sin_thi))) / (cos(radians(alt_sun[day][t])))));
}
}
for (int day = 0; day <= 364; day ) // to test calculation
{
cout << "Day " << day << ": " << bearing_sun[day][12] << endl;
}
}
我在做什么從根本上是錯誤的嗎?
列印軸承值的預期結果范圍約為 350 - 360(大致是由于我之前進行的 excel 計算的四舍五入)。例如,第一個值應該是大約 355。
代碼應該在沒有用戶輸入的情況下運行,問題現在應該是明確的。
else我的第一個想法是只有在回圈中使用該陳述句時才會發生不良結果。for但是我有限的業余測驗,比如在回圈和陳述句之外運行單個運算式if,產生了相同的結果。在我看來,僅將運算式本身或陣列作為可能的原因。
一些輸出到控制臺的示例截圖,所有正值都是正確的,所有負值都是錯誤的
謝謝
uj5u.com熱心網友回復:
When I run your program with UndefinedBehaviorSanitizer active, I get a run-time error message in line 48 and 51 that you are converting a NaN to an integer, which invokes undefined behavior. This means that your calculatings are producing a NaN.
When I run your program line by line in a debugger, I notice that when line 51, which is
bearing_sun[day][t] = degrees(acos((((0.4 * cos(radians(0.99 * (day - 173)))) * cos_thi) - ((sin(((0.4 * cos(radians(0.99 * (day - 173)))) - 1.57))) * (cos_omega[t] * sin_thi))) / (cos(radians(alt_sun[day][t])))));
is executed for the first time, it writes the value
-2147483648
into bearing_sun[0][0], which does not seem to be the intended value.
Since this line is a very long line, I modified it to several lines, so that I can examine the intermediate results more easily:
else
{
auto intermediate_result_1 = (0.4 * cos(radians(0.99 * (day - 173)))) * cos_thi;
auto intermediate_result_2 = sin(((0.4 * cos(radians(0.99 * (day - 173)))) - 1.57));
auto intermediate_result_3 = cos_omega[t] * sin_thi;
auto intermediate_result_4 = cos(radians(alt_sun[day][t]));
auto intermediate_result_5 = intermediate_result_2 * intermediate_result_3;
auto intermediate_result_6 = intermediate_result_1 - intermediate_result_5;
auto intermediate_result_7 = intermediate_result_6 / intermediate_result_4;
auto intermediate_result_8 = acos( intermediate_result_7 );
auto intermediate_result_9 = degrees( intermediate_result_8 );
bearing_sun[day][t] = intermediate_result_9;
}
This code is much easier to inspect when running the code line by line in a debugger, because you can see the intermediate results.
When I reach the line
auto intermediate_result_8 = acos( intermediate_result_7 );
I can see in the debugger that intermediate_result_7 has the value -1.0009622545441585. Since this is invalid input for the function acos, it will correctly return NaN, so that the final result also is NaN, which is converted to an int, which invokes undefined behavior. This explains the "nonsense negative values" that you mentioned in the question.
Therefore, there appears to be something wrong with the formula that you are using. In order to find the problem, you will have to run the program line by line in a debugger, checking the values of all of the intermediate results.
uj5u.com熱心網友回復:
A rouding error that only affected the calculation when the value was close to the limits of what acos() can accept (between -1 and 1), returning a NaN, invoking undefined behaviour once converted to an int.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/512359.html
