mir: How to change iterator?

Basile B. b2.temp at gmx.com
Thu Apr 16 19:56:21 UTC 2020


On Tuesday, 14 April 2020 at 20:24:05 UTC, jmh530 wrote:
> In the code below, I multiply some slice by 5 and then check 
> whether it equals another slice. This fails for mir's 
> approxEqual because the two are not the same types (yes, I know 
> that isClose in std.math works). I was trying to convert the y 
> variable below to have the same double* iterator as the term on 
> the right, but without much success. I tried std.conv.to and 
> the as, slice, and sliced functions in mir.
>
> I figure I am missing something basic, but I can't quite figure 
> it out...
>
>
> /+dub.sdl:
> dependency "mir-algorithm" version="~>3.7.28"
> +/
>
> import mir.math.common: approxEqual;
> import mir.ndslice.slice : sliced;
>
> void main() {
>     auto x = [0.5, 0.5].sliced(2);
>     auto y = x * 5.0;
>
>     assert(approxEqual(y, [2.5, 2.5].sliced(2)));
> }

`approxEqual` cant work with ranges. If you look at the signature 
there is a use of the constructor syntax, e.g const `T maxRelDiff 
= T(0x1p-20f)` so when `T` is not a basic FP type that just does 
not compile (note the error message if you try with .array on 
both operands)

I'd just use zip(...).each!(...), e.g

     assert(zip(y, [2.5, 2.5].sliced(2)).each!(a => 
assert(approxEqual(a[0], a[1]))));

But I don't know MIR at all.


More information about the Digitalmars-d-learn mailing list