dlangBugzillaToGithub / migration_test

0 stars 0 forks source link

Generic functions in std.math cannot be overloaded #1104

Open dlangBugzillaToGithub opened 6 years ago

dlangBugzillaToGithub commented 6 years ago

rumbu reported this on 2018-01-16T19:39:31Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=18244

CC List

Description

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;
dlangBugzillaToGithub commented 6 years ago

hsteoh commented on 2018-01-16T21:12:38Z

`NaN` is not a template. Are you sure it should be on this list? :)
dlangBugzillaToGithub commented 6 years ago

hsteoh commented on 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.
dlangBugzillaToGithub commented 6 years ago

rumbu commented on 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.
dlangBugzillaToGithub commented 6 years ago

hsteoh commented on 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.
dlangBugzillaToGithub commented 6 years ago

hsteoh commented on 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.
dlangBugzillaToGithub commented 6 years ago

github-bugzilla commented on 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 <sebi.wilzbach@gmail.com>
dlangBugzillaToGithub commented 6 years ago

github-bugzilla commented on 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 <ibuclaw@gdcproject.org>
dlangBugzillaToGithub commented 4 years ago

bugzilla commented on 2019-12-12T16:37:12Z

Before this can be fixed, the alias this problem mentioned in issue 18287 should be removed.
dlangBugzillaToGithub commented 3 years ago

Ajieskola commented on 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.