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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#[macro_use]
extern crate derivative;
extern crate num_traits;
#[cfg(feature = "serialize-serde")]
extern crate serde;

use num_traits::Float;
use std::num::FpCategory;

// TODO: Emit useful errors and use the error_chain crate.

mod constrain;
mod hash;
mod policy;

// TODO: Do not re-export `ConstrainedFloat` or policies. This is only
//       re-exported so that documentation is more complete (`rustdoc` will
//       provide no documentation for type definitions against types that are
//       not re-exported).
pub use constrain::ConstrainedFloat;
pub use hash::{hash_float, hash_float_array, hash_float_slice};
pub use policy::{FinitePolicy, NotNanPolicy};

pub type NotNan<T> = ConstrainedFloat<T, NotNanPolicy<T>>;
pub type Finite<T> = ConstrainedFloat<T, FinitePolicy<T>>;

pub type N32 = NotNan<f32>;
pub type N64 = NotNan<f64>;

// Use "R" for "real" instead of "F" for "finite", because then this name would
// be very similar to `f32` and `f64`, differentiated only be capitalization.
pub type R32 = Finite<f32>;
pub type R64 = Finite<f64>;

// This is essentially `num_traits::Float` without its NaN or INF functions.
// Until such a distinction is made upstream, this can be used to be generic
// over both raw and constrained floats.
//
// Implementations for `Real`, `Infinity` and `Nan` are provided for all types
// implementing `num_traits::Float`.
pub trait Real: Copy + Sized {
    fn max_value() -> Self;
    fn min_value() -> Self;
    fn min_positive_value() -> Self;

    fn neg_zero() -> Self;

    fn is_sign_positive(self) -> bool;
    fn is_sign_negative(self) -> bool;

    fn classify(self) -> FpCategory;
    fn is_normal(self) -> bool;

    fn integer_decode(self) -> (u64, i16, i8);

    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 atan2(self, other: Self) -> Self;
    fn sin_cos(self) -> (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;

    // TODO: Provide the remaining functions from `Float`.
}

pub trait Infinite: Copy + Sized {
    fn infinity() -> Self;
    fn neg_infinity() -> Self;
    fn is_infinite(self) -> bool;
    fn is_finite(self) -> bool;
}

pub trait Nan: Copy + Sized {
    fn nan() -> Self;
    fn is_nan(self) -> bool;
}

impl<T> Real for T
where
    T: Float,
{
    #[inline(always)]
    fn max_value() -> Self {
        Float::max_value()
    }

    #[inline(always)]
    fn min_value() -> Self {
        Float::min_value()
    }

    #[inline(always)]
    fn min_positive_value() -> Self {
        Float::min_positive_value()
    }

    #[inline(always)]
    fn neg_zero() -> Self {
        Float::neg_zero()
    }

    #[inline(always)]
    fn is_sign_positive(self) -> bool {
        Float::is_sign_positive(self)
    }

    #[inline(always)]
    fn is_sign_negative(self) -> bool {
        Float::is_sign_negative(self)
    }

    #[inline(always)]
    fn classify(self) -> FpCategory {
        Float::classify(self)
    }

    #[inline(always)]
    fn is_normal(self) -> bool {
        Float::is_normal(self)
    }

    #[inline(always)]
    fn integer_decode(self) -> (u64, i16, i8) {
        Float::integer_decode(self)
    }

    #[inline(always)]
    fn hypot(self, other: Self) -> Self {
        Float::hypot(self, other)
    }

    #[inline(always)]
    fn sin(self) -> Self {
        Float::sin(self)
    }

    #[inline(always)]
    fn cos(self) -> Self {
        Float::cos(self)
    }

    #[inline(always)]
    fn tan(self) -> Self {
        Float::tan(self)
    }

    #[inline(always)]
    fn asin(self) -> Self {
        Float::asin(self)
    }

    #[inline(always)]
    fn acos(self) -> Self {
        Float::acos(self)
    }

    #[inline(always)]
    fn atan(self) -> Self {
        Float::atan(self)
    }

    #[inline(always)]
    fn atan2(self, other: Self) -> Self {
        Float::atan2(self, other)
    }

    #[inline(always)]
    fn sin_cos(self) -> (Self, Self) {
        Float::sin_cos(self)
    }

    #[inline(always)]
    fn sinh(self) -> Self {
        Float::sinh(self)
    }

    #[inline(always)]
    fn cosh(self) -> Self {
        Float::cosh(self)
    }

    #[inline(always)]
    fn tanh(self) -> Self {
        Float::tanh(self)
    }

    #[inline(always)]
    fn asinh(self) -> Self {
        Float::asinh(self)
    }

    #[inline(always)]
    fn acosh(self) -> Self {
        Float::acosh(self)
    }

    #[inline(always)]
    fn atanh(self) -> Self {
        Float::atanh(self)
    }
}

impl<T> Infinite for T
where
    T: Float,
{
    #[inline(always)]
    fn infinity() -> Self {
        Float::infinity()
    }

    #[inline(always)]
    fn neg_infinity() -> Self {
        Float::neg_infinity()
    }

    #[inline(always)]
    fn is_infinite(self) -> bool {
        Float::is_infinite(self)
    }

    #[inline(always)]
    fn is_finite(self) -> bool {
        Float::is_finite(self)
    }
}

impl<T> Nan for T
where
    T: Float,
{
    #[inline(always)]
    fn nan() -> Self {
        Float::nan()
    }

    #[inline(always)]
    fn is_nan(self) -> bool {
        Float::is_nan(self)
    }
}