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 {
|
13948
|
11 |
#[inline]
|
13949
|
12 |
pub fn new(numerator: i32, denominator: u32) -> Self {
|
13905
|
13 |
FPNum::from(numerator) / denominator
|
|
14 |
}
|
|
15 |
|
13909
|
16 |
#[inline]
|
13949
|
17 |
pub fn signum(&self) -> i8 {
|
13905
|
18 |
if self.is_negative {
|
|
19 |
-1
|
|
20 |
} else {
|
|
21 |
1
|
|
22 |
}
|
|
23 |
}
|
|
24 |
|
13909
|
25 |
#[inline]
|
13949
|
26 |
pub fn is_negative(&self) -> bool {
|
13905
|
27 |
self.is_negative
|
|
28 |
}
|
|
29 |
|
13909
|
30 |
#[inline]
|
13949
|
31 |
pub fn is_positive(&self) -> bool {
|
13905
|
32 |
!self.is_negative
|
|
33 |
}
|
|
34 |
|
13909
|
35 |
#[inline]
|
13949
|
36 |
pub fn is_zero(&self) -> bool {
|
13905
|
37 |
self.value == 0
|
|
38 |
}
|
|
39 |
|
13909
|
40 |
#[inline]
|
13949
|
41 |
pub fn abs(&self) -> Self {
|
13905
|
42 |
Self {
|
|
43 |
is_negative: false,
|
|
44 |
value: self.value,
|
|
45 |
}
|
|
46 |
}
|
|
47 |
|
13909
|
48 |
#[inline]
|
13949
|
49 |
pub fn round(&self) -> i64 {
|
13905
|
50 |
if self.is_negative {
|
|
51 |
-((self.value >> 32) as i64)
|
|
52 |
} else {
|
|
53 |
(self.value >> 32) as i64
|
|
54 |
}
|
|
55 |
}
|
|
56 |
|
13909
|
57 |
#[inline]
|
14102
|
58 |
pub fn abs_round(&self) -> u32 {
|
|
59 |
(self.value >> 32) as u32
|
|
60 |
}
|
|
61 |
|
|
62 |
#[inline]
|
13949
|
63 |
pub fn sqr(&self) -> Self {
|
13905
|
64 |
Self {
|
|
65 |
is_negative: false,
|
|
66 |
value: ((self.value as u128).pow(2) >> 32) as u64,
|
|
67 |
}
|
|
68 |
}
|
|
69 |
|
13949
|
70 |
pub fn sqrt(&self) -> Self {
|
13905
|
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 |
}
|
13946
|
93 |
|
13948
|
94 |
#[inline]
|
13949
|
95 |
pub fn with_sign(&self, is_negative: bool) -> FPNum {
|
13946
|
96 |
FPNum{ is_negative, ..*self}
|
|
97 |
}
|
|
98 |
|
|
99 |
#[inline]
|
13949
|
100 |
pub fn with_sign_as(self, other: FPNum) -> FPNum {
|
13946
|
101 |
self.with_sign(other.is_negative)
|
|
102 |
}
|
13948
|
103 |
|
|
104 |
#[inline]
|
13949
|
105 |
pub fn point(self) -> FPPoint {
|
13948
|
106 |
FPPoint::new(self, self)
|
|
107 |
}
|
13905
|
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 {
|
13909
|
142 |
#[inline]
|
13905
|
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 {
|
13909
|
151 |
#[inline]
|
13905
|
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 {
|
13908
|
160 |
#[inline]
|
|
161 |
fn extend(n: &FPNum) -> i128 {
|
|
162 |
if n.is_negative {
|
|
163 |
-(n.value as i128)
|
13905
|
164 |
} else {
|
13908
|
165 |
n.value as i128
|
13905
|
166 |
}
|
|
167 |
}
|
13908
|
168 |
extend(self).cmp(&(extend(rhs)))
|
13905
|
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 |
|
13949
|
294 |
#[macro_export]
|
13911
|
295 |
macro_rules! fp {
|
|
296 |
(-$n: tt / $d: tt) => { FPNum::new(-$n, $d) };
|
|
297 |
($n: tt / $d: tt) => { FPNum::new($n, $d) };
|
13926
|
298 |
(-$n: tt) => { FPNum::from(-$n) };
|
13911
|
299 |
($n: tt) => { FPNum::from($n) };
|
|
300 |
}
|
|
301 |
|
13946
|
302 |
const LINEARIZE_TRESHOLD: u64 = 0x1_0000;
|
|
303 |
|
|
304 |
#[derive(Clone, Copy, Debug)]
|
13949
|
305 |
pub struct FPPoint {
|
13946
|
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]
|
13949
|
314 |
pub fn new(x: FPNum, y: FPNum) -> Self {
|
13946
|
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]
|
13949
|
324 |
pub fn zero() -> FPPoint { FPPoint::new(fp!(0), fp!(0)) }
|
13946
|
325 |
|
|
326 |
#[inline]
|
13949
|
327 |
pub fn unit_x() -> FPPoint { FPPoint::new(fp!(1), fp!(0)) }
|
13946
|
328 |
|
|
329 |
#[inline]
|
13949
|
330 |
pub fn unit_y() -> FPPoint { FPPoint::new(fp!(0), fp!(1)) }
|
13946
|
331 |
|
|
332 |
#[inline]
|
13949
|
333 |
pub fn x(&self) -> FPNum {
|
13946
|
334 |
FPNum {
|
|
335 |
is_negative: self.x_is_negative,
|
|
336 |
value: self.x_value
|
|
337 |
}
|
|
338 |
}
|
|
339 |
|
|
340 |
#[inline]
|
13949
|
341 |
pub fn y(&self) -> FPNum {
|
13946
|
342 |
FPNum {
|
|
343 |
is_negative: self.y_is_negative,
|
|
344 |
value: self.y_value
|
|
345 |
}
|
|
346 |
}
|
|
347 |
|
|
348 |
#[inline]
|
13949
|
349 |
pub fn max_norm(&self) -> FPNum {
|
13960
|
350 |
std::cmp::max(self.x().abs(), self.y().abs())
|
13949
|
351 |
}
|
|
352 |
|
|
353 |
#[inline]
|
|
354 |
pub fn sqr_distance(&self) -> FPNum {
|
13946
|
355 |
self.x().sqr() + self.y().sqr()
|
|
356 |
}
|
|
357 |
|
|
358 |
#[inline]
|
13949
|
359 |
pub fn distance(&self) -> FPNum {
|
13946
|
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]
|
13949
|
369 |
pub fn is_in_range(&self, radius: FPNum) -> bool {
|
|
370 |
self.max_norm() < radius && self.sqr_distance() < radius.sqr()
|
13946
|
371 |
}
|
|
372 |
|
|
373 |
#[inline]
|
13949
|
374 |
pub fn dot(&self, other: &FPPoint) -> FPNum {
|
13946
|
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 {
|
13948
|
398 |
($op: ty, $name: tt) => {
|
13946
|
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 |
|
13953
|
411 |
macro_rules! right_scalar_bin_op_impl {
|
|
412 |
($($op: tt)::+, $name: tt) => {
|
|
413 |
impl $($op)::+<FPNum> for FPPoint {
|
13948
|
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 |
|
13953
|
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 |
|
13946
|
439 |
bin_op_impl!(ops::Add, add);
|
|
440 |
bin_op_impl!(ops::Sub, sub);
|
|
441 |
bin_op_impl!(ops::Mul, mul);
|
13950
|
442 |
bin_op_impl!(ops::Div, div);
|
13953
|
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);
|
13946
|
448 |
|
13950
|
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 |
|
13946
|
480 |
#[inline]
|
13949
|
481 |
pub fn distance<T>(x: T, y: T) -> FPNum
|
13946
|
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 |
|
13905
|
491 |
/* TODO:
|
|
492 |
AngleSin
|
|
493 |
AngleCos
|
|
494 |
*/
|
|
495 |
|
|
496 |
#[cfg(test)]
|
|
497 |
#[test]
|
|
498 |
fn basics() {
|
13911
|
499 |
let n = fp!(15/2);
|
13905
|
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);
|
13911
|
508 |
assert_eq!(-n, fp!(-15/2));
|
13905
|
509 |
|
|
510 |
assert_eq!(n.round(), 7);
|
|
511 |
assert_eq!((-n).round(), -7);
|
|
512 |
}
|
|
513 |
|
|
514 |
#[test]
|
|
515 |
fn zero() {
|
13911
|
516 |
let z = fp!(0);
|
|
517 |
let n = fp!(15/2);
|
13905
|
518 |
|
|
519 |
assert!(z.is_zero());
|
|
520 |
assert!(z.is_positive());
|
|
521 |
assert!((-z).is_negative);
|
13907
|
522 |
assert_eq!(n - n, z);
|
|
523 |
assert_eq!(-n + n, z);
|
13947
|
524 |
assert_eq!(n.with_sign_as(-n), -n);
|
13905
|
525 |
}
|
|
526 |
|
|
527 |
#[test]
|
13908
|
528 |
fn ord() {
|
13911
|
529 |
let z = fp!(0);
|
|
530 |
let n1_5 = fp!(3/2);
|
|
531 |
let n2_25 = fp!(9/4);
|
13908
|
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]
|
13905
|
541 |
fn arith() {
|
13911
|
542 |
let n1_5 = fp!(3/2);
|
|
543 |
let n2_25 = fp!(9/4);
|
|
544 |
let n_0_15 = fp!(-15/100);
|
13905
|
545 |
|
13911
|
546 |
assert_eq!(n1_5 + n1_5, fp!(3));
|
|
547 |
assert_eq!(-n1_5 - n1_5, fp!(-3));
|
13905
|
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 |
|
13907
|
554 |
assert_eq!(-n2_25 / -n1_5, n1_5);
|
|
555 |
assert_eq!(n1_5 / -10, n_0_15);
|
|
556 |
|
13905
|
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);
|
13950
|
563 |
|
|
564 |
let mut m = fp!(1);
|
|
565 |
m += n1_5;
|
|
566 |
assert_eq!(m, fp!(5/2));
|
13905
|
567 |
}
|
13946
|
568 |
|
|
569 |
#[test]
|
|
570 |
fn point() {
|
|
571 |
let z = FPPoint::zero();
|
13953
|
572 |
let n = fp!(16/9);
|
13946
|
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));
|
13953
|
579 |
assert_eq!(n * p, p * n);
|
13946
|
580 |
assert_eq!(distance(4, 3), fp!(5));
|
13948
|
581 |
assert_eq!(p * fp!(-3), FPPoint::new(fp!(-3), fp!(6)));
|
13960
|
582 |
assert_eq!(p.max_norm(), fp!(2));
|
13946
|
583 |
} |