Bug 18244 – Generic functions in std.math cannot be overloaded

Status
REOPENED
Severity
major
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-01-16T19:39:31Z
Last change time
2024-12-01T16:31:56Z
Keywords
pull
Assigned to
No Owner
Creator
Răzvan Ștefănescu
Moved to GitHub: phobos#10291 →

Comments

Comment #0 by rumbu — 2018-01-16T19:39:31Z
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? :)
Comment #2 by hsteoh — 2018-01-16T21:31:04Z
https://github.com/dlang/phobos/pull/6040 Note that currently I'm not touching abs() because the sig constraint is just too complex.
Comment #3 by rumbu — 2018-01-16T21:48:42Z
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/phobos https://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
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/a74271be4c93cf95905265597ac9bca426378f55 Revert "Issue 18244: std.math generic functions need to have sig constraints" https://github.com/dlang/phobos/commit/230292aef0353efa25ba5e298c30cdf5ce6717b5 Merge pull request #6213 from dlang/revert-6040-signbit_sig Revert "Issue 18244: std.math generic functions need to have sig constraints" merged-on-behalf-of: Iain Buclaw <[email protected]>
Comment #8 by bugzilla — 2019-12-12T16:37:12Z
Before this can be fixed, the alias this problem mentioned in issue 18287 should be removed.
Comment #9 by Ajieskola — 2021-11-01T16:55:44Z
I think this issue has too high severity. You can define your own function if you really need overloads after all. Decreasing to major.
Comment #10 by robert.schadek — 2024-12-01T16:31:56Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10291 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB