Java. Class Math. Overview. Trigonometric functions

Class Math. Overview. Trigonometric functions. Examples


Contents


Search other resources:

 
1. General information about the Math class

The java.lang library contains tools for performing mathematical calculations, in particular, the well-known Math class. This class implements math functions that operate on floating point numbers. Class functions are divided into the following groups:

  • trigonometric functions;
  • exponential functions;
  • rounding functions;
  • other special functions.

The class also contains two constants, the access to which is as follows:

  • Math.PI – Pi number (3.14…);
  • Math.E – exponent (2.71…).

 

2. Trigonometric functions. The list

Trigonometric functions of the Math class allow you to get the numerical value of a known mathematical function. These functions operate in radians. If degrees are known, then to get radians, you need to use a conversion formula like

radians = Math.PI * degree / 180;

here

  • degree – angle specified in degrees;
  • radians – angle specified in radians;
  • Math.PI – number 3.1415. This is a constant from the Math class.

The list of trigonometric functions in the Math class is as follows

  • sin(x) – returns the sine of angle x, given in radians;
  • cos(x) – calculates the cosine of the angle x;
  • tan(x) – calculates the tangent of the angle x.

Accordingly, the declaration of functions in the Math class has the form

static double sin(double x)
static double cos(double x)
static double tan(double x)

where x is the angle value given in radians.

Example.

public class MathFunctions {

  public static void main(String[] args) {
    // 1. Angle set to 45 degrees
    double degree = 45; // given angle in degrees

    // 2. Get radians from degrees
    double radian = Math.PI * degree / 180;

    // 3. Get the sine of a 45 degree angle
    double resSin = Math.sin(radian);

    // 4. Get the cosine
    double resCos = Math.cos(radian);

    // 5. Get the tangent
    double resTan = Math.tan(radian);

    // 6. Print the result
    System.out.println("resSin = " + resSin);
    System.out.println("resCos = " + resCos);
    System.out.println("resTan = " + resTan);
  }
}

Program result

resSin = 0.7071067811865475
resCos = 0.7071067811865476
resTan = 0.9999999999999999

 

3. Inverse trigonometric functions. The list. Example

Inverse trigonometric functions take the result of the corresponding trigonometric function and return the angle in radians. If necessary, the resulting value in radians can be converted to degrees using the formula:

degree = 180 * radians / Math.PI;

here

  • degree – angle in degrees;
  • radians – angle in radians;
  • Math.PI – number 3.1415. This is a constant from the Math class.

The list of inverse trigonometric functions is as follows:

  • asin() – arcsine of the number;
  • acos() – arc cosine;
  • atan() – arc tangent of a numeric value;
  • atan2() – arc tangent given by x/y proportion.

The declaration of inverse trigonometric functions is as follows

static double asin(double value)
static double acos(double value)
static double atan(double value)
static double atan2(double x, double y)

here

  • value – some numeric value. For the asin() and acos() functions, value must be between [0; 1].

Example.

public class MathFunctions {

  public static void main(String[] args) {
    // 1. Set a value
    double value = 1.0;

    // 2. Get arccosine, result in radians
    double aCos = Math.acos(value); // aCos = 0 radians
    System.out.println("arcCos(1.0) = " + aCos);

    // 3. Get the arcsine
    double aSin = Math.asin(value); // aSin = 1.57079 radians
    System.out.println("arcSin(1.0) = " + aSin);

    // 4. Get arctangent.
    //   Form a value that corresponds to an angle of 45 degrees
    value = 1;
    double aTan = Math.atan(value); // aTan = 0.78 radians
    System.out.println("aTan = " + aTan);

    // 5. Get the arctangent from the aspect ratio
    double aTan2 = Math.atan2(1, 1); // angle 45 degrees
    System.out.println("aTan2 = " + aTan2); // aTan2 = 0.78
  }
}

Program result

arcCos(1.0) = 0.0
arcSin(1.0) = 1.5707963267948966
aTan = 0.7853981633974483
aTan2 = 0.7853981633974483

 

4. Hyperbolic functions

Hyperbolic functions return a value from an argument specified in radians

  • sinh() – hyperbolic sine;
  • cosh() – hyperbolic cosine;
  • tanh() – hyperbolic tangent.

The declaration of hyperbolic functions is as follows

static double sinh(double x)
static double cosh(double x)
static double tanh(double x)

here

  • x – value in radians.

Example.

 

public class MathFunctions {

  public static void main(String[] args) {
    // 1. Set a value
    double value = 1.0;

    // 2. Sine hyperbolic
    double SinH = Math.sinh(value);
    System.out.println("SinH = " + SinH);

    // 3. Cosine hyperbolic
    value = 1.0;
    double CosH = Math.cosh(value);
    System.out.println("CosH = " + CosH);

    // 4. Tangent hyperbolic
    value = 1.0;
    double TanH = Math.tanh(value);
    System.out.println("TanH = " + TanH);
  }
}

Program result

SinH = 1.1752011936438014
CosH = 1.543080634815244
TanH = 0.7615941559557649

 


Related topics