There are functions in std.math accepting any parameter type instead of being specialized on numeric types.
This blocks overloading of such functions for other custom numeric types as long as the end user imports std.math.
List of generic functions:
approxEqual
isFinite
isNormal
isSubnormal
NaN
nextafter
signbit
sgn
Special case: "abs" assumes wrongly that the comparison operator is pure @safe nothrow @nogc for any imaginable type which supports T.init < 0;
Comment #1 by hsteoh — 2018-01-16T21:12:38Z
`NaN` is not a template. Are you sure it should be on this list? :)
NaN assumes that you want to create a real nan;
The correct signature will be in my opinion:
NaN(F, T)(const T payload)
if (isFloatingPoint!T && isUnsigned!T)
This will allow to create float or double NaNs.
Ref abs: I don't understand exactly why we have abs and fabs. I think that dropping "fabs" and specializing "abs" for each numeric type is a better approach.
Comment #4 by hsteoh — 2018-01-16T22:12:07Z
Huh. I'm surprised there's such a thing as fabs in std.math. :-P
I agree that we should just unify everything under abs(). This is not C, where we can't overload abs() for integer vs. float arguments. Of course, fabs can still be left as an alias to abs() for backward compatibility.
We should probably file a separate bug to track this issue.
Comment #5 by hsteoh — 2018-01-16T22:15:17Z
W.r.t NaN, I agree it would be nice to be able to specify the exact type desired. I don't think the payload has to be a template parameter; it should just be ulong since all unsigned integral types will convert to ulong, and we can just take however many bits to fit into the payload of the requested type.
Again, I think this belongs in a separate issue so that it doesn't get lost amid the overloading issues.
Comment #6 by github-bugzilla — 2018-01-18T07:55:31Z
Commits pushed to master at https://github.com/dlang/phoboshttps://github.com/dlang/phobos/commit/113579ec6be26777198a2e13abe9913226a385e6
Fix issue 18244: Generic functions in std.math need to have proper sig constraints.
Not having any sig constraints causes needless conflicts with
user-defined numerical types, e.g.:
````
// myNumber.d
module myNumber;
struct MyNumber { ... }
int signbit(T : MyNumber)(T num) { ... }
// usercode.d
import myNumber;
import std.math;
MyNumber num;
auto x = signbit(num); // tries to call std.math.signbit, and fails
````
Add sig constraints to:
* signbit
* isFinite.
* isNormal.
* isSubnormal
* sgn.
* nextafter.
https://github.com/dlang/phobos/commit/e4da79f10f52b1a9cae1143c28b8b3b520efaa10
Merge pull request #6040 from quickfur/signbit_sig
Issue 18244: std.math generic functions need to have sig constraints
merged-on-behalf-of: Sebastian Wilzbach <[email protected]>
Comment #7 by github-bugzilla — 2018-02-24T11:36:41Z