Using class Math

Math is yet another class belonging to the core set of the Java programing language. We take a tour on selected methods:

Figure 404. Math.sin(double x) Slide presentation
Code Result Math notation
final double x = 90;
final double y = Math.sin(x);
System.out.println(y + " == sin(" + x + ")");
0.8939966636005579 == sin(90.0) 
y = sin x

exercise No. 134

Common pitfall using trigonometric functions

Q:

We reconsider Figure 404, “Math.sin(double x). Did you expect a value of 0.8939966636005579 corresponding to an angle of 90° here? Discuss the underlying problem.

A:

The mathematically inclined reader may have expected a result of 1.000... corresponding to a right angle of 90° rather than 0.893....

This is a common misconception: At school you were probably using so called degrees ranging from 0° to 360° for describing angle values. In Mathematics however trigonometric functions are being defined as power series e.g.:

sin x = x - x 3 3! + x 5 5! + ... = n = 0 ( -1 ) n x 2n + 1 ( 2n + 1 ) !

As an immediate consequence describing a full circle of angle values the variable x here is ranging from 0 to 2 π rather than from 0° to 360°. This angle unit is called radians. If you still want to use degrees you will have to convert these to radians beforehand by multiplying with 2 π 360° or simply π 180 :

final double x = 90;
final double y = Math.sin(x * Math.PI / 180); //converting degrees to radians
System.out.println(y + " == sin(" + x + ")");

exercise No. 135

Using constants from java.lang.Math.

Q:

In Exercise Calculating a circle's area avoiding accidental redefinition you calculated a given circle's area protecting variables against accidental redefinition using the final modifier:

public static void main(String[] args) {

  double radius = 2.31;         // A circle having a radius (given e.g. in mm).
  final double PI = 3.1415926;  // Creating PI as a constant (non-modifiable/
                                // assignable) variable.

  final double area = PI * radius * radius;
  System.out.println(area);
}

You may have wondered why you had to punch in the value of such an important constant as π by yourself. Actually Java predefines constants in java.lang.Math class. Read its documentation to rewrite your code thereby replacing your own variable pi's definition .

Tip

You may want to read the Static Members and Java Packages sections of [Kurniawan].

A:

The short answer simply is:

public static void main(String[] args) {

  double radius = 2.31;       // A circle having a radius (given e.g. in mm).

  final double area = Math.PI * radius * radius;
  System.out.println(area);
}

In case you bother about using the somewhat clumsy Math.PI expression rather than just using PI itself some syntactic sugar is on offer by means of a static import statement:

import static java.lang.Math.PI;

public class CircleAreaCalculator {

   public static void main(String[] args) {

      double radius = 2.31; // A circle having a radius (given e.g. in mm).

      final double area = PI * radius * radius; // This actually refers to Math.PI
      System.out.println(area);
   }
}

We dig a little deeper to fully understand the underlying concepts. Obtaining a JDK's source code and browsing its implementation Math.java reveals:

/*
 * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
 * ...
 */

package java.lang;
...

public final class Math {

...
    /**
     * The {@code double} value that is closer than any other to
     * <i>pi</i>, the ratio of the circumference of a circle to its
     * diameter.
     */
    public static final double PI = 3.14159265358979323846;
...

This accounts for using the expression Math.PI.

The careful reader may have expected an import statement in order to use the Math class:

import java.lang.Math; // Optional: Classes from java.lang are imported
                       // implicitely as per the Java language specification.

public class CircleAreaCalculator {

   public static void main(String[] args) {

      double radius = 2.31; // A circle having a radius (given e.g. in mm).

      final double area = Math.PI * radius * radius;
      System.out.println(area);
   }
}

But since the Math class belongs to the java.lang package it is present by default and does not have to be imported.