rust/fpnum/src/lib.rs
changeset 13927 cf28d7a2b7fe
parent 13926 2717a5289d88
child 13928 5fdc41cd0841
equal deleted inserted replaced
13926:2717a5289d88 13927:cf28d7a2b7fe
     6     is_negative: bool,
     6     is_negative: bool,
     7     value: u64,
     7     value: u64,
     8 }
     8 }
     9 
     9 
    10 impl FPNum {
    10 impl FPNum {
       
    11     #[inline]
    11     fn new(numerator: i32, denominator: u32) -> Self {
    12     fn new(numerator: i32, denominator: u32) -> Self {
    12         FPNum::from(numerator) / denominator
    13         FPNum::from(numerator) / denominator
    13     }
    14     }
    14 
    15 
    15     #[inline]
    16     #[inline]
    83             is_negative: false,
    84             is_negative: false,
    84             value: r << 16,
    85             value: r << 16,
    85         }
    86         }
    86     }
    87     }
    87 
    88 
       
    89     #[inline]
    88     fn with_sign(&self, is_negative: bool) -> FPNum {
    90     fn with_sign(&self, is_negative: bool) -> FPNum {
    89         FPNum{ is_negative, ..*self}
    91         FPNum{ is_negative, ..*self}
    90     }
    92     }
    91 
    93 
    92     #[inline]
    94     #[inline]
    93     fn with_sign_as(self, other: FPNum) -> FPNum {
    95     fn with_sign_as(self, other: FPNum) -> FPNum {
    94         self.with_sign(other.is_negative)
    96         self.with_sign(other.is_negative)
       
    97     }
       
    98 
       
    99     #[inline]
       
   100     fn point(self) -> FPPoint {
       
   101         FPPoint::new(self, self)
    95     }
   102     }
    96 }
   103 }
    97 
   104 
    98 impl From<i32> for FPNum {
   105 impl From<i32> for FPNum {
    99     #[inline]
   106     #[inline]
   376         Self::new(-self.x(), -self.y())
   383         Self::new(-self.x(), -self.y())
   377     }
   384     }
   378 }
   385 }
   379 
   386 
   380 macro_rules! bin_op_impl {
   387 macro_rules! bin_op_impl {
   381     ($op: path, $name: tt) => {
   388     ($op: ty, $name: tt) => {
   382         impl $op for FPPoint {
   389         impl $op for FPPoint {
   383             type Output = Self;
   390             type Output = Self;
   384 
   391 
   385             #[inline]
   392             #[inline]
   386             fn $name(self, rhs: Self) -> Self {
   393             fn $name(self, rhs: Self) -> Self {
   389             }
   396             }
   390         }
   397         }
   391     }
   398     }
   392 }
   399 }
   393 
   400 
       
   401 macro_rules! scalar_bin_op_impl {
       
   402     ($op: ty, $name: tt) => {
       
   403         impl $op for FPPoint {
       
   404             type Output = Self;
       
   405 
       
   406             #[inline]
       
   407             fn $name(self, rhs: FPNum) -> Self {
       
   408                 Self::new(self.x().$name(rhs),
       
   409                           self.y().$name(rhs))
       
   410             }
       
   411         }
       
   412     }
       
   413 }
       
   414 
   394 bin_op_impl!(ops::Add, add);
   415 bin_op_impl!(ops::Add, add);
   395 bin_op_impl!(ops::Sub, sub);
   416 bin_op_impl!(ops::Sub, sub);
   396 bin_op_impl!(ops::Mul, mul);
   417 bin_op_impl!(ops::Mul, mul);
       
   418 scalar_bin_op_impl!(ops::Add<FPNum>, add);
       
   419 scalar_bin_op_impl!(ops::Mul<FPNum>, mul);
       
   420 scalar_bin_op_impl!(ops::Sub<FPNum>, sub);
       
   421 scalar_bin_op_impl!(ops::Div<FPNum>, div);
   397 
   422 
   398 #[inline]
   423 #[inline]
   399 fn distance<T>(x: T, y: T) -> FPNum
   424 fn distance<T>(x: T, y: T) -> FPNum
   400     where T: ops::Add + ops::Mul + Copy +
   425     where T: ops::Add + ops::Mul + Copy +
   401              From<<T as ops::Add>::Output> +
   426              From<<T as ops::Add>::Output> +
   488     assert_eq!(p.sqr_distance(), fp!(5));
   513     assert_eq!(p.sqr_distance(), fp!(5));
   489     assert_eq!(p + -p, FPPoint::zero());
   514     assert_eq!(p + -p, FPPoint::zero());
   490     assert_eq!(p * z, z);
   515     assert_eq!(p * z, z);
   491     assert_eq!(p.dot(&z), fp!(0));
   516     assert_eq!(p.dot(&z), fp!(0));
   492     assert_eq!(distance(4, 3), fp!(5));
   517     assert_eq!(distance(4, 3), fp!(5));
   493 }
   518     assert_eq!(p * fp!(-3), FPPoint::new(fp!(-3), fp!(6)));
       
   519 }