Bug 18257 – std.math.abs/fabs - redesign

Status
NEW
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
All
OS
Windows
Creation time
2018-01-17T17:39:41Z
Last change time
2024-12-01T16:32:02Z
Assigned to
No Owner
Creator
Răzvan Ștefănescu
Moved to GitHub: phobos#10293 →

Comments

Comment #0 by rumbu — 2018-01-17T17:39:41Z
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.
Comment #1 by robert.schadek — 2024-12-01T16:32:02Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10293 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB