• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doh! I can't believe I don't know this....Square a number?

 
Will Carpenter
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What operator do I use to square a number in java?
Given: int x = 3;
x**2; //is it this?
x^2; //maybe this?
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no primitive for this. "^" is the bitwise exclusive or operator, and ** isn't anything. You'll have to type out x*x or use Math.pow().
 
Parth Sagdeo
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yep, or more specifically, for all powers, just go Math.pow(base,exponent)
 
Gjorgi Var
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And both "base" and "exponent" are of type double...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic