The Math Package

Let's introduce another of the standard classes. This one is called java.lang.Math and it has a couple of dozen useful mathematical functions and constants, including trig routines (watch out—these expect an argument in radians, not degrees), pseudorandom numbers, square root, rounding, and the constants pi and e.

There are two methods in Math to convert between degrees and radians:

public static double toDegrees(double); // new in JDK 1.2
public static double toRadians(double); // new in JDK 1.2

You'll need these when you call the trig functions if your measurements are in degrees.

You can review the source of the Math package at $JAVAHOME/src/java/lang/Math.java and in the browser looking at the Java API.

The Math.log() function returns a natural (base e) logarithm. Convert natural logarithms to base 10 logarithms with code like this:

double nat_log = ...

double base10log = nat_log / Math.log(10.0);

The list of members in the java.lang.Math class is:

public final class Math {
     public static final double E = 2.7182818284590452354;
     public static final double PI = 3.14159265358979323846;
     public static native double IEEEremainder(double, double);
     public static double abs(double);
     public static float abs(float);
     public static int abs(int);
     public static long abs(long);

// trig functions
     public static double toDegrees(double);
     public static double toRadians(double);
     public static native double sin(double);
     public static native double cos(double);
     public static native double tan(double);
     public static native double asin(double);
     public static native double acos(double);
     public static native double atan(double);
     public static native double atan2(double, double);
     public static native double exp(double);
     public static native double pow(double, double);
     public static native double log(double);
     public static native double sqrt(double);

// rounding and comparing
     public static native double ceil(double);
     public static native double floor(double);
     public static double max(double, double);
     public static float max(float, float);
     public static int max(int, int);
     public static long max(long, long);
     public static double min(double, double);
     public static float min(float, float);
     public static int min(int, int);
     public static long min(long, long);
     public static long round(double);
     public static int round(float);

// returns a random number between 0.0 and 1.0
     public static synchronized double random();

// rounds the argument to an integer, stored as a double
     public static native double rint(double);
}

Originally, all the Math methods were “strictfp”, meaning that the JVM could not use an extended exponent range to represent intermediate results on x86 hardware. That was changed in JDK 1.3, which introduced the package java.lang.StrictMath. StrictMath and Math have the same API. Use StrictMath when absolute portability of numeric results is an issue for you.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.57.172