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