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

Specify negative-returning behavior of abs method #16707

Closed
klutzy opened this issue Aug 23, 2014 · 3 comments · Fixed by #16769
Closed

Specify negative-returning behavior of abs method #16707

klutzy opened this issue Aug 23, 2014 · 3 comments · Fixed by #16769

Comments

@klutzy
Copy link
Contributor

klutzy commented Aug 23, 2014

Currently ::std::i64::MIN.abs() returns ::std::i64::MIN.
I don't think this is absolutely "wrong", but then at least it should be specified that "abs() method can return negative values".

Currently std::num::abs() says it can return NaN for floats, but no mention for general situation.

@Gankra
Copy link
Contributor

Gankra commented Aug 23, 2014

For reference, this is the behavior of Java, C, and C++. But C# yields an error when this is done.

Java in particular documents Math.abs on ints to be:

Returns the absolute value of an int value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative.

While C# simply states that the input is:

A number that is greater than Int64.MinValue, but less than or equal to Int64.MaxValue.

A third alternative exists in making the output of min be the unsigned version of the input, which should be possible with associated types. You can actually do this today with std::int::MIN.abs() as uint. However I think this solution is generally not well accepted.

@robertg
Copy link

robertg commented Aug 23, 2014

Maybe we should document this so there is no confusion?

@klutzy
Copy link
Contributor Author

klutzy commented Aug 27, 2014

Similar issues for rationals:

extern crate num;
use std::i64::MIN;
use num::rational::Ratio;

let a = Ratio::new(MIN, 1);
a.abs() == a; // true

Hmm, I originally thought that we should settle some agreement about negative-returning behaviors and add some notes like "Note: implementation may return negative value if it is impossible to represent correctly".
But now I'm not sure if it is good design that Signed provides abs method at all.

bors added a commit to rust-lang-ci/rust that referenced this issue Mar 3, 2024
Export `SemanticsImpl` from `ra_ap_hir` crate, since it's already exposed via `Semantics.deref()`

The `SemanticsImpl` type is already de-facto exposed via `<Semantics as Deref>::Target`.

By not being part of the public crate interface it however doesn't get included in the documentation, resulting in a massive blind spot when it comes to `ra_ap_hir`'s type resolution APIs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants