zmatrix 0.4.2

Compile-time dimensional analysis & physics-aware linear algebra for aerospace and robotics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
use super::*;
use std::f64::consts::{PI, TAU};
use std::ops::{Add, Div, Mul, Neg, Sub};

impl Angular {
    pub fn from_rad(rad: f64) -> Self {
        Self {
            default_type: AngularType::Rad,
            v: rad,
        }
    }

    pub fn from_deg(deg: f64) -> Self {
        Self {
            default_type: AngularType::Deg,
            v: deg,
        }
    }

    pub fn as_rad(&self) -> f64 {
        return match self.default_type {
            AngularType::Rad => self.v,
            AngularType::Deg => self.v * PI / 180.0,
        };
    }

    pub fn as_deg(&self) -> f64 {
        return match self.default_type {
            AngularType::Rad => self.v * 180.0 / PI,
            AngularType::Deg => self.v,
        };
    }

    pub fn sin(&self) -> f64 {
        self.as_rad().sin()
    }

    pub fn tan(&self) -> f64 {
        self.as_rad().tan()
    }

    pub fn cos(&self) -> f64 {
        self.as_rad().cos()
    }
    // 输出的结果在-PI ~ PI之间
    pub fn mod_to_round_half(&self) -> Self {
        let x = self.as_rad();
        let period = 2.0 * PI;
        Angular::from_rad(x - ((x + PI) / period).floor() * period)
    }
    //   输出在   0 ~ 2PI  之间
    pub fn mod_to_round(&self) -> Self {
        let v = self.as_rad() - TAU * f64::floor(self.as_rad() / TAU);
        Self::from_rad(v)
    }

    pub fn atan2(a: f64, b: f64) -> Self {
        let v = f64::atan2(a, b);
        Self::from_rad(v)
    }

    pub fn atan(a: f64) -> Self {
        let v = f64::atan(a);
        Self::from_rad(v)
    }

    pub fn asin(v: f64) -> Self {
        let rad = f64::asin(v);
        Self::from_rad(rad)
    }

    pub fn acos(v: f64) -> Self {
        let rad = f64::acos(v);
        Self::from_rad(rad)
    }
}

impl From<Coef> for Angular {
    // 角度也可以看做一个无量纲数,因为角度可以看做是弧长跟半径的比值,所以它们是可以相互转化的。
    fn from(value: Coef) -> Self {
        Angular::from_rad(value.get_value())
    }
}

impl Add for Angular {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        let v = self.as_deg() + rhs.as_deg();
        Angular::from_deg(v)
    }
}

// 引用-引用 与 混合引用:Angular 加法
impl<'a, 'b> Add<&'b Angular> for &'a Angular {
    type Output = Angular;
    fn add(self, rhs: &'b Angular) -> Self::Output {
        Angular::from_deg(self.as_deg() + rhs.as_deg())
    }
}
impl<'a> Add<&'a Angular> for Angular {
    type Output = Angular;
    fn add(self, rhs: &'a Angular) -> Self::Output {
        Angular::from_deg(self.as_deg() + rhs.as_deg())
    }
}
impl<'a> Add<Angular> for &'a Angular {
    type Output = Angular;
    fn add(self, rhs: Angular) -> Self::Output {
        Angular::from_deg(self.as_deg() + rhs.as_deg())
    }
}

// Angular + 裸 f64:按**当前存储单位**直接相加,不做单位换算 —— 有意设计。
// 裸 f64 不带单位,库无从判断它代表哪种单位;擅自换算等于替调用方做假设。
// 需要单位安全时改用两个物理量运算,如 `x + Angular::from_rad(1.0)`。
// 详见 `physics::basic` 模块文档「物理量与裸 f64 的运算」。
impl Add<f64> for Angular {
    type Output = Self;
    fn add(self, rhs: f64) -> Self::Output {
        let v = self.v + rhs;
        Angular {
            v,
            default_type: self.default_type,
        }
    }
}

