13882
|
1 |
use std::cmp;
|
|
2 |
use std::ops;
|
|
3 |
|
|
4 |
#[derive(Clone, Debug, Copy)]
|
|
5 |
pub struct FPNum {
|
|
6 |
is_negative: bool,
|
|
7 |
value: u64,
|
|
8 |
}
|
|
9 |
|
|
10 |
impl FPNum {
|
|
11 |
fn new(numerator: i32, denominator: u32) -> Self {
|
|
12 |
FPNum::from(numerator) / denominator
|
|
13 |
}
|
|
14 |
|
13885
|
15 |
#[inline]
|
13882
|
16 |
fn signum(&self) -> i8 {
|
|
17 |
if self.is_negative {
|
|
18 |
-1
|
|
19 |
} else {
|
|
20 |
1
|
|
21 |
}
|
|
22 |
}
|
|
23 |
|
13885
|
24 |
#[inline]
|
13882
|
25 |
fn is_negative(&self) -> bool {
|
|
26 |
self.is_negative
|
|
27 |
}
|
|
28 |
|
13885
|
29 |
#[inline]
|
13882
|
30 |
fn is_positive(&self) -> bool {
|
|
31 |
!self.is_negative
|
|
32 |
}
|
|
33 |
|
13885
|
34 |
#[inline]
|
13882
|
35 |
fn is_zero(&self) -> bool {
|
|
36 |
self.value == 0
|
|
37 |
}
|
|
38 |
|
13885
|
39 |
#[inline]
|
13882
|
40 |
fn abs(&self) -> Self {
|
|
41 |
Self {
|
|
42 |
is_negative: false,
|
|
43 |
value: self.value,
|
|
44 |
}
|
|
45 |
}
|
|
46 |
|
13885
|
47 |
#[inline]
|
13882
|
48 |
fn round(&self) -> i64 {
|
|
49 |
if self.is_negative {
|
|
50 |
-((self.value >> 32) as i64)
|
|
51 |
} else {
|
|
52 |
(self.value >> 32) as i64
|
|
53 |
}
|
|
54 |
}
|
|
55 |
|
13885
|
56 |
#[inline]
|
13882
|
57 |
fn sqr(&self) -> Self {
|
|
58 |
Self {
|
|
59 |
is_negative: false,
|
|
60 |
value: ((self.value as u128).pow(2) >> 32) as u64,
|
|
61 |
}
|
|
62 |
}
|
|
63 |
|
|
64 |
fn sqrt(&self) -> Self {
|
|
65 |
debug_assert!(!self.is_negative);
|
|
66 |
|
|
67 |
let mut t: u64 = 0x4000000000000000;
|
|
68 |
let mut r: u64 = 0;
|
|
69 |
let mut q = self.value;
|
|
70 |
|
|
71 |
for _ in 0..32 {
|
|
72 |
let s = r + t;
|
|
73 |
r >>= 1;
|
|
74 |
|
|
75 |
if s <= q {
|
|
76 |
q -= s;
|
|
77 |
r += t;
|
|
78 |
}
|
|
79 |
t >>= 2;
|
|
80 |
}
|
|
81 |
|
|
82 |
Self {
|
|
83 |
is_negative: false,
|
|
84 |
value: r << 16,
|
|
85 |
}
|
|
86 |
}
|
|
87 |
}
|
|
88 |
|
|
89 |
impl From<i32> for FPNum {
|
|
90 |
#[inline]
|
|
91 |
fn from(n: i32) -> Self {
|
|
92 |
FPNum {
|
|
93 |
is_negative: n < 0,
|
|
94 |
value: (n.abs() as u64) << 32,
|
|
95 |
}
|
|
96 |
}
|
|
97 |
}
|
|
98 |
|
|
99 |
impl From<u32> for FPNum {
|
|
100 |
#[inline]
|
|
101 |
fn from(n: u32) -> Self {
|
|
102 |
Self {
|
|
103 |
is_negative: false,
|
|
104 |
value: (n as u64) << 32,
|
|
105 |
}
|
|
106 |
}
|
|
107 |
}
|
|
108 |
|
|
109 |
impl From<FPNum> for f64 {
|
|
110 |
#[inline]
|
|
111 |
fn from(n: FPNum) -> Self {
|
|
112 |
if n.is_negative {
|
|
113 |
n.value as f64 / (-0x10000000 as f64)
|
|
114 |
} else {
|
|
115 |
n.value as f64 / 0x10000000 as f64
|
|
116 |
}
|
|
117 |
}
|
|
118 |
}
|
|
119 |
|
|
120 |
impl PartialEq for FPNum {
|
13885
|
121 |
#[inline]
|
13882
|
122 |
fn eq(&self, other: &Self) -> bool {
|
|
123 |
self.value == other.value && (self.is_negative == other.is_negative || self.value == 0)
|
|
124 |
}
|
|
125 |
}
|
|
126 |
|
|
127 |
impl Eq for FPNum {}
|
|
128 |
|
|
129 |
impl PartialOrd for FPNum {
|
13885
|
130 |
#[inline]
|
13882
|
131 |
fn partial_cmp(&self, rhs: &Self) -> std::option::Option<std::cmp::Ordering> {
|
|
132 |
Some(self.cmp(rhs))
|
|
133 |
}
|
|
134 |
}
|
|
135 |
|
|
136 |
impl Ord for FPNum {
|
|
137 |
#[inline]
|
|
138 |
fn cmp(&self, rhs: &Self) -> cmp::Ordering {
|
13884
|
139 |
#[inline]
|
|
140 |
fn extend(n: &FPNum) -> i128 {
|
|
141 |
if n.is_negative {
|
|
142 |
-(n.value as i128)
|
13882
|
143 |
} else {
|
13884
|
144 |
n.value as i128
|
13882
|
145 |
}
|
|
146 |
}
|
13884
|
147 |
extend(self).cmp(&(extend(rhs)))
|
13882
|
148 |
}
|
|
149 |
}
|
|
150 |
|
|
151 |
impl ops::Add for FPNum {
|
|
152 |
type Output = Self;
|
|
153 |
|
|
154 |
#[inline]
|
|
155 |
fn add(self, rhs: Self) -> Self {
|
|
156 |
if self.is_negative == rhs.is_negative {
|
|
157 |
Self {
|
|
158 |
is_negative: self.is_negative,
|
|
159 |
value: self.value + rhs.value,
|
|
160 |
}
|
|
161 |
} else if self.value > rhs.value {
|
|
162 |
Self {
|
|
163 |
is_negative: self.is_negative,
|
|
164 |
value: self.value - rhs.value,
|
|
165 |
}
|
|
166 |
} else {
|
|
167 |
Self {
|
|
168 |
is_negative: rhs.is_negative,
|
|
169 |
value: rhs.value - self.value,
|
|
170 |
}
|
|
171 |
}
|
|
172 |
}
|
|
173 |
}
|
|
174 |
|
|
175 |
impl ops::Sub for FPNum {
|
|
176 |
type Output = Self;
|
|
177 |
|
|
178 |
#[inline]
|
|
179 |
fn sub(self, rhs: Self) -> Self {
|
|
180 |
if self.is_negative == rhs.is_negative {
|
|
181 |
if self.value > rhs.value {
|
|
182 |
Self {
|
|
183 |
is_negative: self.is_negative,
|
|
184 |
value: self.value - rhs.value,
|
|
185 |
}
|
|
186 |
} else {
|
|
187 |
Self {
|
|
188 |
is_negative: !rhs.is_negative,
|
|
189 |
value: rhs.value - self.value,
|
|
190 |
}
|
|
191 |
}
|
|
192 |
} else {
|
|
193 |
Self {
|
|
194 |
is_negative: self.is_negative,
|
|
195 |
value: self.value + rhs.value,
|
|
196 |
}
|
|
197 |
}
|
|
198 |
}
|
|
199 |
}
|
|
200 |
|
|
201 |
impl ops::Neg for FPNum {
|
|
202 |
type Output = Self;
|
|
203 |
|
|
204 |
#[inline]
|
|
205 |
fn neg(self) -> Self {
|
|
206 |
Self {
|
|
207 |
is_negative: !self.is_negative,
|
|
208 |
value: self.value,
|
|
209 |
}
|
|
210 |
}
|
|
211 |
}
|
|
212 |
|
|
213 |
impl ops::Mul for FPNum {
|
|
214 |
type Output = Self;
|
|
215 |
|
|
216 |
#[inline]
|
|
217 |
fn mul(self, rhs: Self) -> Self {
|
|
218 |
Self {
|
|
219 |
is_negative: self.is_negative ^ rhs.is_negative,
|
|
220 |
value: ((self.value as u128 * rhs.value as u128) >> 32) as u64,
|
|
221 |
}
|
|
222 |
}
|
|
223 |
}
|
|
224 |
|
|
225 |
impl ops::Mul<i32> for FPNum {
|
|
226 |
type Output = Self;
|
|
227 |
|
|
228 |
#[inline]
|
|
229 |
fn mul(self, rhs: i32) -> Self {
|
|
230 |
Self {
|
|
231 |
is_negative: self.is_negative ^ (rhs < 0),
|
|
232 |
value: self.value * rhs.abs() as u64,
|
|
233 |
}
|
|
234 |
}
|
|
235 |
}
|
|
236 |
|
|
237 |
impl ops::Div for FPNum {
|
|
238 |
type Output = Self;
|
|
239 |
|
|
240 |
#[inline]
|
|
241 |
fn div(self, rhs: Self) -> Self {
|
|
242 |
Self {
|
|
243 |
is_negative: self.is_negative ^ rhs.is_negative,
|
|
244 |
value: (((self.value as u128) << 32) / rhs.value as u128) as u64,
|
|
245 |
}
|
|
246 |
}
|
|
247 |
}
|
|
248 |
|
|
249 |
impl ops::Div<i32> for FPNum {
|
|
250 |
type Output = Self;
|
|
251 |
|
|
252 |
#[inline]
|
|
253 |
fn div(self, rhs: i32) -> Self {
|
|
254 |
Self {
|
|
255 |
is_negative: self.is_negative ^ (rhs < 0),
|
|
256 |
value: self.value / rhs.abs() as u64,
|
|
257 |
}
|
|
258 |
}
|
|
259 |
}
|
|
260 |
|
|
261 |
impl ops::Div<u32> for FPNum {
|
|
262 |
type Output = Self;
|
|
263 |
|
|
264 |
#[inline]
|
|
265 |
fn div(self, rhs: u32) -> Self {
|
|
266 |
Self {
|
|
267 |
is_negative: self.is_negative,
|
|
268 |
value: self.value / rhs as u64,
|
|
269 |
}
|
|
270 |
}
|
|
271 |
}
|
|
272 |
|
|
273 |
/* TODO:
|
|
274 |
Distance
|
|
275 |
DistanceI
|
|
276 |
SignAs
|
|
277 |
AngleSin
|
|
278 |
AngleCos
|
|
279 |
*/
|
|
280 |
|
|
281 |
#[cfg(test)]
|
|
282 |
#[test]
|
|
283 |
fn basics() {
|
|
284 |
let n = FPNum::new(15, 2);
|
|
285 |
assert!(n.is_positive());
|
|
286 |
assert!(!n.is_negative());
|
|
287 |
|
|
288 |
assert!(!(-n).is_positive());
|
|
289 |
assert!((-n).is_negative());
|
|
290 |
|
|
291 |
assert_eq!(-(-n), n);
|
|
292 |
assert_eq!((-n).abs(), n);
|
|
293 |
assert_eq!(-n, FPNum::new(-15, 2));
|
|
294 |
|
|
295 |
assert_eq!(n.round(), 7);
|
|
296 |
assert_eq!((-n).round(), -7);
|
|
297 |
}
|
|
298 |
|
|
299 |
#[test]
|
|
300 |
fn zero() {
|
|
301 |
let z = FPNum::from(0);
|
|
302 |
let n = FPNum::new(15, 2);
|
|
303 |
|
|
304 |
assert!(z.is_zero());
|
|
305 |
assert!(z.is_positive());
|
|
306 |
assert!((-z).is_negative);
|
13883
|
307 |
assert_eq!(n - n, z);
|
|
308 |
assert_eq!(-n + n, z);
|
13882
|
309 |
}
|
|
310 |
|
|
311 |
#[test]
|
13884
|
312 |
fn ord() {
|
|
313 |
let z = FPNum::from(0);;
|
|
314 |
let n1_5 = FPNum::new(3, 2);
|
|
315 |
let n2_25 = FPNum::new(9, 4);
|
|
316 |
|
|
317 |
assert!(!(z > z));
|
|
318 |
assert!(!(z < z));
|
|
319 |
assert!(n2_25 > n1_5);
|
|
320 |
assert!(-n2_25 < n1_5);
|
|
321 |
assert!(-n2_25 < -n1_5);
|
|
322 |
}
|
|
323 |
|
|
324 |
#[test]
|
13882
|
325 |
fn arith() {
|
|
326 |
let n1_5 = FPNum::new(3, 2);
|
|
327 |
let n2_25 = FPNum::new(9, 4);
|
13883
|
328 |
let n_0_15 = FPNum::new(-15, 100);
|
13882
|
329 |
|
|
330 |
assert_eq!(n1_5 + n1_5, FPNum::from(3));
|
|
331 |
assert_eq!(-n1_5 - n1_5, FPNum::from(-3));
|
|
332 |
|
|
333 |
assert_eq!(n1_5 * n1_5, n2_25);
|
|
334 |
assert_eq!(-n1_5 * -n1_5, n2_25);
|
|
335 |
assert_eq!(n1_5 * -n1_5, -n2_25);
|
|
336 |
assert_eq!(-n1_5 * n1_5, -n2_25);
|
|
337 |
|
13883
|
338 |
assert_eq!(-n2_25 / -n1_5, n1_5);
|
|
339 |
assert_eq!(n1_5 / -10, n_0_15);
|
|
340 |
|
13882
|
341 |
assert_eq!(n1_5.sqr(), n2_25);
|
|
342 |
assert_eq!((-n1_5).sqr(), n2_25);
|
|
343 |
|
|
344 |
assert_eq!(n2_25.sqrt(), n1_5);
|
|
345 |
|
|
346 |
assert_eq!((n1_5 * n1_5 * n1_5.sqr()).sqrt(), n2_25);
|
|
347 |
}
|