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