The Final Version of the Math2 Class


The Final Version of the Math2 Class

This is what the Math2.java source code looks like after we are finished implementing the methods . The LN10 constant is used in the log10() method. The TWOPI constant and c[] coefficients are used in the gamma() method.

 package TechJava.MathLib; public class Math2 {   private static final double LN10 = Math.log(10.0);   private static final double TWOPI =                            Math.sqrt(2.0*Math.PI);   private static double c[] = {1.000000000190015,              76.18009172947146, -86.50532032941677,              24.01409824083091, -1.231739572450155,             0.1208650973866179, -0.5395239384953e-5};   public static double log10(double a) {     return Math.log(a)/LN10;   }   public static double logX(double a, double b) {     return Math.log(a)/Math.log(b);   }   public static double sinh(double a) {     return 0.5*(Math.exp(a) - Math.exp(-a));   }   public static double cosh(double a) {     return 0.5*(Math.exp(a) + Math.exp(-a));   }   public static double tanh(double a) {     double e1 = Math.exp(a);     double e2 = Math.exp(-a);     return (e1-e2)/(e1+e2);   }   public static double gamma(double z) {     double grp1, grp2, grp3;     int j;     grp1 = z + 0.5;     grp2 = z + 5.5;     grp3 = c[0];     for(j=1; j<7; ++j) {       grp3 += c[j]/(z+j);     }     return Math.pow(grp2,grp1)*            Math.exp(-grp2)*TWOPI*grp3/z;    } } 

With the built-in math capability of Java and the logarithm and hyperbolic trigonometric methods defined in the Math2 class, we have exceeded the intrinsic math capability of C and C++. It took about 20 lines of code to do that. Even complicated formulas such as the gamma function can be included in your user -defined math libraries with relatively little effort.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net