1. Math Methods

1.1. Objectives

  • Utilize Math methods from the Java Library in code

1.2. Time Goal

  • 10 minutes on this section

1.3. Key Terms

argument
A value that you provide when you call a method. This value must have the type that the method expects.
composition
The ability to combine simple expressions and statements into compound expressions and statements.
invoke
To cause a method to execute. Also known as “calling” a method.

1.4. Exercises

Note

The Java library includes a Math class that provides common mathematical operations. The class Math contains methods for performing basic numeric operations such as exponential, logarithm, square root, and trigonometric functions. Math is in the java.lang package, so you don’t have to import it.

double root = Math.sqrt(17.0);
double angle = 1.5;
double height = Math.sin(angle);

The first line sets root to the square root of 17. The third line finds the sine of 1.5 (the value of angle).

Values for the trigonometric functions – sin, cos, and tan – must be in radians. To convert from degrees to radians, you can divide by 180 and multiply by π. Conveniently, the Math class provides a constant double named PI that contains an approximation of π:

double degrees = 90;
double angle = degrees / 180.0 * Math.PI;

Notice that PI is in capital letters. Java does not recognize Pi, pi, or pie. Also, PI is the name of a variable, not a method, so it doesn’t have parentheses. The same is true for the constant Math.E, which approximates Euler’s number.

Converting to and from radians is a common operation, so the Math class provides methods that do that for you.

double radians = Math.toRadians(180.0);
double degrees = Math.toDegrees(Math.PI);

Another useful method is round, which rounds a floating-point value to the nearest integer and returns a long. The following result is 63 (rounded up from 62.8319).

long x = Math.round(Math.PI * 20.0);

A long is like an int, but bigger. More specifically, an int uses 32 bits of memory; the largest value it can hold is \(2^{31}−1\), which is about 2 billion. A long uses 64 bits, so the largest value is \(2^{63}−1\), which is about 9 quintillion.

Note

You have probably learned to evaluate simple expressions like sin(π/2) and log(1/x). First, you evaluate the expression in parentheses, which is called the argument of the function. Then you can evaluate the function itself, either by hand or by punching it into a calculator.

This process can be applied repeatedly to evaluate more complex expressions like log(1/sin(π/2)). First we evaluate the argument of the innermost function (π/2 = 1.57), then evaluate the function itself (sin(1.57) = 1.0), and so on.

Just as with mathematical functions, Java methods can be composed to solve complex problems. That means you can use one method as part of another. In fact, you can use any expression as an argument to a method, as long as the resulting value has the correct type:

double x = Math.cos(angle + Math.PI / 2.0);

This statement divides Math.PI by two, adds the result to angle, and computes the cosine of the sum. You can also take the result of one method and pass it as an argument to another:

double x = Math.exp(Math.log(10.0));

In Java, the log method always uses base e. So this statement finds the log base e of 10, and then raises e to that power. The result gets assigned to x.

Some math methods take more than one argument. For example, Math.pow takes two arguments and raises the first to the power of the second. This line computes 210 and assigns the value 1024.0 to the variable x:

double x = Math.pow(2.0, 10.0);

When using Math methods, beginners often forget the word Math. For example, if you just write x = pow(2.0, 10.0), you will get a compiler error:

File: Test.java  [line: 5]
Error: cannot find symbol
symbol:   method pow(double,double)
location: class Test

The message “cannot find symbol” is confusing, but the last two lines provide a useful hint. The compiler is looking for a method named pow in the file Test.java (the file for this example). If you don’t specify a class name when referring to a method, the compiler looks in the current class by default

Note

Common Math Methods

Method Function
Math.sqrt(x) \(\sqrt{x}\)
Math.pow(x,y) \(x^y\)
Math.sin(x) \(\sin x\)
Math.cos(x) \(\cos x\)
Math.tan(x) \(\tan x\)
Math.asin(x) \(\arcsin x\)
Math.acos(x) \(\arccos x\)
Math.atan(x) \(\arctan x\)
Math.exp(x) \(e^x\)
Math.log(x) \(\ln x\)
Math.log10(x) \(log_{10} x\)
Math.round(x) Closest integer to x
Math.abs(x) \(|x|\)
Math.max(x,y) Maximum of x and y
Math.min(x,y) Minimum of x and y

What does the following expression evaluate to Math.max(Math.min(1, 2), 0)

What does the following expression evaluate to Math.sqrt(10 * 10)

What does the following expression evaluate to Math.pow(2, 3)

What does the following expression evaluate to Math.abs(100)

What does the following expression evaluate to Math.abs(8 - 16)

What is the output of this block of code

int x = 10, y = -15, z = 2;
System.out.println(Math.pow(Math.abs(x + y), 2));

//Hint the return type of Math.pow is double
Next Section - 2. Method Fundamentals