-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for slices? #58
Comments
Yeah, this seems like it would be really useful! I'm pretty sure this should work, seeing as slices implement the approx traits? Perhaps the documentation should be improved? |
If they do, it would certainly help to mention it ;) I concluded from the docs that they don't, and rolled my own (totally not configurable): #[macro_export]
macro_rules! assert_allclose {
($a:expr, $b:expr) => {
use approx::ulps_eq;
assert_eq!(
$a.len(),
$b.len(),
"Data lengths differ: {} != {}",
$a.len(),
$b.len()
);
for i in 0 .. $a.len() {
assert!(
ulps_eq!($a[i], $b[i], max_ulps = 7),
"Values at index {} differ: {} != {}\na = {:?}\nb = {:?}",
i,
$a[i],
$b[i],
$a,
$b
);
}
};
} This indeed looks like it should do the job, but in practice I'm getting: What may be a bit of a problem with the trait based approach is that a failed assert probably cannot output information to explain why it failed -- which can be quite a time safer. I'm wondering if this could be solved in a combination with the great pretty assertion crate. |
Ahh - once we have assert_abs_diff_eq!([1.0, 2.0, 3.0].as_ref(), [1.0, 2.0, 3.0].as_ref()); This converts both sides to `&[{float}]. |
I ran into this today. use approx::assert_abs_diff_eq;
use std::fmt::Debug;
fn array_assert_eq<A: approx::AbsDiffEq<A> + Debug, const N: usize>(a: [A; N], b: [A; N]) {
a.into_iter()
.zip_eq(b.into_iter())
.for_each(|(x, y)| assert_abs_diff_eq!(x, y));
} which is super-bad, but it means that const-generics are present enough to support slices finally? |
In data science use cases, the most frequent test is to assert approximate equality of entire float data vectors. For instance in Numpy based code, testing with
assert_allclose
is ubiquitous. I'm looking for the equivalent in Rust.As far as I can see, the macros currently doesn't support slices. Is there an interest to support them?
The text was updated successfully, but these errors were encountered: