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
198
199
200
201
202
203
204
205
206
207
208
209
use std::fmt::Debug;
use std::ops::*;

pub trait Scalar: Copy + Debug + PartialEq + PartialOrd {
    fn min(self, other: Self) -> Self {
        if self < other { self } else { other }
    }

    fn max(self, other: Self) -> Self {
        if self > other { self } else { other }
    }
}

impl<S: Copy + Debug + PartialEq + PartialOrd> Scalar for S {}

pub trait Number: Scalar + 
    Add<Output=Self> + AddAssign +
    Sub<Output=Self> + SubAssign + 
    Mul<Output=Self> + MulAssign +
    Div<Output=Self> + DivAssign +
    Rem<Output=Self> + RemAssign
{    
    const ZERO: Self;
    const ONE: Self;
    const TWO: Self;
}

macro_rules! impl_number {
    ($ty: ty, $zero: tt, $one: tt) => {
        impl Number for $ty {
            const ZERO: Self = $zero;
            const ONE: Self = $one;
            const TWO: Self = $one + $one;
        }
    }
}

impl_number!(i8, 0, 1);
impl_number!(u8, 0, 1);
impl_number!(i16, 0, 1);
impl_number!(u16, 0, 1);
impl_number!(i32, 0, 1);
impl_number!(u32, 0, 1);
impl_number!(i64, 0, 1);
impl_number!(u64, 0, 1);
impl_number!(i128, 0, 1);
impl_number!(u128, 0, 1);
impl_number!(isize, 0, 1);
impl_number!(usize, 0, 1);
impl_number!(f32, 0.0, 1.0);
impl_number!(f64, 0.0, 1.0);

pub trait Signed: Number + Neg<Output=Self> {
    fn abs(self) -> Self;
    fn signum(self) -> Self;
}

macro_rules! impl_signed {
    ($ty: ty) => {
        impl Signed for $ty {
            #[inline(always)]
            fn abs(self) -> Self {
                Self::abs(self)
            } 

            #[inline(always)]
            fn signum(self) -> Self {
                Self::signum(self)
            }
        }
    };
}

impl_signed!(i8);
impl_signed!(i32);
impl_signed!(i64);
impl_signed!(i128);
impl_signed!(isize);
impl_signed!(f32);
impl_signed!(f64);

pub trait Float: Signed + From<f32> {
    type Bits;

    const HALF: Self;
    const RADIX: u32;
    const MANTISSA_DIGITS: u32;
    const DIGITS: u32;
    const EPSILON: Self;
    const MIN: Self;
    const MIN_POSITIVE: Self;
    const MAX: Self;
    const MIN_EXP: i32;
    const MAX_EXP: i32;
    const MIN_10_EXP: i32;
    const MAX_10_EXP: i32;
    const NAN: Self;
    const INFINITY: Self;
    const NEG_INFINITY: Self;

    fn floor(self) -> Self;
    fn ceil(self) -> Self;
    fn round(self) -> Self;
    fn trunc(self) -> Self;
    fn fract(self) -> Self;
    fn copysign(self, other: Self) -> Self;
    fn mul_add(self, a: Self, b: Self) -> Self;
    fn div_euclid(self, other: Self) -> Self;
    fn rem_euclid(self, other: Self) -> Self;
    fn powi(self, pow: i32) -> Self;
    fn powf(self, pow: Self) -> Self;
    fn sqrt(self) -> Self;
    fn exp(self) -> Self;
    fn exp2(self) -> Self;
    fn ln(self) -> Self;
    fn log(self, base: Self) -> Self;
    fn log10(self) -> Self;
    fn log2(self) -> Self;
    fn cbrt(self) -> Self;
    fn hypot(self, other: Self) -> Self;
    fn sin(self) -> Self;
    fn cos(self) -> Self;
    fn tan(self) -> Self;
    fn asin(self) -> Self;
    fn acos(self) -> Self;
    fn atan(self) -> Self;
    fn sin_cos(self) -> (Self, Self);
    fn exp_m1(self) -> Self;
    fn ln_1p(self) -> Self;
    fn sinh(self) -> Self;
    fn cosh(self) -> Self;
    fn tanh(self) -> Self;
    fn asinh(self) -> Self;
    fn acosh(self) -> Self;
    fn atanh(self) -> Self;
    fn recip(self) -> Self;
    fn to_radians(self) -> Self;
    fn to_degrees(self) -> Self;
    fn from_bits(bits: Self::Bits) -> Self;
    fn to_bits(self) -> Self::Bits;
}

