Skip to content
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

Phobos should have a trait to identify NaN-like behavior #10401

Open
dlangBugzillaToGithub opened this issue Jan 3, 2020 · 1 comment
Open

Comments

@dlangBugzillaToGithub
Copy link

simen.kjaras reported this on 2020-01-03T20:53:53Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=20478

Description

Since we have the pattern of returning float.nan from opCmp to signal incomparable values, it makes sense to have a trait to identify this behavior. Something like this:

template hasNanComparison(T1, T2 = T1)
{
    static if (isFloatingPoint!T1)
        enum hasNanComparison = true;
    else static if (!isAggregateType!T1)
        enum hasNanComparison = false;
    else static if (!is(typeof((T1 a, T2 b) => a.opCmp(b))))
        enum hasNanComparison = false;
    else
        enum hasNanComparison = hasNanComparison!(ReturnType!((T1 a, T2 b) => a.opCmp(b)));
}

///
@safe unittest
{
    assert( hasNanComparison!float);
    assert( hasNanComparison!real);
    assert(!hasNanComparison!int);
    assert(!hasNanComparison!string);
    assert(!hasNanComparison!(int*));
    assert(!hasNanComparison!(int[]));

    struct S
    {
        float opCmp(S s)
        {
            return float.nan;
        }
    }
    assert( hasNanComparison!S);

    struct S2
    {
        S opCmp(S2 s)
        {
            return S();
        }
    }
    assert( hasNanComparison!S2);
}
@dlangBugzillaToGithub
Copy link
Author

dlang-bot commented on 2020-01-03T20:56:41Z

@Biotronic created dlang/phobos pull request #7342 "Fix issue 20478" fixing this issue:

- Fix issue 20478

https://github.com/dlang/phobos/pull/7342

@thewilsonator thewilsonator removed OS:Windows Issues Specific to Windows Arch:x86 Issues specific to x86 P4 labels Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants