Question

I'm trying to make a number squared (for example, x^2) in Android code, but I get this error:

The operator ^ is undefined for the argument type(s) int, boolean

is there a different way to square a number/variable in Android?

Was it helpful?

Solution

do same as you do it in core java,use. Math.pow(yournumber,power) works like yournumber^power.

import java.lang.*;

public class MathDemo {

   public static void main(String[] args) {

  // get two double numbers
  double x = 2.0;
  double y = 5.4;

  // print x raised by y and then y raised by x
  System.out.println("Math.pow(" + x + "," + y + ")=" + Math.pow(x, y));// works like x^y
  System.out.println("Math.pow(" + y + "," + x + ")=" + Math.pow(y, x));//works like y^x

   }
}

OTHER TIPS

You can use java's Math.pow() in android too

double power = Math.pow(2,2);

If you just need an integer squared, you can do x*x.

Careful, as it might overflow.

Another option is java.lang.Math#pow, but that works on floating point numbers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top