How would you be able to change Math.sin
, Math.cos
and Math.tan
into an int
?
This is because I heard that it is useful to have those Math resources in movement, gravity, etc. in gaming.
Also, as a little inside question, does anyone know if there is a method where you can square or cube a number instead of doing:
(Pseudo Example)
9 * 9 * 9
?

- 73,224
- 17
- 184
- 273

- 49
- 1
- 5
1 Answers
You can convert any float
or double
to an int
with a cast. For example:
int integerResult = (int)Math.sin(variable);
However, this really does not make sense. Since the result of Math.sin()
will always be from -1 to 1, this cast will always make the result 0 or -1 or 1. From the sound of your question you're not very sure about how to use these. I would strongly recommend you brush up on your math skills before attempting anything too complicated with your programming. You can, of course, use these in floating point format instead of as an integer.
Your second question. 9 * 9 * 9
is equivalent to 9 cubed. You could make your own method for the clearest intent:
int cube(int input) {
//return the input cubed
return input*input*input;
}
Or use the built in function Math.pow()
like so:
float nineCubed = Math.pow(9,3);
You can see check the java doc to see what functions Java has built in for math.
Finally, you can check out some of these math resources:
-
The result of Math.sin() will be from -1 to 1. I would recommend looking at Math.pow(), which handles squaring, cubing and so forth. For example Math.pow(9,3) = 999. – utdiscant Jun 17 '12 at 13:49
-
@TaylorGolden No problem. If this answer was helpful to you, you can accept it as your answer. And welcome to GDSE! – House Jun 17 '12 at 15:44