From 378cad782703c87821c3376b7cf776c82d1406fe Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Sun, 13 Dec 2020 10:24:54 +0000 Subject: [PATCH] More precision lowering --- rand_distr/tests/value_stability.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/rand_distr/tests/value_stability.rs b/rand_distr/tests/value_stability.rs index 525663effc..8db9b3b46e 100644 --- a/rand_distr/tests/value_stability.rs +++ b/rand_distr/tests/value_stability.rs @@ -6,7 +6,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use core::{fmt::Debug, cmp::PartialEq}; +use core::fmt::{Debug, Display}; +use core::cmp::PartialEq; +use num_traits::Float; use rand::Rng; use rand_distr::*; @@ -26,6 +28,16 @@ fn test_samples>( } } +fn test_samples_approx>( + seed: u64, distr: D, thresh: F, expected: &[F], +) { + let mut rng = get_rng(seed); + for &val in expected { + let x = rng.sample(&distr); + assert!((x - val).abs() < thresh, "not approx eq: {}, {}", x, val); + } +} + #[test] fn binominal_stability() { // We have multiple code paths: np < 10, p > 0.5 @@ -95,7 +107,7 @@ fn pareto_stability() { test_samples(213, Pareto::new(1.0, 1.0).unwrap(), &[ 1.0423688f32, 2.1235929, 4.132709, 1.4679428, ]); - test_samples(213, Pareto::new(2.0, 0.5).unwrap(), &[ + test_samples_approx(213, Pareto::new(2.0, 0.5).unwrap(), 1e-14, &[ 9.019295276219136f64, 4.3097126018270595, 6.837815045397157, @@ -184,7 +196,7 @@ fn gamma_stability() { 0.5013580219361969, 0.1457735613733489, ]); - test_samples(223, ChiSquared::new(0.1).unwrap(), &[ + test_samples_approx(223, ChiSquared::new(0.1).unwrap(), 1e-15, &[ 0.014824404726978617f64, 0.021602123937134326, 0.0000003431429746851693, @@ -285,7 +297,7 @@ fn normal_stability() { test_samples(213, LogNormal::new(0.0, 1.0).unwrap(), &[ 0.88830346f32, 2.1844804, 1.0678421, 0.30322206, ]); - test_samples(213, LogNormal::new(2.0, 0.5).unwrap(), &[ + test_samples_approx(213, LogNormal::new(2.0, 0.5).unwrap(), 1e-14, &[ 6.964174338639032f64, 10.921015733601452, 7.6355881556915906,