fast_posit/posit/
unary.rs

1use super::*;
2
3impl<
4  const N: u32,
5  const ES: u32,
6  Int: crate::Int,
7> Posit<N, ES, Int> {
8  /// Returns the posit value of the lexicographic successor of `self`'s representation.
9  ///
10  /// **Standard:** "next".
11  pub fn next(self) -> Self {
12    Self::from_bits(self.0.wrapping_add(Int::ONE))
13  }
14
15  /// Returns the posit value of the lexicographic predecessor of `self`'s representation.
16  ///
17  /// **Standard:** "prior".
18  pub fn prior(self) -> Self {
19    Self::from_bits(self.0.wrapping_sub(Int::ONE))
20  }
21}
22
23impl<const N: u32,const ES: u32,Int: crate::Int>
24core::ops::Neg for Posit<N, ES, Int> {
25  type Output = Posit<N, ES, Int>;
26
27  fn neg(self) -> Self::Output {
28    Posit::from_bits(self.0.wrapping_neg())
29  }
30}
31
32impl<const N: u32,const ES: u32,Int: crate::Int>
33core::ops::Neg for &Posit<N, ES, Int> {
34  type Output = Posit<N, ES, Int>;
35
36  fn neg(self) -> Self::Output {
37    Posit::from_bits(self.0.wrapping_neg())
38  }
39}
40
41#[cfg(test)]
42mod tests {
43  use super::*;
44  use malachite::rational::Rational;
45
46  mod neg {
47    use super::*;
48
49    #[test]
50    fn p8() {
51      assert_eq!(-crate::p8::ZERO, crate::p8::ZERO);
52      assert_eq!(-crate::p8::NAR, crate::p8::NAR);
53      for p in crate::p8::cases_exhaustive() {
54        assert_eq!(Rational::try_from(-p).unwrap(), -Rational::try_from(p).unwrap())
55      }
56    }
57
58    #[test]
59    fn posit_10_1() {
60      assert_eq!(-Posit::<10, 0, i16>::ZERO, Posit::<10, 0, i16>::ZERO);
61      assert_eq!(-Posit::<10, 0, i16>::NAR, Posit::<10, 0, i16>::NAR);
62      for p in Posit::<10, 0, i16>::cases_exhaustive() {
63        assert_eq!(Rational::try_from(-p).unwrap(), -Rational::try_from(p).unwrap())
64      }
65    }
66  }
67}