1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
use core::ops::{Mul, Neg};
use dashu_base::{Abs, Sign, Signed};
use dashu_int::UBig;
use crate::{
rbig::{RBig, Relaxed},
repr::Repr,
};
impl RBig {
/// Get the sign of the number. Zero value has a positive sign.
///
/// # Examples
///
/// ```
/// # use dashu_base::Sign;
/// # use dashu_ratio::RBig;
/// assert_eq!(RBig::ZERO.sign(), Sign::Positive);
/// assert_eq!(RBig::ONE.sign(), Sign::Positive);
/// assert_eq!(RBig::NEG_ONE.sign(), Sign::Negative);
/// ```
#[inline]
pub const fn sign(&self) -> Sign {
self.0.numerator.sign()
}
/// A number representing the sign of `self`.
///
/// * [RBig::ONE] if the number is positive (including `inf`)
/// * [RBig::ZERO] if the number is zero
/// * [RBig::NEG_ONE] if the number is negative (including `-inf`)
///
/// # Examples
///
/// ```
/// # use dashu_ratio::RBig;
///
/// let r = RBig::from_parts((-10).into(), 5u8.into());
/// assert_eq!(r.signum(), RBig::NEG_ONE);
/// ```
#[inline]
pub const fn signum(&self) -> Self {
RBig(Repr {
numerator: self.0.numerator.signum(),
denominator: UBig::ONE,
})
}
}
impl Relaxed {
/// Get the sign of the number. Zero value has a positive sign.
///
/// See [RBig::sign] for details.
#[inline]
pub const fn sign(&self) -> Sign {
self.0.numerator.sign()
}
/// A number representing the sign of `self`.
///
/// See [RBig::signum] for details.
#[inline]
pub const fn signum(&self) -> Self {
Relaxed(Repr {
numerator: self.0.numerator.signum(),
denominator: UBig::ONE,
})
}
}
impl Repr {
#[inline]
pub fn neg(mut self) -> Repr {
self.numerator = -self.numerator;
self
}
#[inline]
pub fn abs(mut self) -> Repr {
if self.numerator.sign() == Sign::Negative {
self.numerator = -self.numerator
}
self
}
}
impl Neg for RBig {
type Output = RBig;
#[inline]
fn neg(self) -> Self::Output {
RBig(self.0.neg())
}
}
impl Neg for &RBig {
type Output = RBig;
#[inline]
fn neg(self) -> Self::Output {
RBig(self.0.clone().neg())
}
}
impl Neg for Relaxed {
type Output = Relaxed;
#[inline]
fn neg(self) -> Self::Output {
Relaxed(self.0.neg())
}
}
impl Neg for &Relaxed {
type Output = Relaxed;
#[inline]
fn neg(self) -> Self::Output {
Relaxed(self.0.clone().neg())
}
}
impl Mul<Repr> for Sign {
type Output = Repr;
#[inline]
fn mul(self, mut rhs: Repr) -> Repr {
rhs.numerator *= self;
rhs
}
}
impl Mul<Sign> for Repr {
type Output = Repr;
#[inline]
fn mul(mut self, rhs: Sign) -> Repr {
self.numerator *= rhs;
self
}
}
impl Mul<Sign> for RBig {
type Output = RBig;
#[inline]
fn mul(mut self, rhs: Sign) -> RBig {
self.0.numerator *= rhs;
self
}
}
impl Mul<Sign> for Relaxed {
type Output = Relaxed;
#[inline]
fn mul(mut self, rhs: Sign) -> Self::Output {
self.0.numerator *= rhs;
self
}
}
impl Signed for RBig {
#[inline]
fn sign(&self) -> Sign {
self.0.numerator.sign()
}
}
impl Signed for Relaxed {
#[inline]
fn sign(&self) -> Sign {
self.0.numerator.sign()
}
}
impl Abs for Repr {
type Output = Self;
#[inline]
fn abs(self) -> Self::Output {
let Repr {
numerator,
denominator,
} = self;
Repr {
numerator: numerator.abs(),
denominator,
}
}
}
impl Abs for RBig {
type Output = Self;
#[inline]
fn abs(self) -> Self::Output {
RBig(self.0.abs())
}
}
impl Abs for Relaxed {
type Output = Self;
#[inline]
fn abs(self) -> Self::Output {
Relaxed(self.0.abs())
}
}