impl Sub for Angular {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        let v = self.as_deg() - rhs.as_deg();
        Angular::from_deg(v)
    }
}

// 引用-引用 与 混合引用:Angular 减法
impl<'a, 'b> Sub<&'b Angular> for &'a Angular {
    type Output = Angular;
    fn sub(self, rhs: &'b Angular) -> Self::Output {
        Angular::from_deg(self.as_deg() - rhs.as_deg())
    }
}
impl<'a> Sub<&'a Angular> for Angular {
    type Output = Angular;
    fn sub(self, rhs: &'a Angular) -> Self::Output {
        Angular::from_deg(self.as_deg() - rhs.as_deg())
    }
}
impl<'a> Sub<Angular> for &'a Angular {
    type Output = Angular;
    fn sub(self, rhs: Angular) -> Self::Output {
        Angular::from_deg(self.as_deg() - rhs.as_deg())
    }
}

impl Sub<f64> for Angular {
    type Output = Self;
    fn sub(self, rhs: f64) -> Self::Output {
        let v = self.v - rhs;
        Angular {
            v,
            default_type: self.default_type,
        }
    }
}

impl Div<chrono::TimeDelta> for Angular {
    type Output = AngularVelocity;
    fn div(self, rhs: chrono::TimeDelta) -> Self::Output {
        let v = self.as_rad() / time_delta_to_secs_f64(&rhs);
        AngularVelocity::from_rad_per_second(v)
    }
}

// 引用版本:Angular / TimeDelta -> AngularVelocity
// 引用版本:Angular / TimeDelta -> AngularVelocity
impl<'a> Div<chrono::TimeDelta> for &'a Angular {
    type Output = AngularVelocity;
    fn div(self, rhs: chrono::TimeDelta) -> Self::Output {
        AngularVelocity::from_rad_per_second(self.as_rad() / time_delta_to_secs_f64(&rhs))
    }
}
impl<'a> Div<&'a chrono::TimeDelta> for Angular {
    type Output = AngularVelocity;
    fn div(self, rhs: &'a chrono::TimeDelta) -> Self::Output {
        AngularVelocity::from_rad_per_second(self.as_rad() / time_delta_to_secs_f64(rhs))
    }
}
impl<'a, 'b> Div<&'b chrono::TimeDelta> for &'a Angular {
    type Output = AngularVelocity;
    fn div(self, rhs: &'b chrono::TimeDelta) -> Self::Output {
        AngularVelocity::from_rad_per_second(self.as_rad() / time_delta_to_secs_f64(rhs))
    }
}

impl Div<AngularVelocity> for Angular {
    type Output = chrono::TimeDelta;
    fn div(self, rhs: AngularVelocity) -> Self::Output {
        time_delta_from_secs_f64(self.as_rad() / rhs.as_rad_per_second())
    }
}

impl<'a, 'b> Div<&'b AngularVelocity> for &'a Angular {
    type Output = chrono::TimeDelta;
    fn div(self, rhs: &'b AngularVelocity) -> Self::Output {
        time_delta_from_secs_f64(self.as_rad() / rhs.as_rad_per_second())
    }
}

impl<'a> Div<&'a AngularVelocity> for Angular {
    type Output = chrono::TimeDelta;
    fn div(self, rhs: &'a AngularVelocity) -> Self::Output {
        time_delta_from_secs_f64(self.as_rad() / rhs.as_rad_per_second())
    }
}

impl<'a> Div<AngularVelocity> for &'a Angular {
    type Output = chrono::TimeDelta;
    fn div(self, rhs: AngularVelocity) -> Self::Output {
        time_delta_from_secs_f64(self.as_rad() / rhs.as_rad_per_second())
    }
}

impl Mul<f64> for Angular {
    type Output = Self;
    fn mul(self, rhs: f64) -> Self::Output {
        let v = self.as_rad() * rhs;
        Self::from_rad(v)
    }
}

