// // sr -- Special Relativity (time dilation) calculator for low speeds. // // 30-Apr-2006 tvb www.LeapSecond.com/tools // #include #include #include #include #define INCH_TO_METER(x) ( (x) * 2.54 / 100.0 ) #define FOOT_TO_METER(x) ( INCH_TO_METER((x) * 12) ) #define MILE_TO_METER(x) ( FOOT_TO_METER((x) * 5280) ) #define KILO_TO_METER(x) ( (x) * 1000 ) #define kt_to_kmph(v) ( (v) * 1.852 ) #define kmph_to_kt(v) ( (v) / 1.852 ) #define mach_to_ms(v) ( (v) * 340.3 ) #define ms_to_mach(v) ( (v) / 340.3 ) #define ALL_UNITS "mph, kt, fps, kph, m/s" void main (int argc, char *argv[]) { double c = 299792458.0; double speed, v, df; char *units; int debug; if (argc < 3) { fprintf(stderr, "Usage: %s [speed] [units]\n", argv[0]); fprintf(stderr, "(speed units are one of: %s)\n", ALL_UNITS); exit(1); } debug = (argc > 3); speed = atof(argv[1]); units = argv[2]; if (strcmp(units, "mph") == 0) { v = MILE_TO_METER(speed) / 3600; } else if (strcmp(units, "fps") == 0) { v = FOOT_TO_METER(speed); } else if (strcmp(units, "kph") == 0) { v = KILO_TO_METER(speed) / 3600; } else if (strcmp(units, "kt") == 0) { v = KILO_TO_METER(kt_to_kmph(speed)) / 3600; } else if (strcmp(units, "m/s") == 0) { v = speed; } else { fprintf(stderr, "Unit %s unknown. Valid units: %s\n", units, ALL_UNITS); exit(1); } if (v < 0) { fprintf(stderr, "Velocity too low for this tool.\n"); exit(1); } printf("%16.6lf mph (miles per hour)\n", 1/MILE_TO_METER(1/v) * 3600); printf("%16.6lf kph (kilometers/hour)\n", 1/KILO_TO_METER(1/v) * 3600); printf("%16.6lf fps (feet per second)\n", 1/FOOT_TO_METER(1/v)); printf("%16.6lf m/s (meters per second)\n", v); { double kmph = 1/KILO_TO_METER(1/v) * 3600; printf("%16.6lf kt (knots)\n", kmph_to_kt(kmph)); } printf("%16.6lf ma (mach)\n", ms_to_mach(v)); printf("%16.6lf m/s c\n", c); printf("%16.6le v/c\n", v/c); if (v > 5000 && !debug) { fprintf(stderr, "Velocity too high for this tool.\n"); exit(1); } df = -0.5 * (v*v) / (c*c); printf("%16.6le -1/2*v^2/c^2\n", df); printf("%16.6lf ps/hour\n", df * 1e12 * 3600); printf("%16.6lf ns/day\n", df * 1e9 * 86400); if (debug) { double v2 = v * v; double c2 = c * c; double x = 1 - (v2/c2); printf("\n"); printf("%.16le v2/c2\n", v2/c2 ); printf("%.16le 1-v2/c2\n", 1-v2/c2 ); printf("0x%.8lx,%.8lx 1-v2/c2 (hex)\n", ((long *)&x)[1], ((long *)&x)[0]); printf("%.16le sqrt(1-v2/c2)\n", sqrt(1-v2/c2) ); printf("%.16le 1/sqrt(1-v2/c2)\n", 1/sqrt(1-v2/c2) ); printf("%.16le 1/sqrt(1-v2/c2)-1\n", 1/sqrt(1-v2/c2)-1); printf("%.16le 1-sqrt(1-v2/c2)\n", 1-sqrt(1-v2/c2) ); printf("%.16le 0.5*v2/c2\n", 0.5*v2/c2); } }