// Copyright (c) 2018 The predicates-rs Project Developers. // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms.
use std::fmt;
use float_cmp::ApproxEq; use float_cmp::Ulps;
usecrate::reflection; usecrate::Predicate;
/// Predicate that ensures two numbers are "close" enough, understanding that rounding errors /// occur. /// /// This is created by the `predicate::float::is_close`. #[derive(Debug, Clone, Copy, PartialEq)] pubstruct IsClosePredicate {
target: f64,
epsilon: f64,
ulps: <f64 as Ulps>::U,
}
impl IsClosePredicate { /// Set the amount of error allowed. /// /// Values `1`-`5` should work in most cases. Sometimes more control is needed and you will /// need to set `IsClosePredicate::epsilon` separately from `IsClosePredicate::ulps`. /// /// # Examples /// /// ``` /// use predicates::prelude::*; /// /// let a = 0.15_f64 + 0.15_f64 + 0.15_f64; /// let predicate_fn = predicate::float::is_close(a).distance(5); /// ``` pubfn distance(mutself, distance: <f64 as Ulps>::U) -> Self { self.epsilon = (distance as f64) * f64::EPSILON; self.ulps = distance; self
}
/// Set the absolute deviation allowed. /// /// This is meant to handle problems near `0`. Values `1.`-`5.` epislons should work in most /// cases. /// /// # Examples /// /// ``` /// use predicates::prelude::*; /// /// let a = 0.15_f64 + 0.15_f64 + 0.15_f64; /// let predicate_fn = predicate::float::is_close(a).epsilon(5.0 * f64::EPSILON); /// ``` pubfn epsilon(mutself, epsilon: f64) -> Self { self.epsilon = epsilon; self
}
/// Set the relative deviation allowed. /// /// This is meant to handle large numbers. Values `1`-`5` should work in most cases. /// /// # Examples /// /// ``` /// use predicates::prelude::*; /// /// let a = 0.15_f64 + 0.15_f64 + 0.15_f64; /// let predicate_fn = predicate::float::is_close(a).ulps(5); /// ``` pubfn ulps(mutself, ulps: <f64 as Ulps>::U) -> Self { self.ulps = ulps; self
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.