diff --git a/TimeDilation/TimeDilation.cpp b/TimeDilation/TimeDilation.cpp index 8a528ee809899eaac1d8f574147e7744b215a554..db228fcc7ea072fda0a3275938e27eb1ded92060 100644 --- a/TimeDilation/TimeDilation.cpp +++ b/TimeDilation/TimeDilation.cpp @@ -1,55 +1,69 @@ -#include <iostream> -#include <math.h> -#include <iostream> -#include <iomanip> -#include <string> - -using namespace std; - -// Constants -const double MPH_TO_M_DIV_S = 0.44704; -const double C = 299791958; // speed of light (m/s) -const double C_SQUARED = C * C; - -const int DAY_PER_WEEK = 5; // Based on 5-day work-week -const int WEEKS_PER_YEAR = 16 * 2; // Based on fall & spring semesters -const int DRIVE_DURATION = 60; // length of drive one way in minutes -const int MINUTES_TO_SECONDS = 60; - -const std::string DIVIDER(32, '-'); - -int main(int argc, char** argv) -{ - double time = 2 * DAY_PER_WEEK * WEEKS_PER_YEAR - * DRIVE_DURATION * MINUTES_TO_SECONDS; - - cout.setf(ios::fixed); - setprecision(32); - - for(double speed = 10; speed <= 60; speed += 5) - { - double speed_in_ms = speed * MPH_TO_M_DIV_S; - - double gamma = 1.0 / sqrt(1 - pow((speed_in_ms / C), 2)); - double gamma_inv = (C_SQUARED - (0.5 * pow(speed_in_ms, 2.0))) / C_SQUARED; - - double proper_time = time / gamma_inv; - - double diff = time - proper_time; - - cout << "Mph " << right << setw(19) << speed << "\n"; - cout << "m/s " << right << setw(19) << speed_in_ms << "\n"; - - cout << DIVIDER << "\n"; - cout << "Gamma " << right << setw(19) << gamma << "\n"; - cout << "Gamma inv " << right << setw(19) << gamma_inv << "\n"; - - cout << DIVIDER << "\n"; - cout << "Time " << right << setw(19) << time << "\n"; - cout << "Time(proper) " << right << setw(19) << proper_time << "\n"; - cout << "Difference " << right << setw(19) << diff << "\n" - << "\n"; - } - - return 0; -} +#include <cmath> +#include <iostream> +#include <iomanip> +#include <string> + +using namespace std; + +// Constants +const double MPH_TO_M_DIV_S = 0.44704; +const double C = 299791958; // speed of light (m/s) +const double C_SQUARED = C * C; + +const int DAY_PER_WEEK = 5; // Based on 5-day work-week +const int WEEKS_PER_YEAR = 16 * 2; // Based on fall & spring semesters +const int DRIVE_DURATION = 60; // length of drive one way in minutes +const int MINUTES_TO_SECONDS = 60; + +const std::string DIVIDER(32, '-'); +//------------------------------------------------------------------------------ + +/** + * Compute gamma and inverse gamma + * + * @tparam T precision (float or double). + * + * @param speed travel speed in MPH + */ +template<typename T> +std::pair<T, T> compute_gamma(const T speed) +{ + const T gamma = 1.0 / sqrt(1 - pow((speed / C), 2)); + const T gamma_inv = (C_SQUARED - (0.5 * pow(speed, 2.0))) / C_SQUARED; + + return {gamma, gamma_inv}; +} + +//------------------------------------------------------------------------------ +int main(int argc, char** argv) +{ + cout.setf(ios::fixed); + setprecision(32); + + constexpr double time = 2 * DAY_PER_WEEK * WEEKS_PER_YEAR + * DRIVE_DURATION * MINUTES_TO_SECONDS; + + for (double speed = 10; speed <= 60; speed += 5) + { + const double speed_in_ms = speed * MPH_TO_M_DIV_S; + const auto& [gamma, gamma_inv] = compute_gamma<double>(speed_in_ms); + + const double proper_time = time / gamma_inv; + const double diff = time - proper_time; + + cout << "Mph " << right << setw(19) << speed << "\n"; + cout << "m/s " << right << setw(19) << speed_in_ms << "\n"; + + cout << DIVIDER << "\n"; + cout << "Gamma " << right << setw(19) << gamma << "\n"; + cout << "Gamma inv " << right << setw(19) << gamma_inv << "\n"; + + cout << DIVIDER << "\n"; + cout << "Time " << right << setw(19) << time << "\n"; + cout << "Time(proper) " << right << setw(19) << proper_time << "\n"; + cout << "Difference " << right << setw(19) << diff << "\n" + << "\n"; + } + + return 0; +} diff --git a/TimeDilation/makefile b/TimeDilation/makefile index eb60200ea2362f718d60b09e441050646c79fdf3..35ad648043fca027ec287a253643726f38d297c9 100644 --- a/TimeDilation/makefile +++ b/TimeDilation/makefile @@ -2,7 +2,7 @@ MAINPROG=timeDilation SOURCES:=$(wildcard *.cpp) OBJECTS=$(SOURCES:.cpp=.o) -FLAGS=-std=c++17 -Wall -fsanitize=address,leak -fuse-ld=gold +FLAGS=-std=c++17 -Wall -Wextra -Wpedantic -Weffc++ -fsanitize=address,leak -fuse-ld=gold all: $(SOURCES) $(MAINPROG) diff --git a/TimeDilation/timeDilationRevised.py b/TimeDilation/timeDilationRevised.py index ed010eb0cfadc49e655a396078835f10d3151aa5..7edb864e507d2ab8319076a353ac16bccf808695 100755 --- a/TimeDilation/timeDilationRevised.py +++ b/TimeDilation/timeDilationRevised.py @@ -14,7 +14,8 @@ def compute_gamma(speed): """ Compute Gamma and Inverse Gamma - :param: speed speed in m/s + Args: + speed: travel speed in m/s """ gamma = Decimal(1.0) / Decimal.sqrt(1 - (speed / C) ** Decimal(2)) @@ -44,14 +45,14 @@ def main(): diff = driving_time - proptime print("-" * (14 + getcontext().prec + 2)) - print("MPH ", speed) - print("m/s ", speed_in_ms) - print("Gamma ", gamma) - print("Gamma inv ", gamma_inv) - print("Time ", driving_time) - print("Time (proper) ", proptime) - print("Time (proper) ", driving_time * gamma) - print("Difference ", diff) + print(f"MPH {speed}") + print(f"m/s {speed_in_ms}") + print(f"Gamma {gamma}") + print(f"Gamma inv {gamma_inv}") + print(f"Time {driving_time}") + print(f"Time (proper) {proptime}") + print(f"Time (proper) {driving_time * gamma}") + print(f"Difference {diff}") if __name__ == "__main__":