NoobEditor
NoobEditor

Reputation: 15921

Error in determine positive\negative number in JAVA

Below snippet is to find the sign(+/-) of number without using > or < conditional operators basically!

    Scanner s = new Scanner(System.in);
    int n = s.nextInt(); /**take user input /

    /* stretch user input to either of infinity  */
    n *= Double.NEGATIVE_INFINITY;

    /* compare the result now */
    if(n == Double.NEGATIVE_INFINITY)
    {
     System.out.println("number is positive ");
    }
    else if (n == Double.POSITIVE_INFINITY )
    {
        System.out.println("number is negative" );  
    }
    else
    {
        System.out.println("could not determine number type!!" );
    }

I even added

System.out.println("nmbr and negtive infinity is : "+n+" "+Double.NEGATIVE_INFINITY);

for user input : -12, it shows :

/* Ideally it should be "Infinity -Infinity" according to me */
nmbr and negtive infinity is : 2147483647 -Infinity 

after doing the multiplication to see the resultant value, but for some reason. i dont get the nmbr equal to any infinite value

2 questions :

could not determine number type!!

Upvotes: 1

Views: 306

Answers (4)

rpax
rpax

Reputation: 4496

If you want to determine the sign of a number without < and >, you can read

Compute the sign of an integer - Bit Twiddling Hacks

Adapting it to java,

public static boolean isPositive(int v) {
 return (((v != 0) ? 1 : 0 )| (v >> (Integer.SIZE - 1))) != -1;
}

Note: In this example, 0 is considered positive

Upvotes: 1

SebastianH
SebastianH

Reputation: 2182

If you dont want to mess with bit arithmetics you could simply use standard Java API Integer.signum:

int signum = Integer.signum(n);
if (signum == 0)
  //n is 0
else if (signum == -1)
  //n is negative
else //(signum == 1)
  //n is positive 

Upvotes: 1

Harmlezz
Harmlezz

Reputation: 8078

Try this if you are interested only in int values:

Scanner s = new Scanner(System.in);
int n = s.nextInt(); /**take user input */

if ((n >> 31) == 0) System.out.println("number is positive");
else System.out.println("number is negative");

Alternatively for (n >> 31) == 0 you may use (x | 0x80000000) == 0

== UPDATE == as mentioned in your comment, you are interested in doubles. Same game:

Scanner s = new Scanner(System.in);
int n = s.nextInt(); /** take user input */

if (n * Double.NEGATIVE_INFINITY == Double.POSITIVE_INFINITY) System.out.println("number is negative");
else System.out.println("number is positive");

Upvotes: 2

Absurd-Mind
Absurd-Mind

Reputation: 8004

You have an implicit cast in you code. this:

n *= Double.NEGATIVE_INFINITY;

is roughly equal to:

n = (int) (n * Double.NEGATIVE_INFINITY);

n is an int in this code snippet, Double.NEGATIVE_INFINITY is a double, so the result of the calculation is a double. After that because you are saving your result in an int variable, your result gets downcasted to an int. ints do not have something like infinity, so your double gets casted to the highest possible Integer: Integer.MAX_VALUE == 2147483647.
It is obvious that this number is not infinity.

If you change your n to double, then your code is working

Upvotes: 2

Related Questions