11.1. Math UtilitiesJava supports integer and floating-point arithmetic directly in the language. Higher-level math operations are supported through the java.lang.Math class. As you may have seen by now, wrapper classes for primitive data types allow you to treat them as objects. Wrapper classes also hold some methods for basic conversions. Java provides the java.util.Random class for generating random numbers. First, a few words about built-in arithmetic in Java. Java handles errors in integer arithmetic by throwing an ArithmeticException: int zero = 0; try { int i = 72 / zero; } catch ( ArithmeticException e ) { // division by zero } To generate the error in this example, we created the intermediate variable zero. The compiler is somewhat crafty and would have caught us if we had blatantly tried to perform division by a literal zero. Floating-point arithmetic expressions, on the other hand, don't throw exceptions. Instead, they take on the special out-of-range values shown in Table 11-1.
The following example generates an infinite result: double zero = 0.0; double d = 1.0/zero; if ( d == Double.POSITIVE_INFINITY ) System.out.println( "Division by zero" ); The special value NaN (not a number) indicates the result of dividing zero by zero. This value has the special mathematical distinction of not being equal to itself (NaN != NaN evaluates to true). Use Float.isNaN( ) or Double.isNaN( ) to test for NaN. 11.1.1. The java.lang.Math ClassThe java.lang.Math class is Java's math library. It holds a suite of static methods covering all of the usual mathematical operations like sin( ), cos( ), and sqrt( ). The Math class isn't very object-oriented (you can't create an instance of Math). Instead it's really just a convenient holder for static methods that are more like global functions. As we saw in Chapter 6, it's possible to use the new Java 5.0 static import functionality to import the names of static methods and constants like this directly into the scope of our class and use them as if they were defined there. Table 11-2 summarizes the methods in java.lang.Math.
log( ), pow( ), and sqrt( ) can throw a runtime ArithmeticException. abs( ), max( ), and min( ) are overloaded for all the scalar values, int, long, float, or double, and return the corresponding type. Versions of Math.round( ) accept either float or double and return int or long, respectively. The rest of the methods operate on and return double values: double irrational = Math.sqrt( 2.0 ); // 1.414... int bigger = Math.max( 3, 4 ); // 4 long one = Math.round( 1.125798 ); // 1 For convenience, Math also contains the static final double values E and PI: double circumference = diameter * Math.PI; 11.1.2. The java.math PackageIf the long and double types are not large or precise enough for you, the java.math package provides two classes, BigInteger and BigDecimal, that support arbitrary-precision numbers. These full-featured classes have a bevy of methods for performing arbitrary-precision math and precisely controlling rounding of remainders. In the following example, we use BigDecimal to add two very large numbers and then create a fraction with a 100-digit result: long l1 = 9223372036854775807L; // Long.MAX_VALUE long l2 = 9223372036854775807L; System.out.println( l1 + l2 ); // -2 ! Not good. try { BigDecimal bd1 = new BigDecimal( "9223372036854775807" ); BigDecimal bd2 = new BigDecimal( 9223372036854775807L ); System.out.println( bd1.add( bd2 ) ); // 18446744073709551614 BigDecimal numerator = new BigDecimal(1); BigDecimal denominator = new BigDecimal(3); BigDecimal fraction = numerator.divide( denominator, 100, BigDecimal.ROUND_UP ); // 100 digit fraction = 0.333333 ... 3334 } catch (NumberFormatException nfe) { } catch (ArithmeticException ae) { } If you implement cryptographic or scientific algorithms for fun, BigInteger is crucial. Other than that, you're not likely to need these classes. 11.1.3. Random NumbersYou can use the java.util.Random class to generate random values. It's a pseudorandom number generator that is initialized with a 48-bit seed.[*] Because it's a pseudorandom algorithm, you'll get the same series of values each time you use the same seed value. The default constructor uses the current time to produce a seed, but you can specify your own value in the constructor:
long seed = mySeed; Random rnums = new Random( seed ); Once you have a generator, you can ask for one or more random values of various types using the methods listed in Table 11-3.
By default, the values are uniformly distributed. You can use the nextGaussian( ) method to create a Gaussian (bell curve) distribution of double values, with a mean of 0.0 and a standard deviation of 1.0. (Lots of natural phenomena follow a Gaussian distribution rather than a strictly uniform random one.) The static method Math.random( ) retrieves a random double value. This method initializes a private random number generator in the Math class the first time it's used, using the default Random constructor. Thus, every call to Math.random( ) corresponds to a call to nextdouble( ) on that random number generator. |