Skip to content
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

Merged
merged 6 commits into from
Dec 15, 2023

Conversation

HuskyNator
Copy link
Contributor

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).

@dlang-bot
Copy link
Contributor

dlang-bot commented Dec 10, 2023

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 verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

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

Auto-close Bugzilla Severity Description
24278 minor std.math.abs promotes unsigned argument to 32 bits

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#8861"

Copy link
Contributor

@dkorpel dkorpel left a 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.

@@ -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.
Copy link
Contributor

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.

Suggested change
* Does not work correctly for value `Num`.min.
* Does not work correctly for value `Num`.min of signed integral types because of overflow.

Copy link
Contributor Author

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))

Copy link
Contributor

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 Show resolved Hide resolved
@@ -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.
Copy link
Member

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?

Copy link
Contributor Author

@HuskyNator HuskyNator Dec 12, 2023

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.

@HuskyNator
Copy link
Contributor Author

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.

I'm not sure how or where to add this (and have difficulty accessing bugzilla at all).
If truly necessary, I'd gladly have some help 😅.

@dkorpel
Copy link
Contributor

dkorpel commented Dec 12, 2023

I filed an issue: https://issues.dlang.org/show_bug.cgi?id=24278
If you include "Fix 24278" or "Fix issue 24278" in one of the commit messages, the bot will auto-close the issue and add it to the change log.

Reorder attributes as per style guidelines:
> Attributes should be listed in alphabetical ordering

Fix issue 24278
@HuskyNator
Copy link
Contributor Author

I'm not sure why 2 tests fail. Their error message suggests a different issue.

@thewilsonator
Copy link
Contributor

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?

@dkorpel dkorpel merged commit 0612f55 into dlang:master Dec 15, 2023
12 of 14 checks passed
@kinke
Copy link
Contributor

kinke commented Dec 15, 2023

is this caused by your recent makefile changes?

Yes; fix here: dlang/dmd#15911

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants