I'm not sure how to write an equation in Netbeans.
The equation is supposed to be: (5−x)^2 +(5−y)^2
all under a square root.
This is what I have tried:
public static int getScore(int x, int y){
return ( (((5-x)^2 + (5-y)^2))^(1/2) );
I'm not sure how to write an equation in Netbeans.
The equation is supposed to be: (5−x)^2 +(5−y)^2
all under a square root.
This is what I have tried:
public static int getScore(int x, int y){
return ( (((5-x)^2 + (5-y)^2))^(1/2) );
This is one of those cases where there's a specialized library function:
return Math.hypot(5-x, 5-y);
This avoids the overflow and underflow issues in computing the square root of the sum of squares directly
The carat ^ performs exclusive or operator in java, which is a bits thing. Don't use it for exponents.
The expression you are looking for is
return Math.sqrt(Math.pow(5 - x, 2) + Math.pow(5 - y, 2)));