posted 3 years ago

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.

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.

sai rama krishna

Ranch Hand

Posts: 536

1

posted 3 years ago

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

Have you validated that the value of 'a' is indeed a negative number in your test cases?

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?

posted 3 years ago

I agree.

@sai rama krishna: You can certainly use

HIH

Winston

[Edit] Caveat: If the values you're trying to compare are

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,

`Integer`s), rather than

`int`s, 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.

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

posted 3 years ago

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

Greetz,

Piet

Campbell Ritchie wrote:I thought the idea was to compare whether

twonumbers 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

Piet Souris

Rancher

Posts: 2455

80

posted 3 years ago

- 1

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

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

Greetz,

Piet

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

1

Campbell Ritchie

Marshal

Posts: 58411

178

posted 3 years ago

Outside of one approach being more verbose than the other, nothing.

You could shorten that code even farther with just:

Of course, this solution will return

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.

Campbell Ritchie

Marshal

Posts: 58411

178

sai rama krishna

Ranch Hand

Posts: 536

1

Campbell Ritchie

Marshal

Posts: 58411

178

Piet Souris

Rancher

Posts: 2455

80

posted 3 years ago

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

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

posted 3 years ago

Actually, I suggest you think about that carefully before you answer categorically.

The title of your thread was "comparing

In strictly binary terms, the "sign" of a 0 is '

will return

It might also be worth mentioning that, since

or indeed

Now that may be exactly what you want, in which case you can forget the rest of this post; but if you had a "

would be true if both numbers are negative or both numbers are >

HIH

Winston

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

Articles by Winston can be found here