# If Statement

• October 26th, 2010, 12:26 PM
Shyamz1
If Statement
Hi guys. Ive created a program that computes powers when typed in as command line arguments. I have the following code, but i cannot understand why the IF statement doesnt make the result 0 is a power 0 is typed in as a command line argument:

int mantissa = Integer.parseInt(args[0]);
int exponent = Integer.parseInt(args[1]);

for(int i=1; i < exponent; i++)

if (args[1] == 0)

regards

Shyam
• October 26th, 2010, 12:55 PM
copeg
Re: If Statement
See Common Java Mistakes, mainly several posts down to the one that describes Problem description: == operator or equals() method
• October 26th, 2010, 12:56 PM
javapenguin
Re: If Statement
Quote:

Originally Posted by Shyamz1
Hi guys. Ive created a program that computes powers when typed in as command line arguments. I have the following code, but i cannot understand why the IF statement doesnt make the result 0 is a power 0 is typed in as a command line argument:

int mantissa = Integer.parseInt(args[0]);
int exponent = Integer.parseInt(args[1]);

for(int i=1; i < exponent; i++)

if (args[1] == 0)

regards

Shyam

x ^ 0 = x/x. 0^0 = 0/0. That is indeterminate.

To get it to be negative exponents, simply have it inverse the result gotten by your for loop and then

do this :

public double negativeExponents(int x, int y)
{

if (x ==0 && y >=0)
return;

double d = 1/ otherMethod(num, -power);
return d;

}

I should note that this won't work for raising something to the 1/3 power or the square root, etc, unless you use doubles, but you're better off using the Math.pow(double num, double power) for those.

I've been able to get the compiler to output Indeterminate or Infinity or - Infinity for 0/0 and 1/0 and -1/0, respectively. However, it only does that in some cases for some odd reason. Also, Math.sqrt(-n) returns NaN, "Not a Number" I suppose it means, unless n is 0 or negative.
• October 26th, 2010, 12:57 PM
javapenguin
Re: If Statement
Quote:

Originally Posted by copeg
See Common Java Mistakes, mainly several posts down to the one that describes Problem description: == operator or equals() method

Wait, yeah, you're right, args[0] is a String and Strings are compared with .equals.