www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19686] New: sgn is too greedy

https://issues.dlang.org/show_bug.cgi?id=19686

          Issue ID: 19686
           Summary: sgn is too greedy
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: simen.kjaras gmail.com

The current implementation of sgn in Phobos is too greedy, and will match types
that it doesn't work with. For reference, this is what sgn look like at time of
writing:

F sgn(F)(F x)  safe pure nothrow  nogc
{
    //    TODO   : make this faster
    return x > 0 ? 1 : x < 0 ? -1 : x;
}

This greedyness leads to error messages with template spam when doing things
like sgn("string"), and also makes it misbehave when used alongside other sgns
for other types (e.g. std.complex).


It should look more like this:

F sgn(F)(F x)  safe pure nothrow  nogc
if (isFloatingPoint!F || isIntegral!F)
{
    //    TODO   : make this faster
    return x > 0 ? 1 : x < 0 ? -1 : x;
}

--
Feb 19 2019