num_valid/functions/
real.rs

1#![deny(rustdoc::broken_intra_doc_links)]
2
3use std::{cmp::Ordering, num::FpCategory};
4
5pub trait Clamp {
6    /// Clamp the value within the specified bounds.
7    ///
8    /// Returns `max` if `self` is greater than `max`, and `min` if `self` is less than `min`.
9    /// Otherwise this returns `self`.
10    ///
11    /// Note that this function returns `NaN` if the initial value was `NaN` as well.
12    ///
13    /// # Panics
14    /// Panics if `min` > `max`, `min` is `NaN`, or `max` is `NaN`.
15    /// ```
16    /// use num_valid::functions::Clamp;
17    ///
18    /// assert!((-3.0f64).kernel_clamp(&-2.0, &1.0) == -2.0);
19    /// assert!((0.0f64).kernel_clamp(&-2.0, &1.0) == 0.0);
20    /// assert!((2.0f64).kernel_clamp(&-2.0, &1.0) == 1.0);
21    /// assert!((f64::NAN).kernel_clamp(&-2.0, &1.0).is_nan());
22    /// ```
23    fn kernel_clamp(self, min: &Self, max: &Self) -> Self;
24}
25
26pub trait Classify {
27    /// Returns the floating point category of the number. If only one property is going to be tested,
28    /// it is generally faster to use the specific predicate instead.
29    /// ```
30    /// use num_valid::functions::Classify;
31    /// use std::num::FpCategory;
32    ///
33    /// let num = 12.4_f64;
34    /// let inf = f64::INFINITY;
35    ///
36    /// assert_eq!(num.kernel_classify(), FpCategory::Normal);
37    /// assert_eq!(inf.kernel_classify(), FpCategory::Infinite);
38    /// ```
39    fn kernel_classify(&self) -> FpCategory;
40}
41
42pub trait ExpM1 {
43    /// Returns `e^(self) - 1`` in a way that is accurate even if the number is close to zero.
44    fn kernel_exp_m1(self) -> Self;
45}
46
47pub trait Hypot {
48    /// Compute the distance between the origin and a point (`self`, `other`) on the Euclidean plane.
49    /// Equivalently, compute the length of the hypotenuse of a right-angle triangle with other sides having length `self.abs()` and `other.abs()`.
50    fn kernel_hypot(self, other: &Self) -> Self;
51}
52
53pub trait Ln1p {
54    /// Returns `ln(1. + self)` (natural logarithm) more accurately than if the operations were performed separately.
55    fn kernel_ln_1p(self) -> Self;
56}
57
58pub trait TotalCmp {
59    fn total_cmp(&self, other: &Self) -> Ordering;
60}