-
-
Notifications
You must be signed in to change notification settings - Fork 709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update math.algebraic.d#abs to return correct integral type #8861
Conversation
Integral types should simply return their own type.
Thanks for your pull request and interest in making D better, @HuskyNator! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla references
Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + phobos#8861" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the weird treatment of byte
and short
is a remnant from the transition to new integer promotion rules of unary minus, but since that transition is over, it's good to remove that code.
This needs a changelog entry or bugzilla issue.
std/math/algebraic.d
Outdated
@@ -43,24 +43,23 @@ import std.traits : CommonType, isFloatingPoint, isIntegral, isSigned, Unqual; | |||
* the return type will be the same as the input. | |||
* | |||
* Limitations: | |||
* Does not work correctly for signed intergal types and value `Num`.min. | |||
* Does not work correctly for value `Num`.min. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can't simply remove "signed intergal types and" because it does work correctly for Num.min
of unsigned or float types. It is worded poorly though.
* Does not work correctly for value `Num`.min. | |
* Does not work correctly for value `Num`.min of signed integral types because of overflow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that makes sense.
Would it be proper to explicitly defined this as undefined behaviour as well?
This seems reasonable to me as described below (#8861 (comment))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can't be undefined behavior without the function becoming @system
, and we want to keep abs
@safe
. You can make it unspecified behavior, but I don't see what's wrong with just telling it overflows and stays the same. Existing code might be checking for it.
std/math/algebraic.d
Outdated
@@ -43,24 +43,23 @@ import std.traits : CommonType, isFloatingPoint, isIntegral, isSigned, Unqual; | |||
* the return type will be the same as the input. | |||
* | |||
* Limitations: | |||
* Does not work correctly for signed intergal types and value `Num`.min. | |||
* Does not work correctly for value `Num`.min. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should also say more clearly what it actually does rather than just "doesn't work correctly" — in fact, D has defined semantics here so is it wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand what behaviour of abs
you expect in this case.
"Calculates the absolute value of a number" is not representable in the case of int.min
. From where I'm standing this is currently undefined. Do we expect saturation, zero, identity? (int.max
/ 0
/ int.min
)
Do we even want to define behaviour for abs(Num.min)
at all?
Under the current code the return value of Num.min
, in the case of signed integral types, will be itself (identity), as -Num.min
will return itself (overflow). I view this more as an implementation side-effect.
I don't believe this should be defined behaviour.
As an example, this is what cpp's (int/long/long long) std::abs
states:
The behavior is undefined if the result cannot be represented by the return type.
On a side-tangent, I've searched for the specifications of unary -
before, but I believe these have been implicitly borrowed from C(++)
. I've been unable to locate them in the documentation. This goes for both -Num.min
and -unsigned
: https://discord.com/channels/242094594181955585/242122752436338688/1169211488062406686 .
This should definitely be explicitly defined behaviour.
I'm not sure how or where to add this (and have difficulty accessing bugzilla at all). |
I filed an issue: https://issues.dlang.org/show_bug.cgi?id=24278 |
Reorder attributes as per style guidelines: > Attributes should be listed in alphabetical ordering Fix issue 24278
I'm not sure why 2 tests fail. Their error message suggests a different issue. |
cc @kinke is this caused by your recent makefile changes? |
Yes; fix here: dlang/dmd#15911 |
The documentation states the return type for an argument of integral types (assuming under phobos: (u)byte,short,int,long) should equal its own type.
Under current implementation this is not the case. Exemptions are made for short and byte.
Using
isIntegral
instead yields the correct behavior, also for unsigned types (which simply return themselves).