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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
//! Ordering and comparisons.
//!
//! This module provides traits and functions for comparing floating-point and
//! other partially ordered values. For primitive floating-point types, a total
//! ordering is provided via the `FloatEq` and `FloatOrd` traits:
//!
//! $$-\infin<\cdots<0<\cdots<\infin<\text{NaN}$$
//!
//! Note that both zero and `NaN` have more than one representation in IEEE-754
//! encoding. Given the set of zero representations $Z$ and set of `NaN`
//! representations $N$, this ordering coalesces `-0`, `+0`, and `NaN`s such
//! that:
//!
//! $$
//! \begin{aligned}
//! a=b&\mid a\in{Z},~b\in{Z}\cr\[1em\]
//! a=b&\mid a\in{N},~b\in{N}\cr\[1em\]
//! n>x&\mid n\in{N},~x\notin{N}
//! \end{aligned}
//! $$
//!
//! These same semantics are used in the `Eq` and `Ord` implementations for
//! `ContrainedFloat`, which includes the `Total`, `NotNan`, and `Finite` type
//! definitions.
//!
//! # Examples
//!
//! Comparing `f64` values using a total ordering:
//!
//! ```rust
//! use core::cmp::Ordering;
//! use decorum::cmp::FloatOrd;
//! use decorum::Nan;
//!
//! let x = f64::NAN;
//! let y = 1.0f64;
//!
//! let (min, max) = match x.float_cmp(&y) {
//! Ordering::Less | Ordering::Equal => (x, y),
//! _ => (y, x),
//! };
//! ```
//!
//! Computing a pairwise minimum that propagates `NaN`s:
//!
//! ```rust
//! use decorum::cmp;
//! use decorum::Nan;
//!
//! let x = f64::NAN;
//! let y = 1.0f64;
//!
//! // `Nan` is incomparable and represents an undefined computation with respect to
//! // ordering, so `min` is assigned a `NaN` value in this example.
//! let min = cmp::min_or_undefined(x, y);
//! ```
use core::cmp::Ordering;
use crate::canonical::ToCanonicalBits;
use crate::constraint::Constraint;
use crate::primitive::Primitive;
use crate::proxy::ConstrainedFloat;
use crate::{Float, Nan};
/// Equivalence relation for floating-point primitives.
///
/// `FloatEq` agrees with the total ordering provided by `FloatOrd`. See the
/// module documentation for more. Importantly, given the set of `NaN`
/// representations $N$, `FloatEq` expresses:
///
/// $$
/// \begin{aligned}
/// a=b&\mid a\in{N},~b\in{N}\cr\[1em\]
/// n\ne x&\mid n\in{N},~x\notin{N}
/// \end{aligned}
/// $$
///
/// # Examples
///
/// Comparing `NaN`s using primitive floating-point types:
///
/// ```rust
/// use decorum::cmp::FloatEq;
/// use decorum::Infinite;
///
/// let x = 0.0f64 / 0.0; // `NaN`.
/// let y = f64::INFINITY - f64::INFINITY; // `NaN`.
///
/// assert!(x.float_eq(&y));
/// ```
pub trait FloatEq {
fn float_eq(&self, other: &Self) -> bool;
}
impl<T> FloatEq for T
where
T: Float + Primitive,
{
fn float_eq(&self, other: &Self) -> bool {
self.to_canonical_bits() == other.to_canonical_bits()
}
}
impl<T> FloatEq for [T]
where
T: Float + Primitive,
{
fn float_eq(&self, other: &Self) -> bool {
if self.len() == other.len() {
self.iter().zip(other.iter()).all(|(a, b)| a.float_eq(b))
}
else {
false
}
}
}
/// Total ordering of primitive floating-point types.
///
/// `FloatOrd` expresses the total ordering:
///
/// $$-\infin<\cdots<0<\cdots<\infin<\text{NaN}$$
///
/// This trait can be used to compare primitive floating-point types without the
/// need to wrap them within a proxy type. See the module documentation for more
/// about the ordering used by `FloatOrd` and proxy types.
pub trait FloatOrd {
fn float_cmp(&self, other: &Self) -> Ordering;
}
impl<T> FloatOrd for T
where
T: Float + Primitive,
{
fn float_cmp(&self, other: &Self) -> Ordering {
match self.partial_cmp(&other) {
Some(ordering) => ordering,
None => {
if self.is_nan() {
if other.is_nan() {
Ordering::Equal
}
else {
Ordering::Greater
}
}
else {
Ordering::Less
}
}
}
}
}
impl<T> FloatOrd for [T]
where
T: Float + Primitive,
{
fn float_cmp(&self, other: &Self) -> Ordering {
match self
.iter()
.zip(other.iter())
.map(|(a, b)| a.float_cmp(b))
.find(|ordering| *ordering != Ordering::Equal)
{
Some(ordering) => ordering,
None => self.len().cmp(&other.len()),
}
}
}
/// Partial ordering of types with intrinsic representations for undefined
/// comparisons.
///
/// `IntrinsicOrd` is similar to `PartialOrd`, but provides a pairwise
/// minimum-maximum API and, for types without a total ordering, is only
/// implemented for such types that additionally have intrinsic representations
/// for _undefined_, such as the `None` variant of `Option` and `NaN`s for
/// floating-point primitives. `PrimitiveOrd` is also _closed_ and always
/// compares two values of the same type.
///
/// This trait is also implemented for numeric types with total orderings, and
/// can be used for comparisons that propagate `NaN`s for floating-point
/// primitives (unlike `PartialOrd`, which expresses comparisons of types `T`
/// and `U` with the extrinsic type `Option<Ordering>`).
///
/// See the `min_or_undefined` and `max_or_undefined` functions.
pub trait IntrinsicOrd: Copy + PartialOrd + Sized {
/// Returns `true` if a value encodes _undefined_, otherwise `false`.
///
/// Prefer this predicate over direct comparisons. For floating-point
/// representations, `NaN` is considered undefined, but direct comparisons
/// with `NaN` values should be avoided.
fn is_undefined(&self) -> bool;
/// Compares two values and returns their pairwise minimum and maximum.
///
/// This function returns a representation of _undefined_ for both the
/// minimum and maximum if either of the inputs are _undefined_ or the
/// inputs cannot be compared, **even if undefined values are ordered or the
/// type has a total ordering**. Undefined values are always propagated.
///
/// # Examples
///
/// Propagating `NaN` values when comparing proxy types with a total
/// ordering:
///
/// ```rust
/// use decorum::cmp::{self, IntrinsicOrd};
/// use decorum::{Nan, Total};
///
/// let x: Total<f64> = 0.0.into();
/// let y: Total<f64> = (0.0 / 0.0).into(); // `NaN`.
///
/// // `Total` provides a total ordering in which zero is less than `NaN`, but `NaN`
/// // is considered undefined and is the result of the intrinsic comparison.
/// assert!(y.is_undefined());
/// assert!(cmp::min_or_undefined(x, y).is_undefined());
/// ```
fn min_max_or_undefined(&self, other: &Self) -> (Self, Self);
fn min_or_undefined(&self, other: &Self) -> Self {
self.min_max_or_undefined(other).0
}
fn max_or_undefined(&self, other: &Self) -> Self {
self.min_max_or_undefined(other).1
}
}
macro_rules! impl_intrinsic_ord {
(no_nan_total => $t:ty) => {
impl IntrinsicOrd for $t {
fn is_undefined(&self) -> bool {
false
}
fn min_max_or_undefined(&self, other: &Self) -> (Self, Self) {
match self.partial_cmp(other) {
Some(ordering) => match ordering {
Ordering::Less | Ordering::Equal => (*self, *other),
_ => (*other, *self),
},
_ => unreachable!(),
}
}
}
};
(nan_partial => $t:ty) => {
impl IntrinsicOrd for $t {
fn is_undefined(&self) -> bool {
self.is_nan()
}
fn min_max_or_undefined(&self, other: &Self) -> (Self, Self) {
match self.partial_cmp(other) {
Some(ordering) => match ordering {
Ordering::Less | Ordering::Equal => (*self, *other),
_ => (*other, *self),
},
_ => (Nan::NAN, Nan::NAN),
}
}
}
};
}
impl_intrinsic_ord!(no_nan_total => isize);
impl_intrinsic_ord!(no_nan_total => i8);
impl_intrinsic_ord!(no_nan_total => i16);
impl_intrinsic_ord!(no_nan_total => i32);
impl_intrinsic_ord!(no_nan_total => i64);
impl_intrinsic_ord!(no_nan_total => i128);
impl_intrinsic_ord!(no_nan_total => usize);
impl_intrinsic_ord!(no_nan_total => u8);
impl_intrinsic_ord!(no_nan_total => u16);
impl_intrinsic_ord!(no_nan_total => u32);
impl_intrinsic_ord!(no_nan_total => u64);
impl_intrinsic_ord!(no_nan_total => u128);
impl_intrinsic_ord!(nan_partial => f32);
impl_intrinsic_ord!(nan_partial => f64);
// Note that it is not necessary for `NaN` to be a member of the constraint.
// This implementation explicitly detects `NaN`s and emits `NaN` as the
// maximum and minimum (it does not use `FloatOrd`).
impl<T, P> IntrinsicOrd for ConstrainedFloat<T, P>
where
T: Float + IntrinsicOrd + Primitive,
P: Constraint<T>,
{
fn is_undefined(&self) -> bool {
self.into_inner().is_nan()
}
fn min_max_or_undefined(&self, other: &Self) -> (Self, Self) {
// This function operates on primitive floating-point values. This
// avoids the need for implementations for each combination of proxy and
// constraint (proxy types do not always implement `Nan`, but primitive
// types do).
let a = self.into_inner();
let b = other.into_inner();
let (min, max) = a.min_max_or_undefined(&b);
// Both `min` and `max` are `NaN` if `a` and `b` are incomparable.
if min.is_nan() {
let nan = T::NAN.into();
(nan, nan)
}
else {
(min.into(), max.into())
}
}
}
impl<T> IntrinsicOrd for Option<T>
where
T: Copy + PartialOrd,
{
fn is_undefined(&self) -> bool {
self.is_none()
}
fn min_max_or_undefined(&self, other: &Self) -> (Self, Self) {
match (self.as_ref(), other.as_ref()) {
(Some(a), Some(b)) => match a.partial_cmp(b) {
Some(ordering) => match ordering {
Ordering::Less | Ordering::Equal => (Some(*a), Some(*b)),
_ => (Some(*b), Some(*a)),
},
_ => (None, None),
},
_ => (None, None),
}
}
}
/// Partial maximum of types with intrinsic representations for undefined.
///
/// See the `IntrinsicOrd` trait.
pub fn max_or_undefined<T>(a: T, b: T) -> T
where
T: IntrinsicOrd,
{
a.max_or_undefined(&b)
}
/// Partial minimum of types with intrinsic representations for undefined.
///
/// See the `IntrinsicOrd` trait.
pub fn min_or_undefined<T>(a: T, b: T) -> T
where
T: IntrinsicOrd,
{
a.min_or_undefined(&b)
}
#[cfg(test)]
mod tests {
use num_traits::{One, Zero};
use crate::cmp::{self, FloatEq, IntrinsicOrd};
use crate::{Nan, Total};
#[test]
#[allow(clippy::eq_op)]
#[allow(clippy::zero_divided_by_zero)]
fn primitive_eq() {
let x = 0.0f64 / 0.0f64; // `NaN`.
let y = f64::INFINITY + f64::NEG_INFINITY; // `NaN`.
let xs = [1.0f64, f64::NAN, f64::INFINITY];
let ys = [1.0f64, f64::NAN, f64::INFINITY];
assert!(x.float_eq(&y));
assert!(xs.float_eq(&ys));
}
#[test]
fn intrinsic_ord_option() {
let zero = Some(0u64);
let one = Some(1u64);
assert_eq!(zero, cmp::min_or_undefined(zero, one));
assert_eq!(one, cmp::max_or_undefined(zero, one));
assert!(cmp::min_or_undefined(None, zero).is_undefined());
}
#[test]
#[allow(clippy::float_cmp)]
fn intrinsic_ord_primitive() {
let zero = 0.0f64;
let one = 1.0f64;
assert_eq!(zero, cmp::min_or_undefined(zero, one));
assert_eq!(one, cmp::max_or_undefined(zero, one));
assert!(cmp::min_or_undefined(f64::NAN, zero).is_undefined());
}
#[test]
fn intrinsic_ord_proxy() {
let nan = Total::<f64>::NAN;
let zero = Total::zero();
let one = Total::one();
assert_eq!((zero, one), zero.min_max_or_undefined(&one));
assert_eq!((zero, one), one.min_max_or_undefined(&zero));
assert_eq!((nan, nan), nan.min_max_or_undefined(&zero));
assert_eq!((nan, nan), zero.min_max_or_undefined(&nan));
assert_eq!((nan, nan), nan.min_max_or_undefined(&nan));
assert_eq!(nan, cmp::min_or_undefined(nan, zero));
assert_eq!(nan, cmp::max_or_undefined(nan, zero));
assert_eq!(nan, cmp::min_or_undefined(nan, nan));
assert_eq!(nan, cmp::max_or_undefined(nan, nan));
}
}