macro_rules! impl_float {
    ($ty: tt, $bits: ty) => {
        impl Float for $ty {
            type Bits = $bits;

            const HALF: Self = 0.5;
            const RADIX: u32 = std::$ty::RADIX;
            const MANTISSA_DIGITS: u32 = std::$ty::MANTISSA_DIGITS;
            const DIGITS: u32 = std::$ty::DIGITS;
            const EPSILON: Self = std::$ty::EPSILON;
            const MIN: Self = std::$ty::MIN;
            const MIN_POSITIVE: Self = std::$ty::MIN_POSITIVE;
            const MAX: Self = std::$ty::MAX;
            const MIN_EXP: i32 = std::$ty::MIN_EXP;
            const MAX_EXP: i32 = std::$ty::MAX_EXP;
            const MIN_10_EXP: i32 = std::$ty::MIN_10_EXP;
            const MAX_10_EXP: i32 = std::$ty::MAX_10_EXP;
            const NAN: Self = std::$ty::NAN;
            const INFINITY: Self = std::$ty::INFINITY;
            const NEG_INFINITY: Self = std::$ty::NEG_INFINITY;

            #[inline(always)] fn floor(self) -> Self { Self::floor(self) }
            #[inline(always)] fn ceil(self) -> Self { Self::ceil(self) }
            #[inline(always)] fn round(self) -> Self { Self::round(self) }
            #[inline(always)] fn trunc(self) -> Self { Self::trunc(self) }
            #[inline(always)] fn fract(self) -> Self { Self::fract(self) }
            #[inline(always)] fn copysign(self, other: Self) -> Self { Self::copysign(self, other) }
            #[inline(always)] fn mul_add(self, a: Self, b: Self) -> Self { Self::mul_add(self, a, b) }
            #[inline(always)] fn div_euclid(self, other: Self) -> Self { Self::div_euclid(self, other) }
            #[inline(always)] fn rem_euclid(self, other: Self) -> Self { Self::rem_euclid(self, other) }
            #[inline(always)] fn powi(self, pow: i32) -> Self { Self::powi(self, pow) }
            #[inline(always)] fn powf(self, pow: Self) -> Self { Self::powf(self, pow) }
            #[inline(always)] fn sqrt(self) -> Self { Self::sqrt(self) }
            #[inline(always)] fn exp(self) -> Self { Self::exp(self) }
            #[inline(always)] fn exp2(self) -> Self { Self::exp2(self) }
            #[inline(always)] fn ln(self) -> Self { Self::ln(self) }
            #[inline(always)] fn log(self, base: Self) -> Self { Self::log(self, base) }
            #[inline(always)] fn log10(self) -> Self { Self::log10(self) }
            #[inline(always)] fn log2(self) -> Self { Self::log2(self) }
            #[inline(always)] fn cbrt(self) -> Self { Self::cbrt(self) }
            #[inline(always)] fn hypot(self, other: Self) -> Self { Self::hypot(self, other) }
            #[inline(always)] fn sin(self) -> Self { Self::sin(self) }
            #[inline(always)] fn cos(self) -> Self { Self::cos(self) }
            #[inline(always)] fn tan(self) -> Self { Self::tan(self) }
            #[inline(always)] fn asin(self) -> Self { Self::asin(self) }
            #[inline(always)] fn acos(self) -> Self { Self::acos(self) }
            #[inline(always)] fn atan(self) -> Self { Self::atan(self) }
            #[inline(always)] fn sin_cos(self) -> (Self, Self) { Self::sin_cos(self) }
            #[inline(always)] fn exp_m1(self) -> Self { Self::exp_m1(self) }
            #[inline(always)] fn ln_1p(self) -> Self { Self::ln_1p(self) }
            #[inline(always)] fn sinh(self) -> Self { Self::sinh(self) }
            #[inline(always)] fn cosh(self) -> Self { Self::cosh(self) }
            #[inline(always)] fn tanh(self) -> Self { Self::tanh(self) }
            #[inline(always)] fn asinh(self) -> Self { Self::asinh(self) }
            #[inline(always)] fn acosh(self) -> Self { Self::acosh(self) }
            #[inline(always)] fn atanh(self) -> Self { Self::atanh(self) }
            #[inline(always)] fn recip(self) -> Self { Self::recip(self) }
            #[inline(always)] fn to_radians(self) -> Self { Self::to_radians(self) }
            #[inline(always)] fn to_degrees(self) -> Self { Self::to_degrees(self) }
            #[inline(always)] fn from_bits(bits: Self::Bits) -> Self { Self::from_bits(bits) }
            #[inline(always)] fn to_bits(self) -> Self::Bits { Self::to_bits(self) }
        }
    };
}

impl_float!(f32, u32);
impl_float!(f64, u64);