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 {
|
13932
|
11 |
#[inline]
|
13933
|
12 |
pub fn new(numerator: i32, denominator: u32) -> Self {
|
13887
|
13 |
FPNum::from(numerator) / denominator
|
|
14 |
}
|
|
15 |
|
13890
|
16 |
#[inline]
|
13933
|
17 |
pub fn signum(&self) -> i8 {
|
13887
|
18 |
if self.is_negative {
|
|
19 |
-1
|
|
20 |
} else {
|
|
21 |
1
|
|
22 |
}
|
|
23 |
}
|
|
24 |
|
13890
|
25 |
#[inline]
|
13933
|
26 |
pub fn is_negative(&self) -> bool {
|
13887
|
27 |
self.is_negative
|
|
28 |
}
|
|
29 |
|
13890
|
30 |
#[inline]
|
13933
|
31 |
pub fn is_positive(&self) -> bool {
|
13887
|
32 |
!self.is_negative
|
|
33 |
}
|
|
34 |
|
13890
|
35 |
#[inline]
|
13933
|
36 |
pub fn is_zero(&self) -> bool {
|
13887
|
37 |
self.value == 0
|
|
38 |
}
|
|
39 |
|
13890
|
40 |
#[inline]
|
13933
|
41 |
pub fn abs(&self) -> Self {
|
13887
|
42 |
Self {
|
|
43 |
is_negative: false,
|
|
44 |
value: self.value,
|
|
45 |
}
|
|
46 |
}
|
|
47 |
|
13890
|
48 |
#[inline]
|
13933
|
49 |
pub fn round(&self) -> i64 {
|
13887
|
50 |
if self.is_negative {
|
|
51 |
-((self.value >> 32) as i64)
|
|
52 |
} else {
|
|
53 |
(self.value >> 32) as i64
|
|
54 |
}
|
|
55 |
}
|
|
56 |
|
13890
|
57 |
#[inline]
|
14086
|
58 |
pub fn abs_round(&self) -> u32 {
|
|
59 |
(self.value >> 32) as u32
|
|
60 |
}
|
|
61 |
|
|
62 |
#[inline]
|
13933
|
63 |
pub fn sqr(&self) -> Self {
|
13887
|
64 |
Self {
|
|
65 |
is_negative: false,
|
|
66 |
value: ((self.value as u128).pow(2) >> 32) as u64,
|
|
67 |
}
|
|
68 |
}
|
|
69 |
|
13933
|
70 |
pub fn sqrt(&self) -> Self {
|
13887
|
71 |
debug_assert!(!self.is_negative);
|
|
72 |
|
|
73 |
let mut t: u64 = 0x4000000000000000;
|
|
74 |
let mut r: u64 = 0;
|
|
75 |
let mut q = self.value;
|
|
76 |
|
|
77 |
for _ in 0..32 {
|
|
78 |
let s = r + t;
|
|
79 |
r >>= 1;
|
|
80 |
|
|
81 |
if s <= q {
|
|
82 |
q -= s;
|
|
83 |
r += t;
|
|
84 |
}
|
|
85 |
t >>= 2;
|
|
86 |
}
|
|
87 |
|
|
88 |
Self {
|
|
89 |
is_negative: false,
|
|
90 |
value: r << 16,
|
|
91 |
}
|
|
92 |
}
|
13930
|
93 |
|
13932
|
94 |
#[inline]
|
13933
|
95 |
pub fn with_sign(&self, is_negative: bool) -> FPNum {
|
13930
|
96 |
FPNum{ is_negative, ..*self}
|
|
97 |
}
|
|
98 |
|
|
99 |
#[inline]
|
13933
|
100 |
pub fn with_sign_as(self, other: FPNum) -> FPNum {
|
13930
|
101 |
self.with_sign(other.is_negative)
|
|
102 |
}
|
13932
|
103 |
|
|
104 |
#[inline]
|
13933
|
105 |
pub fn point(self) -> FPPoint {
|
13932
|
106 |
FPPoint::new(self, self)
|
|
107 |
}
|
13887
|
108 |
}
|
|
109 |
|
|
110 |
impl From<i32> for FPNum {
|
|
111 |
#[inline]
|
|
112 |
fn from(n: i32) -> Self {
|
|
113 |
FPNum {
|
|
114 |
is_negative: n < 0,
|
|
115 |
value: (n.abs() as u64) << 32,
|
|
116 |
}
|
|
117 |
}
|
|
118 |
}
|
|
119 |
|
|
120 |
impl From<u32> for FPNum {
|
|
121 |
#[inline]
|
|
122 |
fn from(n: u32) -> Self {
|
|
123 |
Self {
|
|
124 |
is_negative: false,
|
|
125 |
value: (n as u64) << 32,
|
|
126 |
}
|
|
127 |
}
|
|
128 |
}
|
|
129 |
|
|
130 |
impl From<FPNum> for f64 {
|
|
131 |
#[inline]
|
|
132 |
fn from(n: FPNum) -> Self {
|
|
133 |
if n.is_negative {
|
|
134 |
n.value as f64 / (-0x10000000 as f64)
|
|
135 |
} else {
|
|
136 |
n.value as f64 / 0x10000000 as f64
|
|
137 |
}
|
|
138 |
}
|
|
139 |
}
|
|
140 |
|
|
141 |
impl PartialEq for FPNum {
|
13890
|
142 |
#[inline]
|
13887
|
143 |
fn eq(&self, other: &Self) -> bool {
|
|
144 |
self.value == other.value && (self.is_negative == other.is_negative || self.value == 0)
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
impl Eq for FPNum {}
|
|
149 |
|
|
150 |
impl PartialOrd for FPNum {
|
13890
|
151 |
#[inline]
|
13887
|
152 |
fn partial_cmp(&self, rhs: &Self) -> std::option::Option<std::cmp::Ordering> {
|
|
153 |
Some(self.cmp(rhs))
|
|
154 |
}
|
|
155 |
}
|
|
156 |
|
|
157 |
impl Ord for FPNum {
|
|
158 |
#[inline]
|
|
159 |
fn cmp(&self, rhs: &Self) -> cmp::Ordering {
|
13889
|
160 |
#[inline]
|
|
161 |
fn extend(n: &FPNum) -> i128 {
|
|
162 |
if n.is_negative {
|
|
163 |
-(n.value as i128)
|
13887
|
164 |
} else {
|
13889
|
165 |
n.value as i128
|
13887
|
166 |
}
|
|
167 |
}
|
13889
|
168 |
extend(self).cmp(&(extend(rhs)))
|
13887
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
impl ops::Add for FPNum {
|
|
173 |
type Output = Self;
|
|
174 |
|
|
175 |
#[inline]
|
|
176 |
fn add(self, rhs: Self) -> Self {
|
|
177 |
if self.is_negative == rhs.is_negative {
|
|
178 |
Self {
|
|
179 |
is_negative: self.is_negative,
|
|
180 |
value: self.value + rhs.value,
|
|
181 |
}
|
|
182 |
} else if self.value > rhs.value {
|
|
183 |
Self {
|
|
184 |
is_negative: self.is_negative,
|
|
185 |
value: self.value - rhs.value,
|
|
186 |
}
|
|
187 |
} else {
|
|
188 |
Self {
|
|
189 |
is_negative: rhs.is_negative,
|
|
190 |
value: rhs.value - self.value,
|
|
191 |
}
|
|
192 |
}
|
|
193 |
}
|
|
194 |
}
|
|
195 |
|
|
196 |
impl ops::Sub for FPNum {
|
|
197 |
type Output = Self;
|
|
198 |
|
|
199 |
#[inline]
|
|
200 |
fn sub(self, rhs: Self) -> Self {
|
|
201 |
if self.is_negative == rhs.is_negative {
|
|
202 |
if self.value > rhs.value {
|
|
203 |
Self {
|
|
204 |
is_negative: self.is_negative,
|
|
205 |
value: self.value - rhs.value,
|
|
206 |
}
|
|
207 |
} else {
|
|
208 |
Self {
|
|
209 |
is_negative: !rhs.is_negative,
|
|
210 |
value: rhs.value - self.value,
|
|
211 |
}
|
|
212 |
}
|
|
213 |
} else {
|
|
214 |
Self {
|
|
215 |
is_negative: self.is_negative,
|
|
216 |
value: self.value + rhs.value,
|
|
217 |
}
|
|
218 |
}
|
|
219 |
}
|
|
220 |
}
|
|
221 |
|
|
222 |
impl ops::Neg for FPNum {
|
|
223 |
type Output = Self;
|
|
224 |
|
|
225 |
#[inline]
|
|
226 |
fn neg(self) -> Self {
|
|
227 |
Self {
|
|
228 |
is_negative: !self.is_negative,
|
|
229 |
value: self.value,
|
|
230 |
}
|
|
231 |
}
|
|
232 |
}
|
|
233 |
|
|
234 |
impl ops::Mul for FPNum {
|
|
235 |
type Output = Self;
|
|
236 |
|
|
237 |
#[inline]
|
|
238 |
fn mul(self, rhs: Self) -> Self {
|
|
239 |
Self {
|
|
240 |
is_negative: self.is_negative ^ rhs.is_negative,
|
|
241 |
value: ((self.value as u128 * rhs.value as u128) >> 32) as u64,
|
|
242 |
}
|
|
243 |
}
|
|
244 |
}
|
|
245 |
|
|
246 |
impl ops::Mul<i32> for FPNum {
|
|
247 |
type Output = Self;
|
|
248 |
|
|
249 |
#[inline]
|
|
250 |
fn mul(self, rhs: i32) -> Self {
|
|
251 |
Self {
|
|
252 |
is_negative: self.is_negative ^ (rhs < 0),
|
|
253 |
value: self.value * rhs.abs() as u64,
|
|
254 |
}
|
|
255 |
}
|
|
256 |
}
|
|
257 |
|
|
258 |
impl ops::Div for FPNum {
|
|
259 |
type Output = Self;
|
|
260 |
|
|
261 |
#[inline]
|
|
262 |
fn div(self, rhs: Self) -> Self {
|
|
263 |
Self {
|
|
264 |
is_negative: self.is_negative ^ rhs.is_negative,
|
|
265 |
value: (((self.value as u128) << 32) / rhs.value as u128) as u64,
|
|
266 |
}
|
|
267 |
}
|
|
268 |
}
|
|
269 |
|
|
270 |
impl ops::Div<i32> for FPNum {
|
|
271 |
type Output = Self;
|
|
272 |
|
|
273 |
#[inline]
|
|
274 |
fn div(self, rhs: i32) -> Self {
|
|
275 |
Self {
|
|
276 |
is_negative: self.is_negative ^ (rhs < 0),
|
|
277 |
value: self.value / rhs.abs() as u64,
|
|
278 |
}
|
|
279 |
}
|
|
280 |
}
|
|
281 |
|
|
282 |
impl ops::Div<u32> for FPNum {
|
|
283 |
type Output = Self;
|
|
284 |
|
|
285 |
#[inline]
|
|
286 |
fn div(self, rhs: u32) -> Self {
|
|
287 |
Self {
|
|
288 |
is_negative: self.is_negative,
|
|
289 |
value: self.value / rhs as u64,
|
|
290 |
}
|
|
291 |
}
|
|
292 |
}
|
|
293 |
|
13933
|
294 |
#[macro_export]
|
13896
|
295 |
macro_rules! fp {
|
|
296 |
(-$n: tt / $d: tt) => { FPNum::new(-$n, $d) };
|
|
297 |
($n: tt / $d: tt) => { FPNum::new($n, $d) };
|
13910
|
298 |
(-$n: tt) => { FPNum::from(-$n) };
|
13896
|
299 |
($n: tt) => { FPNum::from($n) };
|
|
300 |
}
|
|
301 |
|
13930
|
302 |
const LINEARIZE_TRESHOLD: u64 = 0x1_0000;
|
|
303 |
|
|
304 |
#[derive(Clone, Copy, Debug)]
|
13933
|
305 |
pub struct FPPoint {
|
13930
|
306 |
x_is_negative: bool,
|
|
307 |
y_is_negative: bool,
|
|
308 |
x_value: u64,
|
|
309 |
y_value: u64,
|
|
310 |
}
|
|
311 |
|
|
312 |
impl FPPoint {
|
|
313 |
#[inline]
|
13933
|
314 |
pub fn new(x: FPNum, y: FPNum) -> Self {
|
13930
|
315 |
Self {
|
|
316 |
x_is_negative: x.is_negative,
|
|
317 |
y_is_negative: y.is_negative,
|
|
318 |
x_value: x.value,
|
|
319 |
y_value: y.value
|
|
320 |
}
|
|
321 |
}
|
|
322 |
|
|
323 |
#[inline]
|
13933
|
324 |
pub fn zero() -> FPPoint { FPPoint::new(fp!(0), fp!(0)) }
|
13930
|
325 |
|
|
326 |
#[inline]
|
13933
|
327 |
pub fn unit_x() -> FPPoint { FPPoint::new(fp!(1), fp!(0)) }
|
13930
|
328 |
|
|
329 |
#[inline]
|
13933
|
330 |
pub fn unit_y() -> FPPoint { FPPoint::new(fp!(0), fp!(1)) }
|
13930
|
331 |
|
|
332 |
#[inline]
|
13933
|
333 |
pub fn x(&self) -> FPNum {
|
13930
|
334 |
FPNum {
|
|
335 |
is_negative: self.x_is_negative,
|
|
336 |
value: self.x_value
|
|
337 |
}
|
|
338 |
}
|
|
339 |
|
|
340 |
#[inline]
|
13933
|
341 |
pub fn y(&self) -> FPNum {
|
13930
|
342 |
FPNum {
|
|
343 |
is_negative: self.y_is_negative,
|
|
344 |
value: self.y_value
|
|
345 |
}
|
|
346 |
}
|
|
347 |
|
|
348 |
#[inline]
|
13933
|
349 |
pub fn max_norm(&self) -> FPNum {
|
13944
|
350 |
std::cmp::max(self.x().abs(), self.y().abs())
|
13933
|
351 |
}
|
|
352 |
|
|
353 |
#[inline]
|
|
354 |
pub fn sqr_distance(&self) -> FPNum {
|
13930
|
355 |
self.x().sqr() + self.y().sqr()
|
|
356 |
}
|
|
357 |
|
|
358 |
#[inline]
|
13933
|
359 |
pub fn distance(&self) -> FPNum {
|
13930
|
360 |
let r = self.x_value | self.y_value;
|
|
361 |
if r < LINEARIZE_TRESHOLD {
|
|
362 |
FPNum::from(r as u32)
|
|
363 |
} else {
|
|
364 |
self.sqr_distance().sqrt()
|
|
365 |
}
|
|
366 |
}
|
|
367 |
|
|
368 |
#[inline]
|
13933
|
369 |
pub fn is_in_range(&self, radius: FPNum) -> bool {
|
|
370 |
self.max_norm() < radius && self.sqr_distance() < radius.sqr()
|
13930
|
371 |
}
|
|
372 |
|
|
373 |
#[inline]
|
13933
|
374 |
pub fn dot(&self, other: &FPPoint) -> FPNum {
|
13930
|
375 |
self.x() * other.x() + self.y() * other.y()
|
|
376 |
}
|
|
377 |
}
|
|
378 |
|
|
379 |
impl PartialEq for FPPoint {
|
|
380 |
#[inline]
|
|
381 |
fn eq(&self, other: &Self) -> bool {
|
|
382 |
self.x() == other.x() && self.y() == other.y()
|
|
383 |
}
|
|
384 |
}
|
|
385 |
|
|
386 |
impl Eq for FPPoint {}
|
|
387 |
|
|
388 |
impl ops::Neg for FPPoint {
|
|
389 |
type Output = Self;
|
|
390 |
|
|
391 |
#[inline]
|
|
392 |
fn neg(self) -> Self {
|
|
393 |
Self::new(-self.x(), -self.y())
|
|
394 |
}
|
|
395 |
}
|
|
396 |
|
|
397 |
macro_rules! bin_op_impl {
|
13932
|
398 |
($op: ty, $name: tt) => {
|
13930
|
399 |
impl $op for FPPoint {
|
|
400 |
type Output = Self;
|
|
401 |
|
|
402 |
#[inline]
|
|
403 |
fn $name(self, rhs: Self) -> Self {
|
|
404 |
Self::new(self.x().$name(rhs.x()),
|
|
405 |
self.y().$name(rhs.y()))
|
|
406 |
}
|
|
407 |
}
|
|
408 |
}
|
|
409 |
}
|
|
410 |
|
13937
|
411 |
macro_rules! right_scalar_bin_op_impl {
|
|
412 |
($($op: tt)::+, $name: tt) => {
|
|
413 |
impl $($op)::+<FPNum> for FPPoint {
|
13932
|
414 |
type Output = Self;
|
|
415 |
|
|
416 |
#[inline]
|
|
417 |
fn $name(self, rhs: FPNum) -> Self {
|
|
418 |
Self::new(self.x().$name(rhs),
|
|
419 |
self.y().$name(rhs))
|
|
420 |
}
|
|
421 |
}
|
|
422 |
}
|
|
423 |
}
|
|
424 |
|
13937
|
425 |
macro_rules! left_scalar_bin_op_impl {
|
|
426 |
($($op: tt)::+, $name: tt) => {
|
|
427 |
impl $($op)::+<FPPoint> for FPNum {
|
|
428 |
type Output = FPPoint;
|
|
429 |
|
|
430 |
#[inline]
|
|
431 |
fn $name(self, rhs: FPPoint) -> Self::Output {
|
|
432 |
Self::Output::new(self.$name(rhs.x()),
|
|
433 |
self.$name(rhs.y()))
|
|
434 |
}
|
|
435 |
}
|
|
436 |
}
|
|
437 |
}
|
|
438 |
|
13930
|
439 |
bin_op_impl!(ops::Add, add);
|
|
440 |
bin_op_impl!(ops::Sub, sub);
|
|
441 |
bin_op_impl!(ops::Mul, mul);
|
13934
|
442 |
bin_op_impl!(ops::Div, div);
|
13937
|
443 |
right_scalar_bin_op_impl!(ops::Add, add);
|
|
444 |
right_scalar_bin_op_impl!(ops::Mul, mul);
|
|
445 |
right_scalar_bin_op_impl!(ops::Sub, sub);
|
|
446 |
right_scalar_bin_op_impl!(ops::Div, div);
|
|
447 |
left_scalar_bin_op_impl!(ops::Mul, mul);
|
13930
|
448 |
|
13934
|
449 |
macro_rules! bin_assign_op_impl {
|
|
450 |
($typ: tt, $($op: tt)::+, $name: tt, $delegate: tt) => {
|
|
451 |
bin_assign_op_impl!($typ, $($op)::+<$typ>, $name, $delegate);
|
|
452 |
};
|
|
453 |
($typ: tt, $($op: tt)::+<$arg: tt>, $name: tt, $delegate: tt) => {
|
|
454 |
impl $($op)::+<$arg> for $typ {
|
|
455 |
#[inline]
|
|
456 |
fn $name(&mut self, rhs: $arg) {
|
|
457 |
*self = *self $delegate rhs;
|
|
458 |
}
|
|
459 |
}
|
|
460 |
}
|
|
461 |
}
|
|
462 |
|
|
463 |
bin_assign_op_impl!(FPNum, ops::AddAssign, add_assign, +);
|
|
464 |
bin_assign_op_impl!(FPNum, ops::SubAssign, sub_assign, -);
|
|
465 |
bin_assign_op_impl!(FPNum, ops::MulAssign, mul_assign, *);
|
|
466 |
bin_assign_op_impl!(FPNum, ops::DivAssign, div_assign, /);
|
|
467 |
bin_assign_op_impl!(FPNum, ops::MulAssign<i32>, mul_assign, *);
|
|
468 |
bin_assign_op_impl!(FPNum, ops::DivAssign<i32>, div_assign, /);
|
|
469 |
bin_assign_op_impl!(FPNum, ops::DivAssign<u32>, div_assign, /);
|
|
470 |
|
|
471 |
bin_assign_op_impl!(FPPoint, ops::AddAssign, add_assign, +);
|
|
472 |
bin_assign_op_impl!(FPPoint, ops::SubAssign, sub_assign, -);
|
|
473 |
bin_assign_op_impl!(FPPoint, ops::MulAssign, mul_assign, *);
|
|
474 |
bin_assign_op_impl!(FPPoint, ops::DivAssign, div_assign, /);
|
|
475 |
bin_assign_op_impl!(FPPoint, ops::AddAssign<FPNum>, add_assign, +);
|
|
476 |
bin_assign_op_impl!(FPPoint, ops::SubAssign<FPNum>, sub_assign, -);
|
|
477 |
bin_assign_op_impl!(FPPoint, ops::MulAssign<FPNum>, mul_assign, *);
|
|
478 |
bin_assign_op_impl!(FPPoint, ops::DivAssign<FPNum>, div_assign, /);
|
|
479 |
|
13930
|
480 |
#[inline]
|
13933
|
481 |
pub fn distance<T>(x: T, y: T) -> FPNum
|
13930
|
482 |
where T: ops::Add + ops::Mul + Copy +
|
|
483 |
From<<T as ops::Add>::Output> +
|
|
484 |
From<<T as ops::Mul>::Output> +
|
|
485 |
Into<FPNum>
|
|
486 |
{
|
|
487 |
let sqr: FPNum = T::from(T::from(x * x) + T::from(y * y)).into();
|
|
488 |
sqr.sqrt()
|
|
489 |
}
|
|
490 |
|
13887
|
491 |
/* TODO:
|
|
492 |
AngleSin
|
|
493 |
AngleCos
|
|
494 |
*/
|
|
495 |
|
|
496 |
#[cfg(test)]
|
|
497 |
#[test]
|
|
498 |
fn basics() {
|
13896
|
499 |
let n = fp!(15/2);
|
13887
|
500 |
assert!(n.is_positive());
|
|
501 |
assert!(!n.is_negative());
|
|
502 |
|
|
503 |
assert!(!(-n).is_positive());
|
|
504 |
assert!((-n).is_negative());
|
|
505 |
|
|
506 |
assert_eq!(-(-n), n);
|
|
507 |
assert_eq!((-n).abs(), n);
|
13896
|
508 |
assert_eq!(-n, fp!(-15/2));
|
13887
|
509 |
|
|
510 |
assert_eq!(n.round(), 7);
|
|
511 |
assert_eq!((-n).round(), -7);
|
|
512 |
}
|
|
513 |
|
|
514 |
#[test]
|
|
515 |
fn zero() {
|
13896
|
516 |
let z = fp!(0);
|
|
517 |
let n = fp!(15/2);
|
13887
|
518 |
|
|
519 |
assert!(z.is_zero());
|
|
520 |
assert!(z.is_positive());
|
|
521 |
assert!((-z).is_negative);
|
13888
|
522 |
assert_eq!(n - n, z);
|
|
523 |
assert_eq!(-n + n, z);
|
13931
|
524 |
assert_eq!(n.with_sign_as(-n), -n);
|
13887
|
525 |
}
|
|
526 |
|
|
527 |
#[test]
|
13889
|
528 |
fn ord() {
|
13896
|
529 |
let z = fp!(0);
|
|
530 |
let n1_5 = fp!(3/2);
|
|
531 |
let n2_25 = fp!(9/4);
|
13889
|
532 |
|
|
533 |
assert!(!(z > z));
|
|
534 |
assert!(!(z < z));
|
|
535 |
assert!(n2_25 > n1_5);
|
|
536 |
assert!(-n2_25 < n1_5);
|
|
537 |
assert!(-n2_25 < -n1_5);
|
|
538 |
}
|
|
539 |
|
|
540 |
#[test]
|
13887
|
541 |
fn arith() {
|
13896
|
542 |
let n1_5 = fp!(3/2);
|
|
543 |
let n2_25 = fp!(9/4);
|
|
544 |
let n_0_15 = fp!(-15/100);
|
13887
|
545 |
|
13896
|
546 |
assert_eq!(n1_5 + n1_5, fp!(3));
|
|
547 |
assert_eq!(-n1_5 - n1_5, fp!(-3));
|
13887
|
548 |
|
|
549 |
assert_eq!(n1_5 * n1_5, n2_25);
|
|
550 |
assert_eq!(-n1_5 * -n1_5, n2_25);
|
|
551 |
assert_eq!(n1_5 * -n1_5, -n2_25);
|
|
552 |
assert_eq!(-n1_5 * n1_5, -n2_25);
|
|
553 |
|
13888
|
554 |
assert_eq!(-n2_25 / -n1_5, n1_5);
|
|
555 |
assert_eq!(n1_5 / -10, n_0_15);
|
|
556 |
|
13887
|
557 |
assert_eq!(n1_5.sqr(), n2_25);
|
|
558 |
assert_eq!((-n1_5).sqr(), n2_25);
|
|
559 |
|
|
560 |
assert_eq!(n2_25.sqrt(), n1_5);
|
|
561 |
|
|
562 |
assert_eq!((n1_5 * n1_5 * n1_5.sqr()).sqrt(), n2_25);
|
13934
|
563 |
|
|
564 |
let mut m = fp!(1);
|
|
565 |
m += n1_5;
|
|
566 |
assert_eq!(m, fp!(5/2));
|
13887
|
567 |
}
|
13930
|
568 |
|
|
569 |
#[test]
|
|
570 |
fn point() {
|
|
571 |
let z = FPPoint::zero();
|
13937
|
572 |
let n = fp!(16/9);
|
13930
|
573 |
let p = FPPoint::new(fp!(1), fp!(-2));
|
|
574 |
|
|
575 |
assert_eq!(p.sqr_distance(), fp!(5));
|
|
576 |
assert_eq!(p + -p, FPPoint::zero());
|
|
577 |
assert_eq!(p * z, z);
|
|
578 |
assert_eq!(p.dot(&z), fp!(0));
|
13937
|
579 |
assert_eq!(n * p, p * n);
|
13930
|
580 |
assert_eq!(distance(4, 3), fp!(5));
|
13932
|
581 |
assert_eq!(p * fp!(-3), FPPoint::new(fp!(-3), fp!(6)));
|
13944
|
582 |
assert_eq!(p.max_norm(), fp!(2));
|
13930
|
583 |
} |