rust/fpnum/src/lib.rs
changeset 13953 7bb60596c27e
parent 13950 ff77c9920007
child 13960 665b4c6612ee
equal deleted inserted replaced
13952:9230aed8a32e 13953:7bb60596c27e
   401             }
   401             }
   402         }
   402         }
   403     }
   403     }
   404 }
   404 }
   405 
   405 
   406 macro_rules! scalar_bin_op_impl {
   406 macro_rules! right_scalar_bin_op_impl {
   407     ($op: ty, $name: tt) => {
   407     ($($op: tt)::+, $name: tt) => {
   408         impl $op for FPPoint {
   408         impl $($op)::+<FPNum> for FPPoint {
   409             type Output = Self;
   409             type Output = Self;
   410 
   410 
   411             #[inline]
   411             #[inline]
   412             fn $name(self, rhs: FPNum) -> Self {
   412             fn $name(self, rhs: FPNum) -> Self {
   413                 Self::new(self.x().$name(rhs),
   413                 Self::new(self.x().$name(rhs),
   415             }
   415             }
   416         }
   416         }
   417     }
   417     }
   418 }
   418 }
   419 
   419 
       
   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 
   420 bin_op_impl!(ops::Add, add);
   434 bin_op_impl!(ops::Add, add);
   421 bin_op_impl!(ops::Sub, sub);
   435 bin_op_impl!(ops::Sub, sub);
   422 bin_op_impl!(ops::Mul, mul);
   436 bin_op_impl!(ops::Mul, mul);
   423 bin_op_impl!(ops::Div, div);
   437 bin_op_impl!(ops::Div, div);
   424 scalar_bin_op_impl!(ops::Add<FPNum>, add);
   438 right_scalar_bin_op_impl!(ops::Add, add);
   425 scalar_bin_op_impl!(ops::Mul<FPNum>, mul);
   439 right_scalar_bin_op_impl!(ops::Mul, mul);
   426 scalar_bin_op_impl!(ops::Sub<FPNum>, sub);
   440 right_scalar_bin_op_impl!(ops::Sub, sub);
   427 scalar_bin_op_impl!(ops::Div<FPNum>, div);
   441 right_scalar_bin_op_impl!(ops::Div, div);
       
   442 left_scalar_bin_op_impl!(ops::Mul, mul);
   428 
   443 
   429 macro_rules! bin_assign_op_impl {
   444 macro_rules! bin_assign_op_impl {
   430     ($typ: tt, $($op: tt)::+, $name: tt, $delegate: tt) => {
   445     ($typ: tt, $($op: tt)::+, $name: tt, $delegate: tt) => {
   431         bin_assign_op_impl!($typ, $($op)::+<$typ>, $name, $delegate);
   446         bin_assign_op_impl!($typ, $($op)::+<$typ>, $name, $delegate);
   432     };
   447     };
   547 }
   562 }
   548 
   563 
   549 #[test]
   564 #[test]
   550 fn point() {
   565 fn point() {
   551     let z = FPPoint::zero();
   566     let z = FPPoint::zero();
       
   567     let n = fp!(16/9);
   552     let p = FPPoint::new(fp!(1), fp!(-2));
   568     let p = FPPoint::new(fp!(1), fp!(-2));
   553 
   569 
   554     assert_eq!(p.sqr_distance(), fp!(5));
   570     assert_eq!(p.sqr_distance(), fp!(5));
   555     assert_eq!(p + -p, FPPoint::zero());
   571     assert_eq!(p + -p, FPPoint::zero());
   556     assert_eq!(p * z, z);
   572     assert_eq!(p * z, z);
   557     assert_eq!(p.dot(&z), fp!(0));
   573     assert_eq!(p.dot(&z), fp!(0));
       
   574     assert_eq!(n * p, p * n);
   558     assert_eq!(distance(4, 3), fp!(5));
   575     assert_eq!(distance(4, 3), fp!(5));
   559     assert_eq!(p * fp!(-3), FPPoint::new(fp!(-3), fp!(6)));
   576     assert_eq!(p * fp!(-3), FPPoint::new(fp!(-3), fp!(6)));
   560 }
   577 }