Math
Objective #1: Use the Math class for routine mathematical
tasks.
- The Math class is automatically imported with Java programs. It contains many useful methods and constants.
- You can use a reasonably good approximation of pi by referencing the constant Math.PI in a Java program. You can use Euler's contant, e, by referencing the constant Math.E.
- You can use a number of Math class methods (i.e. static methods) such as the following methods that you should know for the AP exam or class tests:
- Math.sqrt(x) - which obtains the square root of a value. Note that this method returns a double and not an int.
int num = Math.sqrt(10); and int num = Math.sqrt(9); cause errors since sqrt returns a double which cannot be assigned to an int even if the double is 3.0 for example.
- Math.pow(x, y) - which is used to compute x to the yth power
System.out.println(Math.pow(2, 8)); displays 256 which is 2 raised to the 8th power.
- Math.abs(x) - to
compute the absolute value of x
- Math.max(x, y) -
returns x or y, whichever is greater
System.out.println(Math.max(-2, 3)); displays 3 since 3 is greater than -2
System.out.println(Math.max(Math.max(1, 4), 2)); displays 4
- Math.min(x, y) -
returns x or y, whichever is least
- Math.round(x) - to
round x to the closest integer
System.out.println(Math.round(4.5)); displays 5
System.out.println(Math.round(4.4)); displays 4
- Math.ceil(x) - to
find the smallest integer that is greater than or equal to x. Note that this method returns a double so the statement int num = Math.ceil(2.3); causes a compile error.
System.out.println(Math.ceil(3.1)); displays 4
System.out.println(Math.ceil(-2.9)); displays -2
- Math.floor(x) - to
find the largest integer that is less than or equal to x. Note that this method returns a double so the statement int num = Math.floor(2.3); causes a compile error.
System.out.println(Math.floor(3.9)); displays 3
System.out.println(Math.floor(-2.1)); displays -3
- Math.random() -
returns a pseudorandom floating-point value greater than or equal to
zero but less than 1; that is a value in the range [0, 1)
You are not responsible for memorizing or using the methods below on the AP exam or class tests but they are convenient.
- Math.sin(x) - to compute the sine of x in radians
- Math.cos(x) - to compute the cosine of x in radians
- Math.tan(x) - to compute the tangent of x in radians
- Math.toRadians(x) - to convert x radians to degrees (i.e. returns x * 180/Math.PI)
- Math.toDegrees(x) - to convert x degrees to radians (i.e. returnx x * Math.PI/180)
- Math.exp(x) - to compute e to the xth power
- Math.log(x) - to
compute the natural log of x (i.e. ln(x) where x > 0)
- The methods above are called static methods. You
do not need to instantiate a Math object
in order to use the methods. You simply type the name of the class Math followed
by a dot operator and then the name of the method.
- Here is a neat way to display the maximum value of
three values without using
an if statement:
System.out.println(Math.max(Math.max(x, y), z));