13887
|
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 |
|
13890
|
15 |
#[inline]
|
13887
|
16 |
fn signum(&self) -> i8 {
|
|
17 |
if self.is_negative {
|
|
18 |
-1
|
|
19 |
} else {
|
|
20 |
1
|
|
21 |
}
|
|
22 |
}
|
|
23 |
|
13890
|
24 |
#[inline]
|
13887
|
25 |
fn is_negative(&self) -> bool {
|
|
26 |
self.is_negative
|
|
27 |
}
|
|
28 |
|
13890
|
29 |
#[inline]
|
13887
|
30 |
fn is_positive(&self) -> bool {
|
|
31 |
!self.is_negative
|
|
32 |
}
|
|
33 |
|
13890
|
34 |
#[inline]
|
13887
|
35 |
fn is_zero(&self) -> bool {
|
|
36 |
self.value == 0
|
|
37 |
}
|
|
38 |
|
13890
|
39 |
#[inline]
|
13887
|
40 |
fn abs(&self) -> Self {
|
|
41 |
Self {
|
|
42 |
is_negative: false,
|
|
43 |
value: self.value,
|
|
44 |
}
|
|
45 |
}
|
|
46 |
|
13890
|
47 |
#[inline]
|
13887
|
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 |
|
13890
|
56 |
#[inline]
|
13887
|
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 {
|
13890
|
121 |
#[inline]
|
13887
|
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 {
|
13890
|
130 |
#[inline]
|
13887
|
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 {
|
13889
|
139 |
#[inline]
|
|
140 |
fn extend(n: &FPNum) -> i128 {
|
|
141 |
if n.is_negative {
|
|
142 |
-(n.value as i128)
|
13887
|
143 |
} else {
|
13889
|
144 |
n.value as i128
|
13887
|
145 |
}
|
|
146 |
}
|
13889
|
147 |
extend(self).cmp(&(extend(rhs)))
|
13887
|
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 |
|
13896
|
273 |
macro_rules! fp {
|
|
274 |
(-$n: tt / $d: tt) => { FPNum::new(-$n, $d) };
|
|
275 |
($n: tt / $d: tt) => { FPNum::new($n, $d) };
|
13910
|
276 |
(-$n: tt) => { FPNum::from(-$n) };
|
13896
|
277 |
($n: tt) => { FPNum::from($n) };
|
|
278 |
}
|
|
279 |
|
13887
|
280 |
/* TODO:
|
|
281 |
Distance
|
|
282 |
DistanceI
|
|
283 |
SignAs
|
|
284 |
AngleSin
|
|
285 |
AngleCos
|
|
286 |
*/
|
|
287 |
|
|
288 |
#[cfg(test)]
|
|
289 |
#[test]
|
|
290 |
fn basics() {
|
13896
|
291 |
let n = fp!(15/2);
|
13887
|
292 |
assert!(n.is_positive());
|
|
293 |
assert!(!n.is_negative());
|
|
294 |
|
|
295 |
assert!(!(-n).is_positive());
|
|
296 |
assert!((-n).is_negative());
|
|
297 |
|
|
298 |
assert_eq!(-(-n), n);
|
|
299 |
assert_eq!((-n).abs(), n);
|
13896
|
300 |
assert_eq!(-n, fp!(-15/2));
|
13887
|
301 |
|
|
302 |
assert_eq!(n.round(), 7);
|
|
303 |
assert_eq!((-n).round(), -7);
|
|
304 |
}
|
|
305 |
|
|
306 |
#[test]
|
|
307 |
fn zero() {
|
13896
|
308 |
let z = fp!(0);
|
|
309 |
let n = fp!(15/2);
|
13887
|
310 |
|
|
311 |
assert!(z.is_zero());
|
|
312 |
assert!(z.is_positive());
|
|
313 |
assert!((-z).is_negative);
|
13888
|
314 |
assert_eq!(n - n, z);
|
|
315 |
assert_eq!(-n + n, z);
|
13887
|
316 |
}
|
|
317 |
|
|
318 |
#[test]
|
13889
|
319 |
fn ord() {
|
13896
|
320 |
let z = fp!(0);
|
|
321 |
let n1_5 = fp!(3/2);
|
|
322 |
let n2_25 = fp!(9/4);
|
13889
|
323 |
|
|
324 |
assert!(!(z > z));
|
|
325 |
assert!(!(z < z));
|
|
326 |
assert!(n2_25 > n1_5);
|
|
327 |
assert!(-n2_25 < n1_5);
|
|
328 |
assert!(-n2_25 < -n1_5);
|
|
329 |
}
|
|
330 |
|
|
331 |
#[test]
|
13887
|
332 |
fn arith() {
|
13896
|
333 |
let n1_5 = fp!(3/2);
|
|
334 |
let n2_25 = fp!(9/4);
|
|
335 |
let n_0_15 = fp!(-15/100);
|
13887
|
336 |
|
13896
|
337 |
assert_eq!(n1_5 + n1_5, fp!(3));
|
|
338 |
assert_eq!(-n1_5 - n1_5, fp!(-3));
|
13887
|
339 |
|
|
340 |
assert_eq!(n1_5 * n1_5, n2_25);
|
|
341 |
assert_eq!(-n1_5 * -n1_5, n2_25);
|
|
342 |
assert_eq!(n1_5 * -n1_5, -n2_25);
|
|
343 |
assert_eq!(-n1_5 * n1_5, -n2_25);
|
|
344 |
|
13888
|
345 |
assert_eq!(-n2_25 / -n1_5, n1_5);
|
|
346 |
assert_eq!(n1_5 / -10, n_0_15);
|
|
347 |
|
13887
|
348 |
assert_eq!(n1_5.sqr(), n2_25);
|
|
349 |
assert_eq!((-n1_5).sqr(), n2_25);
|
|
350 |
|
|
351 |
assert_eq!(n2_25.sqrt(), n1_5);
|
|
352 |
|
|
353 |
assert_eq!((n1_5 * n1_5 * n1_5.sqr()).sqrt(), n2_25);
|
|
354 |
}
|