Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

comparing sign of a number in java

 
Ranch Hand
Posts: 542
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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?
if(Integer.signum(a)==-1){

Above line is not working

Please advise. Any sample code, resources highly appreciated. thanks in advance.
 
lowercase baba
Posts: 12792
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "is not working"? What does it do, and what do you expect it to do?
 
sai rama krishna
Ranch Hand
Posts: 542
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(Integer.signum(a)==-1){
System.out.println("given number 'a' is a negative number");
}

I check 'a' signature and if it is negative i want to print saying 'given number 'a' is a negative number'. How to achive that using Integer.signum(). please advise
 
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sai,

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 Barrett
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

A little bit less overhead, perhaps.
 
Marshal
Posts: 67386
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought the idea was to compare whether two numbers are both negative or positive.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


I agree.

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

HIH

Winston

[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.
 
Bartender
Posts: 3674
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?

Greetz,
Piet
 
Campbell Ritchie
Marshal
Posts: 67386
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… or both zero?
 
Chris Barrett
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I thought the idea was to compare whether two numbers are both negative or positive.


My bad. I read the OP's follow-up response to Mr. Rosenberger's clarification request and did not review in detail the original post. Lesson learned.
 
Piet Souris
Bartender
Posts: 3674
151
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,

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

Greetz,
Piet
 
sai rama krishna
Ranch Hand
Posts: 542
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





Above and below code seems almost same to me. what is the difference of these two approaches. please advise

 
Campbell Ritchie
Marshal
Posts: 67386
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second has much less unnecessary code to confuse you. There is a shorter way to write it:-
return (a < 0 && b < 0) || (a > 0 && b > 0);
That will not work when both values are 0.
 
Chris Barrett
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Chris Barrett
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...And Sheriff Ritchie beats me to the draw...
 
Campbell Ritchie
Marshal
Posts: 67386
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Chris R Barrett wrote:...http://i.imgur.com/7hXHLHZ.gif ...

I beg your pardon!!

It wasn't was it?
 
Chris Barrett
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, it was. For some reason the .gif wouldn't display, so I was trying to fix it to a .jpg before you noticed.

Even when I'm dead, you are still faster at the draw...


I hope the forum mods don't mind some pre-Halloween humour.
 
sai rama krishna
Ranch Hand
Posts: 542
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
both zeros is not a concern for me now. Thank you
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:And I challenge Campbell to come up with a stream and a lambda (assuming he has Java 8).


I'm not Campbell (obviously!) but I do have Java 8

edit For a general case of n ints:
 
Campbell Ritchie
Marshal
Posts: 67386
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Chris R Barrett wrote: . . .
I hope the forum mods don't mind some pre-Halloween humour.

We like any amount of humour as it is spelt with an O
 
Campbell Ritchie
Marshal
Posts: 67386
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done DB
 
Piet Souris
Bartender
Posts: 3674
151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, well done!

I had a slightly more general method, but I just changed it as to show an
alternative (albeit one that is far less simple):


applicable, say,


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.

Greetz,
Piet
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How else can one become familiar with new API?
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

HIH

Winston
 
What a show! What atmosphere! What fun! What a tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!