第五部份:程式列表
#include <iostream> #include <string> #include <math.h> using namespace std; const double g = 9.80665; // 地球上標準重力加速度的值 const double c = 299792458; // 光速 = 每秒 299,792,458 公尺 const double _LightSecond = c; // 1 光秒 大約等於 30 萬公里 const double _LightMinute = _LightSecond * 60; // 1 光分 大約等於 1800 萬公里 = 0.18 億公里 const double _AU = 149597870691; // 當前被接受的天文單位是 149,597,870,691±30 公尺 (大約等於 1.5 億公里) const double _LightHour = _LightMinute * 60; // 1 光時 大約等於 10.8 億公里 const double _LightDay = _LightHour * 24; // 1 光日 大約等於 259.2 億公里 const double _LightYear = _LightDay * 365.25; // 1 光年 大約等於 9.47 兆公里 const double _Minute = 60; const double _Hour = 60 * _Minute; const double _Day = 24 * _Hour; const double _Week = 7 * _Day; const double _Year = 365.2425 * _Day; double Lorentz_Transformations_c(double v) { return sqrt(1-v*v); } double Relativity_Addition_c(double v1, double v2) { return (v1+v2)/(1+v1*v2); } class ResultType { public: double Velocity; // 單位時間結束時的相對速度 (單位:光秒/秒) double Distance; // 經過的距離 (單位:光秒) double ElapsedTime; // 靜止觀察者所經過的時間 (單位:秒) ResultType() : Velocity(0), Distance(0), ElapsedTime(0) {} }; void Update(ResultType &R, double &Acceleration, unsigned int &Seconds, double &New_Special_Velocity) { double Old_Velocity = R.Velocity; R.Velocity = Relativity_Addition_c(R.Velocity, Acceleration); double Old_Special_Velocity = New_Special_Velocity; New_Special_Velocity = R.Velocity / Lorentz_Transformations_c(R.Velocity); double Increasing_Distance = (Old_Special_Velocity + New_Special_Velocity) * Seconds / 2; R.Distance += Increasing_Distance; R.ElapsedTime += Increasing_Distance * 2 / (Old_Velocity + R.Velocity); } ResultType Calculate_Nonrecursively(unsigned int Time, unsigned int Seconds) // 傳回 Time 秒之後的速度(單位:光秒/秒)和距離(單位:光秒) (以 Seconds 秒為計算的時間間隔) { ResultType Results; double AccelerationPerUnit = (g * Seconds) / _LightSecond; // 每單位時間(Seconds)可增加的速度 (單位:光秒/秒) // Special_Velocity // = 增加的靜止空間的距離 / 太空船上的時間 // = (太空船上所看到的增加距離 / 洛倫茲收縮) / 太空船上的時間 // = (太空船上所看到的增加距離 / 太空船上的時間) / 洛倫茲收縮 // = 相對速度 / 洛倫茲收縮 (單位:光秒/秒) double New_Special_Velocity = 0; for(unsigned int t=0; t<Time; t+=Seconds) Update(Results, AccelerationPerUnit, Seconds, New_Special_Velocity); return Results; } unsigned int TimeBound = 90; ResultType Calculate_Recursively(unsigned int Time) // 傳回 Time 秒之後的速度(單位:光秒/秒)和距離(單位:光秒) (以 Time/TimeBound 秒為計算的時間間隔) { // Time 小於 TimeBound 則直接做掉 if(Time <= TimeBound) { return Calculate_Nonrecursively(Time, 1); } unsigned int Seconds = Time / TimeBound; // Seconds 是單位時間 unsigned int Loop = Time / Seconds - 1; // Loop 是迴圈變數 // Loop 的值通常是等於 TimeBound - 1 // 可是當 Time 和 TimeBound 很接近時則可能變得很大 // 最大可能是 Time - 1 ResultType Results = Calculate_Recursively(Seconds); // 用遞迴算出 Seconds 秒之後的速度 (單位:光秒/秒) double AccelerationPerUnit = Results.Velocity; // 每單位時間(Seconds)可增加的速度 (單位:光秒/秒) double New_Special_Velocity = Results.Velocity / Lorentz_Transformations_c(Results.Velocity); // 算出 Seconds 秒之後的 New_Special_Velocity // 運用迴圈做累加到時間 Seconds * Loop for(unsigned int t=1; t<Loop; t++) Update(Results, AccelerationPerUnit, Seconds, New_Special_Velocity); unsigned int LastSeconds = Time - Seconds * Loop; // LastSeconds = Seconds ~ 2*Seconds-1 ResultType R = Calculate_Recursively(LastSeconds); // 用遞迴算出 LastSeconds 秒之後的速度 (單位:光秒/秒) Update(Results, R.Velocity, LastSeconds, New_Special_Velocity); return Results; } main() { unsigned int Time = 448712301; ResultType R = Calculate_Recursively(Time); printf("第 %9.6f 年 (= %d秒) 末:\n", Time / _Year, Time); printf(" 速度 = %.17f c\n", R.Velocity); printf("不考慮相對論的距離 = %.2f 光年\n", g * Time * Time / 2 / _LightYear); printf(" 距離 = %.2f 光年\n", R.Distance * _LightSecond / _LightYear); printf(" 地球時間 = %.2f年\n", R.ElapsedTime / _Year); }
其它相關的連結