Skip to content

Improve user-friendliness of shouldApprox for floating point slices #452

Open
@jmh530

Description

@jmh530

mir.test.shouldApprox only works with floating point numbers. This requires some workaround when working with floating point slices. Some kind of improvement in the user-friendliness would be a positive. This could include an overload for shouldApprox, or other approaches.

The code below fails because shouldApprox only accepts a floating point input.

import mir.test: shouldApprox;
import mir.ndslice.slice: sliced;

void main() {
  auto x = [1.0, 2, 3, 4];
  auto y = x.sliced;
  y.shouldApprox == y;
}

The alterative, is to use all with a lambda, but this isn't so user-friendly and makes documented unit tests uglier.

import mir.test: shouldApprox;
import mir.ndslice.slice: sliced;
import mir.algorithm.iteration: all;

void main() {
    auto x = [1.0, 2, 3, 4];
    auto y = x.sliced;
    y.all!((a, b) => a.shouldApprox == b)(y);
}

So it doesn't need ShouldApprox to work with floating point slices, just an overload of shouldApprox that incorporates the all/lambda.

The only reason to have an alternate version of ShouldApprox would be if you want to have a better error message for slices.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions