Giter Club home page Giter Club logo

Comments (4)

brendanzab avatar brendanzab commented on May 29, 2024

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.

bluenote10 avatar bluenote10 commented on May 29, 2024

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:

image

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.

brendanzab avatar brendanzab commented on May 29, 2024

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.

CGMossa avatar CGMossa commented on May 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.