Abstract

The Math class is a wrapper class for a set of useful mathematical functions and constants.  All of the methods and fields are static, and there is no public constructor. 

Package

java.lang

Subclass of

java.lang.Object
java.lang.Math

Static Public Fields

double E
The base of natural logarithms, 2.718...
double PI
The ratio of the circumference of a circle to its diameter, 3.14159...

Common Static Public Methods

static double abs (double)
Returns the absolute value of the argument
static float abs (float)
Returns the absolute value of the argument
static int abs (int)
Returns the absolute value of the argument
static long abs (long)
Returns the absolute value of the argument
static double acos (double)
Returns the arccos of the argument (in radians), in the range (0, pi)
static double asin (double)
Returns the arcsin of the argument (in radians), in the range (-pi/2, pi/2)
static double atan (double)
Returns the arctan of the argument (in radians), in the range (-pi/2, pi/2)
static double atan2 (double y, double x)
Returns the arctan of y/x (in radians), in the range [0, 2*pi)
static double cos (double)
Returns the cosine of the argument (in radians)
static double exp (double)
Returns e raised to the power of the argument
static double log (double)
Returns the natural logarithm of the argument
static double pow (double base, double exp)
Returns the base raised to the exp power
static double random ()
Returns a pseudorandom number in the range [0, 1)
static long round (double)
Returns the nearest integer to the argument
static int round (float)
Returns the nearest integer to the argument
static double sin (double)
Returns the sine of the argument (in radians)
static double sqrt (double)
Returns the square root of the argument
static double tan (double)
Returns the tangent of the argument (in radians)
static double toDegrees (double)
Returns the conversion of the argument from radians to degrees
static double toRadians (double)
Returns the conversion of the argument from degrees to radians

Example

Code

import java.applet.Applet;
import java.awt.*;

public class Trig1 extends Applet {

  public void paint (Graphics g) {
    double x;
    double y;
    int X0;
    int Y0;
    int X1;
    int Y1;
    int X2;
    int Y2;
    int C1;
    int C2;
    int T1;
    int T2;
    double ScaleX;
    double ScaleY;
    double Inc;

    Y0 = 160;
    x = - 5.0;
    ScaleX = 50;
    ScaleY = 50;
    Inc = 1./ScaleX;
    X0 = 250;
    y = Math.sin (x);
    X1 = X0 + (int) Math.round (ScaleX * x);
    Y1 = Y0 - (int) Math.round (ScaleY * y);
    y = Math.cos (x);
    C1 = Y0 - (int) Math.round (ScaleY * y);
    y = Math.tan (x);
    T1 = Y0 - (int) Math.round (ScaleY * y);
    g.setColor (Color.black);
    g.drawLine (0, Y0, X0 + X0, Y0);
    g.drawLine (X0, 0, X0, Y0 + Y0);
    while (x < 5.0) {
      X2 = X0 + (int) Math.round (ScaleX * x);
      y = Math.tan (x);
      T2 = Y0 - (int) Math.round (ScaleY * y);
      if (Math.abs (y) < 4.0) {
        g.setColor (Color.green);
        g.drawLine (X1, T1, X2, T2);
      }
      y = Math.sin (x);
      Y2 = Y0 - (int) Math.round (ScaleY * y);
      g.setColor (Color.blue);
      g.drawLine (X1, Y1, X2, Y2);
      y = Math.cos (x);
      C2 = Y0 - (int) Math.round (ScaleY * y);
      g.setColor (Color.red);
      g.drawLine (X1, C1, X2, C2);
      X1 = X2;
      Y1 = Y2;
      C1 = C2;
      T1 = T2;
      x += Inc;
    }
    g.setColor (Color.red);
    g.drawString ("y = cos (x)", X0 - 60, Y0 - (int) ScaleY - 10);    
    g.setColor (Color.blue);
    g.drawString ("y = sin (x)", X0 + 10, Y0 + 20);    
    g.setColor (Color.green);
    g.drawString ("y = tan (x)", X0 + 70, Y0 - 2 * ((int) ScaleY)); 
    setBackground (Color.white);   
  }

}

External Links

Sun Microsystem Math Class API Version 1.6.0
Sun Microsystem Math Class API Version 1.5.0
Sun Microsystem Math Class API Version 1.4.2
Sun Microsystem Math Class API Version 1.3.1

Valid HTML 4.01 Transitional
Valid HTML 4.01 Transitional

Site Icon Copyright © 1999-2006, jhyoung, revised 11/26/06