Math operations not working!
Hi,
I'm running java on a Mac using OSX and XCode for an IDE. When I try and use the 'to the power' operation it behaves in an unexpected manner.
int minbit = Size - 1;
if(minbit < 0) minbit = 0;
int Min = -1*2^minbit;
int Max = (2^minbit) - 1;
System.out.println("Size = " + Size +", minbit = " + minbit +", Min = " + Min +", Max = " + Max);
return Raw >= Min && Raw <= Max;
Gives the result:
Size = 2, minbit = 1, Min = -1, Max = 2
Size = 3, minbit = 2, Min = -4, Max = -1
Size = 4, minbit = 3, Min = -3, Max = 0
I would expect:
Size = 2, minbit = 1, Min = -2, Max = 1
Size = 3, minbit = 2, Min = -4, Max = 3
Size = 4, minbit = 3, Min = -8, Max = 7
I've no idea what's going on. I thought may be it was the character I was using for the 'power of' sign but I've tried copying it of the web....
Any help'd be appreciated... I'm using a for loop at the moment which is far from ideal.
[1289 byte] By [
abc.mikeya] at [2007-11-26 18:46:14]

Like this:
package cruft;
/**
* Power
* User: Michael
* Date: Feb 17, 2007
* Time: 8:58:11 AM
*/
public class Power
{
public static void main(String[] args)
{
double x = ((args.length > 0) ? Double.parseDouble(args[0]) : 2);
double n = ((args.length > 1) ? Double.parseDouble(args[1]) : 3);
System.out.println("x = " + x);
System.out.println("n = " + n);
System.out.println("pow(x, n) = " + Math.pow(x, n));
}
}
%
> > Ever so true but maybe in this particular case '2**x'
> > for 'x >= 0' the expression '1<<x' would be more efficient.
>
> Jos, your FORTRAN roots are still showing... 8)
:-) ok, you win:/**
* some babble here about 1<<x being equal to 2**x
* @parameter power some more babble about 2**x
* @return and even more babbling about 2**x
*/
public final int raiseTwoToThePowerOf(int power) {
assert power >= 0 : "not a non-negative power: "+power;
int resultOfTwoRaisedToThePowerOfANumber= 1<<power;
return resultOfTwoRaisedToThePowerOfANumber;
}
kind regards,
Jos ;-)
ps. stupid forum sofware ...>