I want to check to see if given integers digits a, b are both positive or both negative. How do i do that in java.
how to check below number 'a' is negative or not in if condition using signum method?
Above line is not working
Please advise. Any sample code, resources highly appreciated. thanks in advance.
Your code worked fine for me, though I would personally add the other two possible conditions in case the issue is with the value of the input:
That generates the expected given number 'a' is a negative number message on my machine.
Have you validated that the value of 'a' is indeed a negative number in your test cases?
Chris R Barrett wrote:That said, I would think in this situation a less than zero or greater than zero condition check would generate the same result without the Integer.signum() method call:...
@sai rama krishna: You can certainly use signum() if you want; however, it's worth remembering that the method returns three values. If you're only interested in knowing whether a number is negative or "not negative", then I reckon simple '< 0' and '>= 0' tests may be better - and probably easier to read too.
signum() is generally used for sign multiplication - where it's extremely useful - not so much for comparison.
[Edit] Caveat: If the values you're trying to compare are objects (ie, Integers), rather than ints, then signum() may well be the better way to go. This is because a '< 0' comparison will involve auto-boxing (actually probably unboxing), and the equivalent "typed" comparison ('x.compareTo(anIntegerContainingZero) < 0') is a lot more verbose.
Campbell Ritchie wrote:I thought the idea was to compare whether two numbers are both negative or positive.
Indeed, that was OP's original question, but it got hidden under a lot of talk about signs.
So, OP is on the right track. If he has two numbers, each being -1, 0 or 1, then how
can OP tell instantly if the two original numbers are both either positive or negative?
OP asked for advice. Having a reread about all these signums, and
checking for positives or zeroes in one operation, I'd say: let's keep
both feet to the ground and use:
or, given a lot of previous topics:
And I challenge Campbell to come up with a stream and a lambda (assuming he has Java 8).
sai rama krishna wrote: what is the difference of these two approaches.
Outside of one approach being more verbose than the other, nothing.
You could shorten that code even farther with just:
return (a < 0 && b < 0) || (a > 0 && b > 0);
Of course, this solution will return false if both a and b are zero.
I had a slightly more general method, but I just changed it as to show an
alternative (albeit one that is far less simple):
The strange thing is: in one minute we write a classic Java solution, but
we're willing to spend a whole evening coming up with an equal stream solution,
feeling proud too. Remarkable.
sai rama krishna wrote:both zeros is not a concern for me now. Thank you
Actually, I suggest you think about that carefully before you answer categorically.
The title of your thread was "comparing sign of a number in java", and signum() treats 0 differently from any other value.
In strictly binary terms, the "sign" of a 0 is '+', because the sign bit indicates whether a number is negative or not, so a "check to see if given integers digits a, b are both positive or both negative" can be interpreted in more than one way:
(signum(a) > 0 && signum(b) > 0) || (signum(a) < 0 && signum(b) < 0)
will return false if either a OR b are 0.
It might also be worth mentioning that, since signum() can only return 3 values: -1, 0 or 1, and one of those values (0) is only returned if the number itself is 0, the above can also be written as:
signum(a) != 0 && signum(a) == signum(b)
a != 0 && signum(a) == signum(b)
Now that may be exactly what you want, in which case you can forget the rest of this post; but if you had a "sign()" method:which returns the sign bit of a number, then
sign(a) == sign(b)
would be true if both numbers are negative or both numbers are > or equal to 0; but you can still eliminate 0's from the result with:
sign(a) == sign(b) && a != 0 && b != 0