我正在撰寫一個簡單的程式來計算月相;我使用 Jean Meeus 的《天文演算法》第 2 版書中介紹的演算法。
在頁。350(第 49 章)他寫道:
[...] 計算以下角度,這些角度以度數表示,可以減少到 0-360 度的區間,如有必要,在繼續之前減少到弧度
所以轉換應該是這樣的:
- 將角度減小到 0-360 范圍內
- 角度從度到弧度的轉換
接下來,在第 353 頁,他給出了一系列例子:
M = -8234.2625 = 45.7375
M1 = -108984.6278 = 95.3722
但我不明白轉換是如何進行的。我已經嘗試了以下計算(類似帕斯卡)的值M
// 1. reduction of the angle to the range 0-360
deg := -8234.2625;
reducedDeg := Abs(deg mod 360) // 314.2625
// 2. conversion of the angle from degrees to radians
convertedDeg := reducedDeg * Pi / 180 // 5.4849
所以轉換的結果是 5.4849,而預期的結果應該是 45.7375。第二個例子也是一樣的M1:
// 1. reduction of the angle to the range 0-360
deg: = -108984.6278;
reducedDeg := Abs(deg mod 360); // 264.6278;
// 2. conversion of the angle from degrees to radians
convertedDeg := reducedDeg * Pi / 180; // 4,6186 should be 95.3722
可能是什么問題呢?
為了清楚起見:
Abs(val mod x)在這些例子中是除以的正(絕對)余val數x;是以下序列的縮寫:
// reduction of the angle to the range 0-360 => Abs(-8234.2625 mod 360)
val := -8234.2625;
val := val / 360; // ?22.872951389
If (val < 0) // in the range 0-360 there are only positive numbers
begin
val := val * -1; // 22.872951389
end
val := val - Int(val); // 0.872951389
val := val * 360; // 314.2625
uj5u.com熱心網友回復:
您的“ mod”演算法(將角度減小到 [0.0°,360°)范圍)是錯誤的,特別是:
If (val < 0) // in the range 0-360 there are only positive numbers begin val := val * -1; // 22.872951389 end
這將鏡像,“翻轉”沿 x 軸的角度。你不想那樣做。你的意思是
val := val ord(val < 0) * 360; { conditionally add a complete turn }
如果您的編譯器符合 ISO 標準 10206,“Extended Pascal”,則可以避免所有這些麻煩。然后,您可以使用內置資料型別解決此子任務complex:
program degreeToRadianDemo(output);
const
{ This is meant to familiarize you with the functions. }
pi = 2 * arg(cmplx(0.0, maxReal));
{ 1° ? π / 180 rad }
degreeInRadian = pi / 180;
var
degree, radian: real;
begin
degree := -108984.6278;
{ `arg` returns principal argument, i.?e. `real` in (?π, π] }
radian := arg(polar(maxReal, degree * degreeInRadian));
{ Add one turn (2π) if we’re in the (?π, 0.0) range. }
radian := radian ord(radian < 0.0) * 2 * pi;
writeLn( radian:8:4);
{ Convert back to degrees. }
writeLn(radian / degreeInRadian:8:4);
end.
這適用于 GPC(GNU Pascal 編譯器)。FPC(FreePascal 編譯器)提供 a ucomplex unit,但它只支持矩形complex(即沒有argand polar)。【FPC 3.2.0版】
此外——我知道這對你來說是一個練習——但不要編程已經為你編程的東西。Delphi 和 FPC 交付一個math單元。這unit具有DegToRad和RadToDeg功能。還有一個FMod函式 -數字運算mod符real- 和mod運算子多載,因為使用DegToRad/RadToDeg函式您仍然需要消除完整的轉彎。看看它FMod的實作。
由于似乎對mod運算子有些混淆,參考 ISO 標準 7185(“標準帕斯卡”),第 48 頁:
形式的一個項如果為零或負數則為
i mod j錯誤;j否則, 的值i mod j應(i???(k?×?j))為整數的值,k使得0 ≤ i mod j < j.
因此,mod運算子的結果保證是非負的。但不幸的是,并非所有編譯器都遵守 ISO 標準。例如,在 FPC(FreePascal 編譯器)中,只有設定了才會回傳正確的結果{$modeSwitch isoMod }。不過請放心,Delphi 和 GPC(GNU Pascal 編譯器)確實可以正常作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520455.html
標籤:算法三角学帕斯卡天文学
