trig/
f32_impl.rs

1use crate::trig_trait::Trig;
2
3impl Trig for f32 {
4    fn sin(&self) -> f32 {
5        f32::sin(*self)
6    }
7    fn cos(&self) -> f32 {
8        f32::cos(*self)
9    }
10    fn tan(&self) -> f32 {
11        f32::tan(*self)
12    }
13    fn csc(&self) -> f32 {
14        1.0 / self.sin()
15    }
16    fn sec(&self) -> f32 {
17        1.0 / self.cos()
18    }
19    fn cot(&self) -> f32 {
20        1.0 / self.tan()
21    }
22    fn asin(&self) -> f32 {
23        f32::asin(*self)
24    }
25    fn acos(&self) -> f32 {
26        f32::acos(*self)
27    }
28    fn atan(&self) -> f32 {
29        f32::atan(*self)
30    }
31    fn atan2(&self, other: &f32) -> f32 {
32        f32::atan2(*self, *other)
33    }
34    fn acsc(&self) -> f32 {
35        (1.0 / self).asin()
36    }
37    fn asec(&self) -> f32 {
38        (1.0 / self).acos()
39    }
40    fn acot(&self) -> f32 {
41        (1.0 / self).atan()
42    }
43    fn deg2rad(&self) -> f32 {
44        self * (std::f32::consts::PI / 180.0)
45    }
46    fn rad2deg(&self) -> f32 {
47        self * (180.0 / std::f32::consts::PI)
48    }
49    fn sind(&self) -> f32 {
50        self.deg2rad().sin()
51    }
52    fn cosd(&self) -> f32 {
53        self.deg2rad().cos()
54    }
55    fn tand(&self) -> f32 {
56        self.deg2rad().tan()
57    }
58    fn cscd(&self) -> f32 {
59        self.deg2rad().csc()
60    }
61    fn secd(&self) -> f32 {
62        self.deg2rad().sec()
63    }
64    fn cotd(&self) -> f32 {
65        self.deg2rad().cot()
66    }
67    fn asind(&self) -> f32 {
68        self.asin().rad2deg()
69    }
70    fn acosd(&self) -> f32 {
71        self.acos().rad2deg()
72    }
73    fn atand(&self) -> f32 {
74        self.atan().rad2deg()
75    }
76    fn atan2d(&self, other: &f32) -> f32 {
77        self.atan2(other).rad2deg()
78    }
79    fn acscd(&self) -> f32 {
80        self.acsc().rad2deg()
81    }
82    fn asecd(&self) -> f32 {
83        self.asec().rad2deg()
84    }
85    fn acotd(&self) -> f32 {
86        self.acot().rad2deg()
87    }
88    fn sinh(&self) -> f32 {
89        f32::sinh(*self)
90    }
91    fn cosh(&self) -> f32 {
92        f32::cosh(*self)
93    }
94    fn tanh(&self) -> f32 {
95        f32::tanh(*self)
96    }
97    fn csch(&self) -> f32 {
98        1.0 / self.sinh()
99    }
100    fn sech(&self) -> f32 {
101        1.0 / self.cosh()
102    }
103    fn coth(&self) -> f32 {
104        1.0 / self.tanh()
105    }
106    fn asinh(&self) -> f32 {
107        f32::asinh(*self)
108    }
109    fn acosh(&self) -> f32 {
110        f32::acosh(*self)
111    }
112    fn atanh(&self) -> f32 {
113        f32::atanh(*self)
114    }
115    fn acsch(&self) -> f32 {
116        (1.0 / self).asinh()
117    }
118    fn asech(&self) -> f32 {
119        (1.0 / self).acosh()
120    }
121    fn acoth(&self) -> f32 {
122        (1.0 / self).atanh()
123    }
124}
125
126#[cfg(test)]
127mod test {
128    use super::*;
129    use numtest::*;
130    use std::f32::consts::E;
131
132    #[test]
133    fn test_sin() {
134        assert_eq!(std::f32::consts::FRAC_PI_2.sin(), 1.0);
135    }
136
137    #[test]
138    fn test_cos() {
139        assert_eq!((2.0 * std::f32::consts::PI).cos(), 1.0);
140    }
141
142    #[test]
143    fn test_tan() {
144        assert_eq!(std::f32::consts::FRAC_PI_4.tan(), 1.0);
145    }
146
147    #[test]
148    fn test_csc() {
149        assert_eq!(std::f32::consts::FRAC_PI_2.csc(), 1.0);
150    }
151
152    #[test]
153    fn test_sec() {
154        assert_eq!(std::f32::consts::PI.sec(), -1.0);
155    }
156
157    #[test]
158    fn test_cot() {
159        assert_eq!(std::f32::consts::FRAC_PI_4.cot(), 1.0);
160    }
161
162    #[test]
163    fn test_asin() {
164        assert_equal_to_atol!(
165            std::f32::consts::FRAC_PI_2.sin().asin(),
166            std::f32::consts::FRAC_PI_2,
167            1e-6
168        );
169    }
170
171    #[test]
172    fn test_acos() {
173        assert_eq!(
174            std::f32::consts::FRAC_PI_4.cos().acos(),
175            std::f32::consts::FRAC_PI_4
176        );
177    }
178
179    #[test]
180    fn test_atan() {
181        assert_eq!(1.0_f32.tan().atan(), 1.0_f32);
182    }
183
184    #[test]
185    fn test_atan2_45_deg_clockwise() {
186        let x = 3.0_f32;
187        let y = -3.0_f32;
188        let angle_expected = -std::f32::consts::FRAC_PI_4;
189        assert_eq!(y.atan2(x), angle_expected);
190    }
191
192    #[test]
193    fn test_atan2_135_deg_counterclockwise() {
194        let x = -3.0_f32;
195        let y = 3.0_f32;
196        let angle_expected = 3.0 * std::f32::consts::FRAC_PI_4;
197        assert_eq!(y.atan2(x), angle_expected);
198    }
199
200    #[test]
201    fn test_acsc() {
202        assert_equal_to_atol!(
203            std::f32::consts::FRAC_PI_2.csc().acsc(),
204            std::f32::consts::FRAC_PI_2,
205            1e-6
206        );
207    }
208
209    #[test]
210    fn test_asec() {
211        assert_eq!(
212            std::f32::consts::FRAC_PI_4.sec().asec(),
213            std::f32::consts::FRAC_PI_4
214        );
215    }
216
217    #[test]
218    fn test_acot() {
219        assert_eq!(1.0_f32.cot().acot(), 1.0_f32);
220    }
221
222    #[test]
223    fn test_deg2rad() {
224        assert_eq!(30.0_f32.deg2rad(), std::f32::consts::FRAC_PI_6);
225    }
226
227    #[test]
228    fn test_rad2deg() {
229        assert_eq!(std::f32::consts::FRAC_PI_6.rad2deg(), 30.0);
230    }
231
232    #[test]
233    fn test_sind() {
234        assert_eq!(90.0_f32.sind(), 1.0);
235    }
236
237    #[test]
238    fn test_cosd() {
239        assert_eq!(360.0_f32.cosd(), 1.0);
240    }
241
242    #[test]
243    fn test_tand() {
244        assert_eq!(45.0_f32.tand(), 1.0);
245    }
246
247    #[test]
248    fn test_cscd() {
249        assert_eq!(90.0_f32.cscd(), 1.0);
250    }
251
252    #[test]
253    fn test_secd() {
254        assert_eq!(180.0_f32.secd(), -1.0);
255    }
256
257    #[test]
258    fn test_cotd() {
259        assert_eq!(45.0_f32.cotd(), 1.0);
260    }
261
262    #[test]
263    fn test_asind() {
264        assert_equal_to_atol!(90.0_f32.sind().asind(), 90.0_f32, 1e-5);
265    }
266
267    #[test]
268    fn test_acosd() {
269        assert_eq!(45.0_f32.cosd().acosd(), 45.0_f32);
270    }
271
272    #[test]
273    fn test_atand() {
274        assert_eq!(30.0_f32.tand().atand(), 30.0_f32);
275    }
276
277    #[test]
278    fn test_atan2d_45_deg_clockwise() {
279        let x = 3.0_f32;
280        let y = -3.0_f32;
281        let angle_expected = -45.0_f32;
282        assert_eq!(y.atan2d(&x), angle_expected);
283    }
284
285    #[test]
286    fn test_atan2d_135_deg_counterclockwise() {
287        let x = -3.0_f32;
288        let y = 3.0_f32;
289        let angle_expected = 135.0_f32;
290        assert_eq!(y.atan2d(&x), angle_expected);
291    }
292
293    #[test]
294    fn test_acscd() {
295        assert_equal_to_atol!(90.0_f32.cscd().acscd(), 90.0_f32, 1e-5);
296    }
297
298    #[test]
299    fn test_asecd() {
300        assert_eq!(45.0_f32.secd().asecd(), 45.0_f32);
301    }
302
303    #[test]
304    fn test_acotd() {
305        assert_eq!(30.0_f32.cotd().acotd(), 30.0_f32);
306    }
307
308    #[test]
309    fn test_sinh() {
310        assert_eq!(1.0_f32.sinh(), ((E * E) - 1.0) / (2.0 * E));
311    }
312
313    #[test]
314    fn test_cosh() {
315        assert_equal_to_atol!(1.0_f32.cosh(), ((E * E) + 1.0) / (2.0 * E), 1e-6);
316    }
317
318    #[test]
319    fn test_tanh() {
320        assert_equal_to_atol!(
321            1.0_f32.tanh(),
322            (1.0 - E.powi(-2)) / (1.0 + E.powi(-2)),
323            1e-7
324        );
325    }
326
327    #[test]
328    fn test_csch() {
329        assert_equal_to_atol!(1.0_f32.csch(), (2.0 * E) / ((E * E) - 1.0), 1e-7);
330    }
331
332    #[test]
333    fn test_sech() {
334        assert_equal_to_atol!(1.0_f32.sech(), (2.0 * E) / ((E * E) + 1.0), 1e-7);
335    }
336
337    #[test]
338    fn test_coth() {
339        assert_equal_to_atol!(
340            1.0_f32.coth(),
341            (1.0 + E.powi(-2)) / (1.0 - E.powi(-2)),
342            1e-6
343        );
344    }
345
346    #[test]
347    fn test_asinh() {
348        assert_equal_to_atol!(1.0_f32.sinh().asinh(), 1.0, 1e-7);
349    }
350
351    #[test]
352    fn test_acosh() {
353        assert_equal_to_atol!(1.0_f32.cosh().acosh(), 1.0, 1e-7);
354    }
355
356    #[test]
357    fn test_atanh() {
358        assert_eq!(1.0_f32.tanh().atanh(), 1.0);
359    }
360
361    #[test]
362    fn test_acsch() {
363        assert_equal_to_atol!(1.0_f32.csch().acsch(), 1.0, 1e-7);
364    }
365
366    #[test]
367    fn test_asech() {
368        assert_equal_to_atol!(0.5_f32.sech().asech(), 0.5, 1e-7);
369    }
370
371    #[test]
372    fn test_acoth() {
373        assert_equal_to_atol!(1.5_f32.coth().acoth(), 1.5, 1e-6);
374    }
375}