[Issue 18257] New: std.math.abs/fabs - redesign

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jan 17 17:39:41 UTC 2018


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

          Issue ID: 18257
           Summary: std.math.abs/fabs - redesign
           Product: D
           Version: D2
          Hardware: All
                OS: Windows
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: rumbu at rumbu.ro

Problem 1: std.math.abs signature matches any type which supports the
comparison operator. That means that you cannot write your own abs for a custom
numeric type as long as your type has a comparison operator.

Problem 2: even if a custom numeric type supports comparison operator,
std.math.abs is declared as pure @safe nothrow @nogc, forcing a potential
numeric implementation to decorate his opCmp overload with the same attributes.

Real case: according to IEEE-754/2008, the comparison operator *must* signal an
invalid operation exception if it encounters a NaN value or *must* set a global
error flag. For a standard compliant numeric type, the comparison operator
cannot be decorated with nothrow @nocg (since it's throwing exceptions) and
cannot be pure (since it's setting global error flags).

Proposal 1:
Rethink abs signature to limit usage to standard numeric types, where
comparison is guaranteed to be pure @safe nothrow @nogc

Proposal 2:
Drop fabs. This is probably coming from C where overloads are not available.
There is no need to have two names for a function doing the exact same thing.
abs must catch any signed type, including floating point.

--


More information about the Digitalmars-d-bugs mailing list