The Math class contains the methods needed to perform basic mathematical functions. You have already used the pow(a, b) method to compute a b in Listing 2.6, ComputeLoan.java, and the Math.random() in Listing 3.5, SubtractionTutor.java. This section introduces other useful methods in the Math class. They can be categorized as trigonometric methods , exponent methods , and service methods . Besides methods, the Math class provides two useful double constants, PI and E (the base of natural logarithms). You can use these constants as Math.PI and Math.E in any program.
The Math class contains the following trigonometric methods:
public static double sin(double radians) public static double cos(double radians) public static double tan(double radians) public static double asin(double radians) public static double acos(double radians) public static double atan(double radians) public static double toRadians(double degree) public static double toDegrees(double radians)
Each method has a single double parameter, and its return type is double . The parameter represents an angle in radians. One degree is equal to p/180 in radians. For example, Math.sin(Math.PI) returns the trigonometric sine of p. Since JDK 1.2, the Math class has also provided the method toRadians(double angdeg) for converting an angle in degrees to radians, and the method toDegrees(double angrad) for converting an angle in radians to degrees.
For example,
Math.sin( ) returns 0.0 Math.sin(Math.toRadians( 270 )) returns -1.0 Math.sin(Math.PI / 6 ) returns 0.5 Math.sin(Math.PI / 2 ) returns 1.0 Math.cos( ) returns 1.0 Math.cos(Math.PI / 6 ) returns 0.866 Math.cos(Math.PI / 2 ) returns
There are five methods related to exponents in the Math class:
/** Return e raised to the power of x (ex) */ public static double exp( double x) /** Return the natural logarithm of x (ln(x) = loge(x)) */ public static double log( double x) /** Return the base 10 logarithm of x (log10(x)) */ public static double log10( double x) /** Return a raised to the power of b (xb) */ public static double pow( double x, double b) /** Return the square root of a ( ) */ public static double sqrt( double x)
Note that the parameter in the sqrt method must not be negative.
For example,
Math.exp( 1 ) returns 2.71828 Math.log( Math.E ) returns 1.0 Math.log10( 10 ) returns 1.0 Math.pow( 2 , 3 ) returns 8.0 Math.pow( 3 , 2 ) returns 9.0 Math.pow( 3.5 , 2.5 ) returns 22.91765 Math.sqrt( 4 ) returns 2.0 Math.sqrt( 10.5 ) returns 3.24
The Math class contains five rounding methods:
/** x rounded up to its nearest integer. This integer is * returned as a double value. */ public static double ceil( double x) /** x is rounded down to its nearest integer. This integer is * returned as a double value. */ public static double floor( double x) /** x is rounded to its nearest integer. If x is equally close * to two integers, the even one is returned as a double. */ public static double rint( double x) /** Return (int)Math.floor(x + 0.5). */ public static int round( float x) /** Return (long)Math.floor(x + 0.5). */ public static long round( double x)
For example,
Math.ceil( 2.1 ) returns 3.0 Math.ceil( 2.0 ) returns 2.0 Math.ceil( “2.0 ) returns “2.0 Math.ceil( “2.1 ) returns -2.0 Math.floor( 2.1 ) returns 2.0 Math.floor( 2.0 ) returns 2.0
Math.floor( -2.0 ) returns “2.0 Math.floor( -2.1 ) returns -3.0 Math.rint( 2.1 ) returns 2.0 Math.rint( 2.0 ) returns 2.0 Math.rint( -2.0 ) returns “2.0 Math.rint( -2.1 ) returns “2.0 Math.rint( 2.5 ) returns 2.0 Math.rint( -2.5 ) returns -2.0 Math.round( 2.6f ) returns 3 // Returns int Math.round( 2.0 ) returns 2 // Returns long Math.round( “2.0f ) returns -2 Math.round( “2.6 ) returns -3
The min and max methods are overloaded to return the minimum and maximum numbers between two numbers ( int , long , float , or double ). For example, max(3.4, 5.0) returns 5.0 , and min(3, 2) returns 2 .
The abs method is overloaded to return the absolute value of the number ( int , long , float , and double ). For example,
Math.max( 2 , 3 ) returns 3 Math.max( 2.5 , 3 ) returns 3.0 Math.min( 2.5 , 3.6 ) returns 2.5 Math.abs( -2 ) returns 2 Math.abs( -2.1 ) returns 2.1
The Math class also has a powerful method, random , which generates a random double value greater than or equal to 0.0 and less than 1.0 ( 0.0 <= Math.random() < 1.0 ). This method is very useful. Listing 3.5, SubtractionTutor.java, uses this method to generate a single-digit integer. You can use it to write a simple expression to generate random numbers in any range. For example,
In general,
Tip
You can view the complete documentation for the Math class online at http://java.sun.com/j2se/1.5.0/docs/api/index.html, as shown in Figure 5.10. You can also download jdk-1_5_0-doc.zip from the same website and install it on your PC so that you can browse the documents locally. Figure 5.10. You can view the documentation for Java API online.(This item is displayed on page 145 in the print version) |
Note
Not all classes need a main method. The Math class and JOptionPane class do not have main methods. These classes contain methods for other classes to use. |