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
use num_rational::Ratio;

#[cfg(feature = "num-bigint")]
use num_bigint::{BigInt, BigUint};

/// Represents a possible error occured calling [FromSqrt]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum FromSqrtError {
    /// A proper representation will requires a integer type with more capacity
    Overflow,
    /// The square root is a complex number
    Complex,
    /// The square root of the target cannot be represented as a quadratic surd
    Unrepresentable,
}

/// Create a number instance from the square root of another number.
///
/// In case there are multiple solution for square root,
/// only canonical result will be returned
pub trait FromSqrt<T>: Sized {
    fn from_sqrt(t: T) -> Result<Self, FromSqrtError>;
}

/// Represents a number conversion with probably exact value.
#[derive(PartialEq, Debug)]
pub enum Approximation<T> {
    Approximated(T),
    Exact(T),
}

impl<T> Approximation<T> {
    /// Get the computed value regardless of whether it's exact
    pub fn value(self) -> T {
        match self {
            Approximation::Approximated(v) => v,
            Approximation::Exact(v) => v,
        }
    }
}

// TODO: backport into num-traits (https://github.com/rust-num/num-rational/issues/100)
/// This trait represents a real number that is computable.
/// See [Wiki](https://en.wikipedia.org/wiki/Computable_number)
pub trait Computable<T> {
    /// Return an approximated rational representation of the number
    /// The `limit` argument specify the maximum value of denominator. This will
    /// ensures the error of the approximation is less than 1/limit^2.
    fn approximated(&self, limit: &T) -> Approximation<Ratio<T>>;
}

// TODO: implement the rational approximation for all irrational types

/// Represents a number type with corresponding signed version
///
/// Signed number can implement this trait as well, with `type Signed = Self`.
pub trait WithSigned {
    type Signed;
    fn to_signed(self) -> Self::Signed;
}

/// Represents a number type with corresponding unsigned version
///
/// Unsigned number can implement this trait as well, with `type Unsigned = Self`.
pub trait WithUnsigned {
    type Unsigned;
    fn to_unsigned(self) -> Self::Unsigned;
}

macro_rules! impl_primitive_sign {
    ($TSigned:ty, $TUnsigned:ty) => {
        impl WithSigned for $TUnsigned {
            type Signed = $TSigned;
            #[inline]
            fn to_signed(self) -> Self::Signed {
                self as $TSigned
            }
        }
        impl WithSigned for $TSigned {
            type Signed = $TSigned;
            #[inline]
            fn to_signed(self) -> Self {
                self
            }
        }
        impl WithUnsigned for $TSigned {
            type Unsigned = $TUnsigned;
            #[inline]
            fn to_unsigned(self) -> Self::Unsigned {
                self as $TUnsigned
            }
        }
        impl WithUnsigned for $TUnsigned {
            type Unsigned = $TUnsigned;
            #[inline]
            fn to_unsigned(self) -> Self {
                self
            }
        }
    };
}
impl_primitive_sign!(i8, u8);
impl_primitive_sign!(i16, u16);
impl_primitive_sign!(i32, u32);
impl_primitive_sign!(i64, u64);
impl_primitive_sign!(i128, u128);

#[cfg(feature = "num-bigint")]
impl WithSigned for BigUint {
    type Signed = BigInt;
    #[inline]
    fn to_signed(self) -> Self::Signed {
        BigInt::from(self)
    }
}

#[cfg(feature = "num-bigint")]
impl WithUnsigned for BigUint {
    type Unsigned = BigUint;
    #[inline]
    fn to_unsigned(self) -> Self {
        self
    }
}

#[cfg(feature = "num-bigint")]
impl WithUnsigned for BigInt {
    type Unsigned = BigUint;
    #[inline]
    fn to_unsigned(self) -> Self::Unsigned {
        self.to_biguint().unwrap()
    }
}

#[cfg(feature = "num-bigint")]
impl WithSigned for BigInt {
    type Signed = BigInt;
    #[inline]
    fn to_signed(self) -> Self::Signed {
        self
    }
}