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
/// Get the sign of the argument with a unit value.
/// Zero is of positive sign.
///
/// ## Arguments
///
/// * `x` - The function argument.
///
/// ## Example
///
/// ```
/// use mixed_num::trigonometry::*;
/// use fixed::{types::extra::U22, FixedI32};
///
/// let mut x = FixedI32::<U22>::from_num(-0.2);
/// let mut y = sign(x);
/// assert_eq!{ y.to_num::<f32>(), -1.0 };
///
/// x = FixedI32::<U22>::from_num(0.2);
/// y = sign(x);
/// assert_eq!{ y.to_num::<f32>(), 1.0 };
/// ```
/// Calculate sin(x) using a Taylor approximation of `sin(x)`.
///
/// Sin is calculated using the following polynomial:
///
/// `sin(x) = x -( x^3/6 )+( x^5/120 )-( x^7/5040 )+( x^9/362880 )`
///
/// ## Argument
///
/// * `x` - The value to apply the operation to.
///
/// `x` must be wrapped to the -π=<x<π range.
///
/// ## Example
///
/// ```
/// use mixed_num::trigonometry::*;
///
/// let mut x:f32 = 0.0;
/// let mut y = sin(x);
/// assert_eq!{ y, 0.0 };
///
/// x = 3.1415/2.0;
/// y = sin(x);
/// assert_eq!{ y, 1.0000035 };
///
/// x = 3.1415;
/// y = sin(x);
/// assert_eq!{ y, 9.274483e-5 };
/// ```
///
/// Calculate cosine using a Taylor approximation of `cos(x)`.
///
/// Cos is calculated by adding a phase shift to x and running it through the polynomial sine method.
///
/// ## Argument
///
/// * `x` - The value to apply the operation to.
///
/// `x` is wrapped to the -π=<x<π range in the function.
///
/// ## Example
///
/// ```
/// use mixed_num::trigonometry::*;
///
/// let mut x = 0f32;
/// let mut y = cos(x);
/// assert_eq!{ y, 1.0000035 };
///
/// x = 3.1415f32/2.0f32;
/// y = cos(x);
/// assert_eq!{ y, 4.6491623e-5 };
/// ```
///
/// ## Comparison and Error
///
/// The figure below shows the comparison between the polynomial cosine, and the `std::f32::cos` implementation.
/// The Difference between the two is plotted as the error.
///
/// 
///
/// The error of the method is compared to the sine implementation in the cordic crate.
///
/// The comparison is done for U22 signed fixed point.
///
/// The figure below is missing numbers on the y axis, but it is plotted on a linear scale, showing the relative error between the two methods.
///
/// 
///
/// Wrapps θ to the -π=<x<π range.
///
/// ## Arguments
///
/// * `phi` - The unwrapped phase in radians.
///
/// ## Example
///
/// ```
/// use mixed_num::trigonometry::*;
/// use fixed::{types::extra::U28, FixedI32};
///
/// let phi = FixedI32::<U28>::from_num(6);
/// let wrapped_phi = wrap_phase(phi);
/// assert_eq!{ wrapped_phi.to_num::<f32>(), -0.2831853 };
/// ```
/// Rase fixed number to an integer-valued power.
/// `base^power`.
///
/// ## Arguments
///
/// * `base` - The base number.
/// * `power` - The power to raise 'base' to.
///
/// ## Example
///
/// ```
/// use mixed_num::trigonometry::*;
/// use fixed::{types::extra::U22, FixedI32};
///
/// let mut x = FixedI32::<U22>::from_num(-2);
/// let y = powi(x, 2);
/// assert_eq!{ y.to_num::<f32>(), 4.0 };
/// ```