1const C: f64 = 299_792_458.0; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13pub enum WarpScale {
14 Tos,
16 Tng,
18}
19
20pub fn warp_velocity(w: f64, scale: WarpScale) -> f64 {
27 if w < 0.0 {
28 return 0.0;
29 }
30 match scale {
31 WarpScale::Tos => w * w * w * C,
32 WarpScale::Tng => {
33 if w >= 10.0 {
34 return f64::INFINITY;
35 }
36 if w <= 9.0 {
37 return f64::powf(w, 10.0 / 3.0) * C;
38 }
39 let epsilon = 1e-6;
42 let alpha = 0.1; let tan_arg = std::f64::consts::PI * (w - 9.0) / (2.0 * (1.0 + epsilon));
44 let base = f64::powf(w, 10.0 / 3.0) * C;
45 let saturation = 1.0 + alpha * tan_arg.tan();
46 base * saturation
47 }
48 }
49}
50
51pub fn warp_factor_c(w: f64, scale: WarpScale) -> f64 {
53 warp_velocity(w, scale) / C
54}
55
56pub fn cochrane_units(w: f64, scale: WarpScale) -> f64 {
59 warp_factor_c(w, scale)
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn tos_warp1_is_c() {
68 let v = warp_velocity(1.0, WarpScale::Tos);
69 assert!((v - C).abs() < 1.0, "warp 1 should be c");
70 }
71
72 #[test]
73 fn tos_warp2_is_8c() {
74 let v = warp_velocity(2.0, WarpScale::Tos);
75 assert!((v - 8.0 * C).abs() < 1.0, "warp 2 should be 8c");
76 }
77
78 #[test]
79 fn tng_warp1_is_c() {
80 let v = warp_velocity(1.0, WarpScale::Tng);
81 assert!((v - C).abs() < 1.0, "warp 1 should be c");
82 }
83
84 #[test]
85 fn tng_warp9() {
86 let v = warp_velocity(9.0, WarpScale::Tng);
88 let expected = f64::powf(9.0, 10.0 / 3.0) * C;
89 assert!((v - expected).abs() < 1.0);
90 }
91
92 #[test]
93 fn tng_warp10_is_infinite() {
94 let v = warp_velocity(10.0, WarpScale::Tng);
95 assert!(v.is_infinite(), "warp 10 should be infinite");
96 }
97
98 #[test]
99 fn tng_warp_9_99_finite() {
100 let v = warp_velocity(9.99, WarpScale::Tng);
102 assert!(v.is_finite(), "warp 9.99 should be finite");
103 assert!(v > 0.0);
104 }
105
106 #[test]
107 fn tng_warp_9_99_no_nan() {
108 let v = warp_velocity(9.999, WarpScale::Tng);
110 assert!(!v.is_nan(), "warp 9.999 must not be NaN");
111 assert!(v.is_finite(), "warp 9.999 should be finite");
112 }
113
114 #[test]
115 fn warp0_is_zero() {
116 assert_eq!(warp_velocity(0.0, WarpScale::Tos), 0.0);
117 assert_eq!(warp_velocity(0.0, WarpScale::Tng), 0.0);
118 }
119
120 #[test]
121 fn negative_warp_is_zero() {
122 assert_eq!(warp_velocity(-1.0, WarpScale::Tos), 0.0);
123 assert_eq!(warp_velocity(-1.0, WarpScale::Tng), 0.0);
124 }
125
126 #[test]
127 fn cochrane_at_warp1() {
128 let c = cochrane_units(1.0, WarpScale::Tng);
129 assert!((c - 1.0).abs() < 1e-10, "C should be 1.0 at warp 1");
130 }
131
132 #[test]
133 fn tng_soft_saturation_increases() {
134 let v9 = warp_velocity(9.0, WarpScale::Tng);
136 let v95 = warp_velocity(9.5, WarpScale::Tng);
137 assert!(v95 > v9, "warp 9.5 should be faster than warp 9");
138 }
139}