Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The power operator in Java?

When I run the following Visual Basic code :

Dim b As Double
b = (2 ^ 16 - 1) * Math.Sqrt(Math.Sqrt((a / (2 ^ 8 - 1))))

(Assuming a is a double whose value is 15.0)
The result I get for b is about 32,275.

But when I run the following Java code, which is supposed to do the same as above:

double b;
b = (2 ^ 16 - 1) * Math.sqrt(Math.sqrt((a / (2 ^ 8 - 1))));

Again with a being 15, I get a much different result: about 17.

Both are solving this equation:

enter image description here

Why is this so? For what I'm working on, the Visual Basic yields result I'm looking for.

like image 506
Kenan Avatar asked Aug 18 '11 01:08

Kenan


People also ask

How do you find the power of 2 in Java?

Another solution is to keep dividing the number by two, i.e, do n = n/2 iteratively. In any iteration, if n%2 becomes non-zero and n is not 1 then n is not a power of 2. If n becomes 1 then it is a power of 2.

How do you write a power of 10 in Java?

Math. pow(10, exponent)

How do you write 10 to the power 9 in Java?

Since 109 fits into an int and is also exactly representable as a double , you can do this: int exp = (int) Math. pow(10, 9); BigInteger answer = BigInteger.


1 Answers

^ is XOR operator in java. Use Math.pow(2,8) which is 2 ^ 8 in Visual Basic.

like image 125
Eng.Fouad Avatar answered Nov 15 '22 19:11

Eng.Fouad