rust/fpnum/src/lib.rs
changeset 13909 cd39e87d7a80
parent 13908 fbbb4fcd6a75
child 13911 9ae1184886db
equal deleted inserted replaced
13908:fbbb4fcd6a75 13909:cd39e87d7a80
    10 impl FPNum {
    10 impl FPNum {
    11     fn new(numerator: i32, denominator: u32) -> Self {
    11     fn new(numerator: i32, denominator: u32) -> Self {
    12         FPNum::from(numerator) / denominator
    12         FPNum::from(numerator) / denominator
    13     }
    13     }
    14 
    14 
       
    15     #[inline]
    15     fn signum(&self) -> i8 {
    16     fn signum(&self) -> i8 {
    16         if self.is_negative {
    17         if self.is_negative {
    17             -1
    18             -1
    18         } else {
    19         } else {
    19             1
    20             1
    20         }
    21         }
    21     }
    22     }
    22 
    23 
       
    24     #[inline]
    23     fn is_negative(&self) -> bool {
    25     fn is_negative(&self) -> bool {
    24         self.is_negative
    26         self.is_negative
    25     }
    27     }
    26 
    28 
       
    29     #[inline]
    27     fn is_positive(&self) -> bool {
    30     fn is_positive(&self) -> bool {
    28         !self.is_negative
    31         !self.is_negative
    29     }
    32     }
    30 
    33 
       
    34     #[inline]
    31     fn is_zero(&self) -> bool {
    35     fn is_zero(&self) -> bool {
    32         self.value == 0
    36         self.value == 0
    33     }
    37     }
    34 
    38 
       
    39     #[inline]
    35     fn abs(&self) -> Self {
    40     fn abs(&self) -> Self {
    36         Self {
    41         Self {
    37             is_negative: false,
    42             is_negative: false,
    38             value: self.value,
    43             value: self.value,
    39         }
    44         }
    40     }
    45     }
    41 
    46 
       
    47     #[inline]
    42     fn round(&self) -> i64 {
    48     fn round(&self) -> i64 {
    43         if self.is_negative {
    49         if self.is_negative {
    44             -((self.value >> 32) as i64)
    50             -((self.value >> 32) as i64)
    45         } else {
    51         } else {
    46             (self.value >> 32) as i64
    52             (self.value >> 32) as i64
    47         }
    53         }
    48     }
    54     }
    49 
    55 
       
    56     #[inline]
    50     fn sqr(&self) -> Self {
    57     fn sqr(&self) -> Self {
    51         Self {
    58         Self {
    52             is_negative: false,
    59             is_negative: false,
    53             value: ((self.value as u128).pow(2) >> 32) as u64,
    60             value: ((self.value as u128).pow(2) >> 32) as u64,
    54         }
    61         }
   109         }
   116         }
   110     }
   117     }
   111 }
   118 }
   112 
   119 
   113 impl PartialEq for FPNum {
   120 impl PartialEq for FPNum {
       
   121     #[inline]
   114     fn eq(&self, other: &Self) -> bool {
   122     fn eq(&self, other: &Self) -> bool {
   115         self.value == other.value && (self.is_negative == other.is_negative || self.value == 0)
   123         self.value == other.value && (self.is_negative == other.is_negative || self.value == 0)
   116     }
   124     }
   117 }
   125 }
   118 
   126 
   119 impl Eq for FPNum {}
   127 impl Eq for FPNum {}
   120 
   128 
   121 impl PartialOrd for FPNum {
   129 impl PartialOrd for FPNum {
       
   130     #[inline]
   122     fn partial_cmp(&self, rhs: &Self) -> std::option::Option<std::cmp::Ordering> {
   131     fn partial_cmp(&self, rhs: &Self) -> std::option::Option<std::cmp::Ordering> {
   123         Some(self.cmp(rhs))
   132         Some(self.cmp(rhs))
   124     }
   133     }
   125 }
   134 }
   126 
   135