-
Notifications
You must be signed in to change notification settings - Fork 49
Standard usage of signum is <, >, ==. Not direct comparison with -1, 0, 1 #1887
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
base: master
Are you sure you want to change the base?
Standard usage of signum is <, >, ==. Not direct comparison with -1, 0, 1 #1887
Conversation
| @AlsoNegation | ||
| boolean after(BigDecimal value) { | ||
| return value.signum() == -1; | ||
| return value.signum() < 0; |
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.
We'll likely want to make the matching changes to this expected output file:
Lines 32 to 54 in 62077da
| ImmutableSet<Boolean> testBigDecimalSignumIsPositive() { | |
| return ImmutableSet.of( | |
| BigDecimal.valueOf(1).signum() == 1, | |
| BigDecimal.valueOf(2).signum() == 1, | |
| BigDecimal.valueOf(3).signum() == 1, | |
| BigDecimal.valueOf(4).signum() == 1, | |
| BigDecimal.valueOf(5).signum() != 1, | |
| BigDecimal.valueOf(6).signum() != 1, | |
| BigDecimal.valueOf(7).signum() != 1, | |
| BigDecimal.valueOf(8).signum() != 1); | |
| } | |
| ImmutableSet<Boolean> testBigDecimalSignumIsNegative() { | |
| return ImmutableSet.of( | |
| BigDecimal.valueOf(1).signum() == -1, | |
| BigDecimal.valueOf(2).signum() == -1, | |
| BigDecimal.valueOf(3).signum() == -1, | |
| BigDecimal.valueOf(4).signum() == -1, | |
| BigDecimal.valueOf(5).signum() != -1, | |
| BigDecimal.valueOf(6).signum() != -1, | |
| BigDecimal.valueOf(7).signum() != -1, | |
| BigDecimal.valueOf(8).signum() != -1); | |
| } |
|
Thanks for reporting this @henri-tremblay! There's some history behind the current setup, but indeed we may want to revisit this. I'll have a closer look at the PR later ~today. (Don't worry about fixing the tests; we can take care of that.) In the interim I'll CC @Venorcis, as he also had opinions. |
bcda1c9 to
498c7e6
Compare
|
Reading the comments from #812 , you were saying the same thing as me @Stephan202 , then changed your mind. I liked the way you thought originally :-) |
Thanks, I do indeed 😄 As highlighted here when introducing this check, unlike the comparator, the signum is mathematically defined to have one of three values. This fact even has relevant implications, such as being able to use the signum for multiplication (and other more fancy derivations). The Wikipedia article on the Sign function also nicely summarizes this:
This makes it way more significant than what AI apparently proclaims to be an 'implementation detail'. I thus think it's good to keep this fact explicit, and not conflate the signum of a number with the number itself (which is what this change would do IMO). Something I could definitely get behind is allowing (i.e. not rewriting) the use of |
9ce4faa to
33892b7
Compare
|
Looks good. No mutations were possible for these changes. |
The
compareToin Java was made to match the mental comparison we would do with numbers. x - y < 0, x - y == 0, x - y > 0 would transfer to x.compareTo(y) < 0, x.compareTo(y) == 0, x.compareTo(y) > 0. We never directly compare to -1, 0, 1 to keep the lisibility.When using signum, it's the same thing. You would do x.signum() < 0 making it clear it's a negative number. When doing x.signum() == -1, you need to think about it. "What do -1 mean?" "Ah! It means negative".
I would highly suggest that error prone follows these guidelines. To be sure it wasn't only me, I asked some my favorite AI what it prefers. Here is the answer.