Comments (4)
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?
from approx.
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.
from approx.
Ahh - once we have const
generics we can implement it for fixed size arrays. We could also do what the standard library used to do and implement the traits for fixed size arrays of length 0 to 32. In the mean time you might be able to do:
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}].
from approx.
I ran into this today.
Your solution @brendanzab works! But I ended up writing this:
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?
from approx.
Related Issues (20)
- Use macro on Rust 1.30 HOT 2
- Automatically deriving implementations HOT 9
- Adding messages to assert_* macros HOT 2
- Release another version HOT 2
- Overflow on absolute comparison of signed integers HOT 3
- Not working at all? HOT 3
- Improve Documentation HOT 2
- Please tag releases HOT 1
- Unecessary std feature HOT 1
- relative_eq() has incorrect behavior for small values HOT 3
- Release a 1.0 version HOT 3
- Inconsistent temporary value behavior with `assert_eq!`
- clippy::if_then_panic warning (nightly) HOT 3
- Maintenance status HOT 3
- Transferral of repository HOT 1
- Give acceptable range on assertion failure
- Changelog for version updates would be nice
- `impl RelativeEq for Option<T: RelativeEq>`? HOT 1
- `assert_relative_eq!()` fails in compilation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from approx.