impl Div<f64> for Angular {
    type Output = Self;
    fn div(self, rhs: f64) -> Self::Output {
        let v = self.as_rad() / rhs;
        Self::from_rad(v)
    }
}

impl Mul<Angular> for f64 {
    type Output = Angular;
    fn mul(self, rhs: Angular) -> Self::Output {
        rhs * self
    }
}

impl Div<Angular> for f64 {
    type Output = Angular;
    fn div(self, rhs: Angular) -> Self::Output {
        let v = self / rhs.v;
        Angular {
            default_type: rhs.default_type,
            v: v,
        }
    }
}

impl Mul<Coef> for Angular {
    type Output = Self;
    fn mul(self, rhs: Coef) -> Self::Output {
        let v = self.as_rad() * rhs.get_value();
        Self::from_rad(v)
    }
}

impl Div<Coef> for Angular {
    type Output = Self;
    fn div(self, rhs: Coef) -> Self::Output {
        let v = self.as_rad() / rhs.get_value();
        Self::from_rad(v)
    }
}

impl Neg for Angular {
    type Output = Self;
    fn neg(self) -> Self::Output {
        let v = -self.as_rad();
        Self::from_rad(v)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::TimeDelta;
    use approx::assert_relative_eq;
    #[test]
    fn test_angular() {
        let angular = Angular::from_rad(1.0);
        assert_eq!(angular.v, 1.0);
        assert_eq!(angular.default_type, AngularType::Rad);

        let angular = Angular::from_rad(6.15 * PI);
        assert_eq!(angular.default_type, AngularType::Rad);
        assert_relative_eq!(angular.v, 6.15 * PI, epsilon = 1.0e-6);

        let angular = Angular::from_deg(1.0);
        assert_eq!(angular.default_type, AngularType::Deg);
        assert_eq!(angular.v, 1.0);
    }
    #[test]
    fn test_angular_convert() {
        let angular = Angular::from_deg(1.0);
        assert_relative_eq!(angular.as_deg(), 1.0);
        assert_relative_eq!(angular.as_rad(), 0.0174532925, epsilon = 1e-8);

        let angular = Angular::from_rad(2.0 * PI);
        assert_relative_eq!(angular.as_deg(), 360.0);
        assert_relative_eq!(angular.as_rad(), 2.0 * PI, epsilon = 1e-8);

        let c: Coef = Coef::new(0.5 * PI);
        let angular: Angular = Angular::from(c);
        assert_relative_eq!(angular.as_deg(), 90.0);
    }

    #[test]
    fn test_angular_sin_cos() {
        let angular = Angular::from_deg(30.0);
        assert_relative_eq!(angular.sin(), 0.5, epsilon = 1e-8);
        assert_relative_eq!(angular.cos(), 0.866025403784, epsilon = 1e-8);

        let angular = Angular::from_rad(PI / 6.0);
        assert_relative_eq!(angular.sin(), 0.5, epsilon = 1e-8);
        assert_relative_eq!(angular.cos(), 0.866025403784, epsilon = 1e-8);

        let angular = Angular::from_rad(PI / 4.0);
        assert_relative_eq!(angular.tan(), 1.0, epsilon = 1e-8);
    }

    #[test]
    fn test_angular_add() {
        {
            let theta1 = Angular::from_rad(PI * 0.5);
            let theta2 = Angular::from_rad(PI * 0.5);
            let theta3 = theta1 + theta2;
            assert_relative_eq!(PI, theta3.as_rad(), epsilon = 1e-8);

            let theta1 = Angular::from_deg(30.0);
            let theta2 = Angular::from_rad(PI / 6.0);
            let theta3 = theta1 + theta2;
            assert_relative_eq!(60.0, theta3.as_deg(), epsilon = 1e-8);

            let theta1 = Angular::from_rad(PI / 6.0);
            let theta2 = Angular::from_deg(30.0);
            let theta3 = theta1 + theta2;
            assert_relative_eq!(60.0, theta3.as_deg(), epsilon = 1e-8);

            let theta1 = Angular::from_deg(30.0);
            let theta2 = Angular::from_deg(90.0);
            let theta3 = theta1 + theta2;
            assert_relative_eq!(2.0 * PI / 3.0, theta3.as_rad(), epsilon = 1e-8);
        }
        {
            let theta1 = Angular::from_deg(30.0);
            let theta2 = theta1 + 30.0;
            assert_relative_eq!(60.0, theta2.as_deg(), epsilon = 1e-8);

            let theta1 = Angular::from_rad(PI * 0.25);
            let theta2 = theta1 + PI * 0.25;
            assert_relative_eq!(90.0, theta2.as_deg(), epsilon = 1e-8);
        }
    }

    #[test]
    fn test_angular_sub() {
        {
            let theta1 = Angular::from_rad(PI * 1.0);
            let theta2 = Angular::from_rad(PI * 0.5);
            let theta3 = theta1 - theta2;
            assert_relative_eq!(PI * 0.5, theta3.as_rad(), epsilon = 1e-8);

            let theta1 = Angular::from_deg(60.0);
            let theta2 = Angular::from_rad(PI / 6.0);
            let theta3 = theta1 - theta2;
            assert_relative_eq!(30.0, theta3.as_deg(), epsilon = 1e-8);

            let theta1 = Angular::from_rad(PI / 6.0);
            let theta2 = Angular::from_deg(15.0);
            let theta3 = theta1 - theta2;
            assert_relative_eq!(15.0, theta3.as_deg(), epsilon = 1e-8);

            let theta1 = Angular::from_deg(90.0);
            let theta2 = Angular::from_deg(30.0);
            let theta3 = theta1 - theta2;
            assert_relative_eq!(PI / 3.0, theta3.as_rad(), epsilon = 1e-8);
        }
        {
            let theta1 = Angular::from_deg(30.0);
            let theta2 = theta1 - 10.0;
            assert_relative_eq!(20.0, theta2.as_deg(), epsilon = 1e-8);

            let theta1 = Angular::from_rad(PI * 0.5);
            let theta2 = theta1 - PI * 0.25;
            assert_relative_eq!(45.0, theta2.as_deg(), epsilon = 1e-8);
        }
    }

    #[test]
    fn test_angular_mod_round() {
        let theta1 = Angular::from_deg(372.0);
        let theta2 = theta1.mod_to_round_half();
        assert_relative_eq!(12.0, theta2.as_deg(), epsilon = 1e-8);

        let theta1 = Angular::from_deg(315.0);
        let theta2 = theta1.mod_to_round_half();
        assert_relative_eq!(-1.0 * PI / 4.0, theta2.as_rad(), epsilon = 1e-8);

        let theta1 = Angular::from_deg(372.0);
        let theta2 = theta1.mod_to_round();
        assert_relative_eq!(12.0, theta2.as_deg(), epsilon = 1e-8);

        let theta1 = Angular::from_deg(315.0);
        let theta2 = theta1.mod_to_round();
        assert_relative_eq!(315.0, theta2.as_deg(), epsilon = 1e-8);
    }

    #[test]
    fn test_angular_to_angular_velocity() {
        let theta1 = Angular::from_rad(PI * 0.5);
        let omg1 = theta1 / TimeDelta::seconds(2);
        assert_eq!(omg1.default_type, AngularVelocityType::RadperSecond);
        assert_eq!(omg1.v, PI / 4.0);
    }

    #[test]
    fn test_angular_to_angular_velocity_timedelta() {
        let theta = Angular::from_rad(PI * 0.5);
        let td = time_delta_from_secs_f64(2.0);
        let omg = theta / td;
        assert_eq!(omg.v, PI / 4.0);

        // 负时间 → 负角速度
        let neg_td = time_delta_from_secs_f64(-2.0);
        let omg_neg = theta / neg_td;
        assert_eq!(omg_neg.as_rad_per_second(), -PI / 4.0);
    }

    #[test]
    fn test_to_mul() {
        let d1 = Angular::from_rad(1000.0);
        let d2 = d1 * 2.0;
        assert_eq!(d2.as_rad(), 2000.0);
        let d1 = Angular::from_rad(1000.0);
        let d2 = d1 * Coef::new(2.0);
        assert_eq!(d2.as_rad(), 2000.0);
    }

    #[test]
    fn test_to_div() {
        let d1 = Angular::from_rad(1000.0);
        let d2 = d1 / 2.0;
        assert_eq!(d2.as_rad(), 500.0);
        let d1 = Angular::from_rad(1000.0);
        let d2 = d1 / Coef::new(2.0);
        assert_eq!(d2.as_rad(), 500.0);
    }

    #[test]
    fn test_atan() {
        let sin = 0.5;
        let cos = 0.8660254037844386;
        let theta = Angular::atan2(sin, cos);
        assert_relative_eq!(theta.as_deg(), 30.0, epsilon = 1e-8);

        let value = 1.0f64;
        let theta = Angular::atan(value);
        assert_relative_eq!(theta.as_deg(), 45.0, epsilon = 1e-8);

        let value = 0.5f64;
        let theta = Angular::asin(value);
        assert_relative_eq!(theta.as_deg(), 30.0, epsilon = 1e-8);
    }

    #[test]
    fn test_acos() {
        // Test normal cases
        let cases = vec![
            (1.0, 0.0),             // acos(1) = 0 radians
            (0.5, PI / 3.0),        // acos(0.5) = π/3 radians (60 degrees)
            (0.0, PI / 2.0),        // acos(0) = π/2 radians (90 degrees)
            (-0.5, 2.0 * PI / 3.0), // acos(-0.5) = 2π/3 radians (120 degrees)
            (-1.0, PI),             // acos(-1) = π radians (180 degrees)
        ];

        for (input, expected_rad) in cases {
            let angle = Angular::acos(input);
            assert_relative_eq!(angle.as_rad(), expected_rad, epsilon = 1e-8);
            // Verify the conversion to degrees works as expected
            assert_relative_eq!(angle.as_deg(), expected_rad * 180.0 / PI, epsilon = 1e-8);
        }

        // Test edge cases
        {
            // Test input exactly at 1.0
            let angle = Angular::acos(1.0);
            assert_relative_eq!(angle.as_rad(), 0.0, epsilon = 1e-8);
        }
        {
            // Test input exactly at -1.0
            let angle = Angular::acos(-1.0);
            assert_relative_eq!(angle.as_rad(), PI, epsilon = 1e-8);
        }

        // Test invalid inputs (should panic as per f64::acos behavior)
        let invalid_cases = vec![1.00001, -1.00001, 2.0, -2.0, f64::NAN, f64::INFINITY];
        for invalid_input in invalid_cases {
            let angle = Angular::acos(invalid_input);
            assert_eq!(true, angle.v.is_nan());
        }

        // Test precision cases (values very close to boundaries)
        let epsilon = 1e-10;
        {
            let angle = Angular::acos(1.0 - epsilon);
            assert!(angle.as_rad() > 0.0 && angle.as_rad() < 1.5e-5);
        }
        {
            let angle = Angular::acos(-1.0 + epsilon);
            assert!(angle.as_rad() > PI - 1.5e-5 && angle.as_rad() < PI);
        }
    }

    #[test]
    fn test_f64_mul_angular() {
        // f64 * Angular 测试
        let a = Angular::from_rad(PI);
        let result = 2.0 * a;
        assert_relative_eq!(result.as_rad(), 2.0 * PI);

        let a = Angular::from_deg(90.0);
        let result = 2.0 * a;
        assert_relative_eq!(result.as_deg(), 180.0);
    }

    #[test]
    fn test_f64_div_angular() {
        // f64 / Angular 测试
        let a = Angular::from_rad(PI);
        let result = 2.0 * PI / a;
        assert_relative_eq!(result.as_rad(), 2.0);

        let a = Angular::from_deg(180.0);
        let result = 180.0 / a;
        assert_relative_eq!(result.as_deg(), 1.0);
    }

    #[test]
    fn test_angular_ref_ops() {
        let a1 = Angular::from_deg(30.0);
        let a2 = Angular::from_rad(std::f64::consts::PI / 6.0); // 30 deg
        let s = &a1 + &a2;
        assert_relative_eq!(s.as_deg(), 60.0);

        let d = &a1 - &a2;
        assert_relative_eq!(d.as_deg(), 0.0, epsilon = 1e-10);

        let omg = &a1 / &TimeDelta::seconds(2);
        assert_relative_eq!(omg.as_rad_per_second(), (std::f64::consts::PI / 12.0));

        // 混合引用:加/减
        let s2 = a1 + &a2;
        assert_relative_eq!(s2.as_deg(), 60.0, epsilon = 1e-10);
        let s3 = &a1 + a2;
        assert_relative_eq!(s3.as_deg(), 60.0, epsilon = 1e-10);
        let a2b = Angular::from_rad(std::f64::consts::PI / 6.0);
        let d2 = a1 - &a2b;
        assert_relative_eq!(d2.as_deg(), 0.0, epsilon = 1e-10);
        let d3 = &a1 - a2b;
        assert_relative_eq!(d3.as_deg(), 0.0, epsilon = 1e-10);

        // 混合引用:/ TimeDelta
        let dur = TimeDelta::seconds(2);
        let w2 = a1 / &dur;
        assert_relative_eq!(w2.as_rad_per_second(), (std::f64::consts::PI / 12.0));
        let w3 = &a1 / dur;
        assert_relative_eq!(w3.as_rad_per_second(), (std::f64::consts::PI / 12.0));

        // 混合引用:/ TimeDelta
        let td = time_delta_from_secs_f64(2.0);
        let w4 = a1 / &td;
        assert_relative_eq!(w4.as_rad_per_second(), (std::f64::consts::PI / 12.0));
        let w5 = &a1 / td;
        assert_relative_eq!(w5.as_rad_per_second(), (std::f64::consts::PI / 12.0));
        let td2 = time_delta_from_secs_f64(2.0);
        let w6 = &a1 / &td2;
        assert_relative_eq!(w6.as_rad_per_second(), (std::f64::consts::PI / 12.0));
    }

    #[test]
    fn test_angular_neg() {
        // 测试正值的负号
        let a1 = Angular::from_rad(PI);
        let neg_a1 = -a1;
        assert_relative_eq!(neg_a1.as_rad(), -PI);

        // 测试负值的负号
        let a2 = Angular::from_deg(-90.0);
        let neg_a2 = -a2;
        assert_relative_eq!(neg_a2.as_deg(), 90.0);

        // 测试零值
        let a3 = Angular::from_rad(0.0);
        let neg_a3 = -a3;
        assert_relative_eq!(neg_a3.as_rad(), 0.0);

        // 测试不同单位的负号操作
        let a4 = Angular::from_deg(180.0);
        let neg_a4 = -a4;
        assert_relative_eq!(neg_a4.as_rad(), -PI);

        // 测试大数值
        let a5 = Angular::from_rad(2.0 * PI);
        let neg_a5 = -a5;
        assert_relative_eq!(neg_a5.as_deg(), -360.0);
    }

    #[test]
    fn test_angular_div_angular_velocity() {
        // 基本测试:弧度 / 弧度每秒 = 秒
        {
            let angle = Angular::from_rad(PI);
            let angular_velocity = AngularVelocity::from_rad_per_second(0.5 * PI);
            let duration = angle / angular_velocity;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 2.0);
        }

        // 基本测试:度 / 度每秒 = 秒
        {
            let angle = Angular::from_deg(180.0);
            let angular_velocity = AngularVelocity::from_deg_per_second(90.0);
            let duration = angle / angular_velocity;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 2.0);
        }

        // 混合单位测试:度 / 弧度每秒
        {
            let angle = Angular::from_deg(180.0); // π 弧度
            let angular_velocity = AngularVelocity::from_rad_per_second(PI);
            let duration = angle / angular_velocity;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 1.0);
        }

        // 混合单位测试:弧度 / 度每秒
        {
            let angle = Angular::from_rad(PI);
            let angular_velocity = AngularVelocity::from_deg_per_second(180.0); // π 弧度/秒
            let duration = angle / angular_velocity;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 1.0);
        }

        // 测试弧度/小时单位
        {
            let angle = Angular::from_rad(2.0 * PI);
            let angular_velocity = AngularVelocity::from_rad_per_hour(3600.0 * PI); // π 弧度/秒 = 3600π 弧度/小时
            let duration = angle / angular_velocity;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 2.0);
        }

        // 测试度/小时单位
        {
            let angle = Angular::from_deg(360.0);
            let angular_velocity = AngularVelocity::from_deg_per_hour(180.0 * 3600.0); // 180度/秒 = 180*3600度/小时
            let duration = angle / angular_velocity;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 2.0);
        }

        // 引用-引用测试
        {
            let angle = Angular::from_rad(PI);
            let angular_velocity = AngularVelocity::from_rad_per_second(0.5 * PI);
            let duration = &angle / &angular_velocity;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 2.0);
        }

        // 混合引用测试:值 / 引用
        {
            let angle = Angular::from_rad(PI);
            let angular_velocity = AngularVelocity::from_rad_per_second(0.5 * PI);
            let duration = angle / &angular_velocity;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 2.0);
        }

        // 混合引用测试:引用 / 值
        {
            let angle = Angular::from_rad(PI);
            let angular_velocity = AngularVelocity::from_rad_per_second(0.5 * PI);
            let duration = &angle / angular_velocity;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 2.0);
        }

        // 测试不同单位的混合引用
        {
            let angle = Angular::from_deg(90.0);
            let angular_velocity = AngularVelocity::from_rad_per_second(PI / 2.0);
            let duration = &angle / &angular_velocity;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 1.0);
        }

        {
            let angle = Angular::from_deg(90.0);
            let angular_velocity = AngularVelocity::from_rad_per_second(PI / 2.0);
            let duration = angle / &angular_velocity;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 1.0);
        }

        {
            let angle = Angular::from_deg(90.0);
            let angular_velocity = AngularVelocity::from_rad_per_second(PI / 2.0);
            let duration = &angle / angular_velocity;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 1.0);
        }

        // 测试零角度
        {
            let angle = Angular::from_rad(0.0);
            let angular_velocity = AngularVelocity::from_rad_per_second(1.0);
            let duration = angle / angular_velocity;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 0.0);
        }

        // 测试大角度
        {
            let angle = Angular::from_rad(10.0 * PI);
            let angular_velocity = AngularVelocity::from_rad_per_second(PI);
            let duration = angle / angular_velocity;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 10.0);
        }

        // 测试小角速度
        {
            let angle = Angular::from_rad(PI);
            let angular_velocity = AngularVelocity::from_rad_per_second(0.1);
            let duration = angle / angular_velocity;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), 10.0 * PI, epsilon = 1e-8);
        }

        // 负角度 → 负时间
        {
            let angle = Angular::from_rad(-PI);
            let angular_velocity = AngularVelocity::from_rad_per_second(PI);
            let duration = angle / angular_velocity;
            assert_relative_eq!(time_delta_to_secs_f64(&duration), -1.0);
        }
    }
}