use std::fmt;
use float_cmp::ApproxEq;
use float_cmp::Ulps;
use reflection;
use Predicate;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct IsClosePredicate {
target: f64,
epsilon: f64,
ulps: <f64 as Ulps>::U,
}
impl IsClosePredicate {
pub fn distance(mut self, distance: <f64 as Ulps>::U) -> Self {
self.epsilon = (distance as f64) * ::std::f64::EPSILON;
self.ulps = distance;
self
}
pub fn epsilon(mut self, epsilon: f64) -> Self {
self.epsilon = epsilon;
self
}
pub fn ulps(mut self, ulps: <f64 as Ulps>::U) -> Self {
self.ulps = ulps;
self
}
}
impl Predicate<f64> for IsClosePredicate {
fn eval(&self, variable: &f64) -> bool {
variable.approx_eq(&self.target, self.epsilon, self.ulps)
}
fn find_case<'a>(&'a self, expected: bool, variable: &f64) -> Option<reflection::Case<'a>> {
let actual = self.eval(variable);
if expected == actual {
Some(
reflection::Case::new(Some(self), actual)
.add_product(reflection::Product::new(
"actual epsilon",
(variable - self.target).abs(),
))
.add_product(reflection::Product::new(
"actual ulps",
variable.ulps(&self.target).abs(),
)),
)
} else {
None
}
}
}
impl reflection::PredicateReflection for IsClosePredicate {
fn parameters<'a>(&'a self) -> Box<Iterator<Item = reflection::Parameter<'a>> + 'a> {
let params = vec![
reflection::Parameter::new("epsilon", &self.epsilon),
reflection::Parameter::new("ulps", &self.ulps),
];
Box::new(params.into_iter())
}
}
impl fmt::Display for IsClosePredicate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "var ~= {}", self.target)
}
}
pub fn is_close(target: f64) -> IsClosePredicate {
IsClosePredicate {
target,
epsilon: 2.0 * ::std::f64::EPSILON,
ulps: 2,
}
}