What is a Math class?
Java math class consists of arithmetic math methods that performs basic numeric operations such as finding the maximum or minimum of two values, rounding values, logarithmic functions, square root, and trigonometric functions, etc. The Math library functions can be accessed using the header Java.lang.Math. These math methods under Java math class are for our convenience.
Everyday mathematics applications include some basic logical or arithmetic operations such as finding the modulus of a number where one can just derive the answer mentally. But if such operations are to be used in Java code, it is not as simple because a proper set of code is to be dedicated to it.
For instance, to find the absolute value of a number, there are two possible scenarios:
• First, to check whether the number is >= 0. If yes, the number itself is its absolute value.
• Else, when number < 0, number*(-1) is its absolute value.
Instead of writing code for such common methods, abs() method in Math package can be used.
Importing math class
Before describing the following math class methods, first import the required class from its respective package, which is easily done by the following two lines of code:
import Java.lang.Math ; // Java.lang.Math class is imported
Or
import Java.lang.*; // this imports all classes under Java.lang package and therefore, it is recommended.
Math class methods
METHODS | SYNTAX |
abs() | public static double abs(datatype arg) |
log() | public static double log(double arg) |
toradian() | public static double toradian(double deg) |
addexact() | public static int addExact(int x, int y) |
asin() | public static double asin(double arg) |
cbrt() | public static double cbrt(double arg) |
floor() | public static double floor(double arg) |
acos() | public static double acos(double arg) |
IEEremainder() | public static double IEEremainder(double d1, double d2) |
Arithmetic methods under math class
1) abs()
Math.abs() method takes in one argument which has type int, float, long or double value, and returns the absolute value of that argument. Absolute value is also known as the modulus function in Math.
Code ----------------------------------
int x = 2;
int y = -3;
int z = Math.abs(x); // z stores 2
int z = Math.abs(y); // z stores 3
**Note- absolute value refers to the modulus operation in math, that is, the distance from the origin of the numeric argument, so it cannot be negative (0 to positive infinity).
2) pow()
Math.pow() method takes as input two arguments of type double, that is, the base and the exponent, respectively.
Code ---------------------------------------
double a = 3.0, b = 3.0; //arguments must be double
double c = Math.pow (a, b); //a is raised to b and is stored in c that is, c = 27
System.out.print (c); // prints c = 27
3) sqrt()
Math.sqrt() considers one argument of type double and returns its square root.
Code----------------------------------------
double d = 25;
double y = Math.sqrt (d); // stores square root of d in y (i.e.) y = 5
System.out.print (y); // prints y =5
Trigonometric methods under math class
1) sin()
Math.sin() takes as argument a value of type double and returns its trigonometric sine value.
Code ----------------------------------------
double = 0.0;
double y = Math.sin (0); // stores sin (0) ie 0
System.out.print (y); // prints y = 0
**Note:
- Remember while taking sine, argument must be in radians
2) cos ()
Math.cos() takes an argument of type double as input and returns its trigonometric cosine value.
Code ----------------------------------------
double x = 0.0;
double y = Math.cos(0); // stores cos(0) ie 1
System.out.print(y); // prints y =1
**Note:
- Remember while taking cosine, argument must be in radian
3) toRadians()
Math.toRadians() considers a double argument in degrees and returns its equivalent radian value.
Code----------------------------------------
double x = 0.0;
double y = Math.toRadians(0); // converts 0.0 degrees to radians that is, 0.0
System.out.print(y); // prints y = 0.0
**Note:
- Conversion is an approximation, so it is not exact.
- It can also be done vice versa by using toDegrees().
Formula-----------------------------------
X (degrees)* pi / 180 = Y (radians)
Additional methods under math class
1) nextAfter()
Math.nextAfter() considers two arguments that act as the start and direction, respectively. It returns the floating-point number adjacent to the start toward the given direction, that is, the second argument.
Code----------------------------------------------------------
double a = 8432.21;
double b = 154.284;
double c = Math.nextAfter(a, b)); //c = 8432.209999 which is adjacent to a towards b
**Note:
- Both the arguments can be a double value type or the first argument can be floating-point value and the second argument a double value.
- Similarly, nextUp () considers one argument that acts as the start and returns the floating-point value adjacent to the start toward the non-negative infinity or positive infinity.
2) addExact()
Math.addExact() takes two integer arguments, both of type integer, and returns their sum, which is also of type integer.
Code ----------------------------------
int x= 2;
int y= 3;
int z= Math. add Exact (x,y); // z stores x+yie z = 5
**Note :
- Throws an arithmetic exception if the result overflows, that is, does not fit the int data type.
3) copySign()
Math.copySign() takes two arguments and returns the first argument with the same sign as that of the second argument.
Code ----------------------------------
float x= 2;
float y= -3;
float z= Math. Copy Sign(x,y); // z stores x with same sign as y’sie -2
**Note: Either both types are double value or both are floating-point value
4) log()
Math.log() takes in an argument of type double and returns the natural logarithm of the given argument. Natural logarithm refers to taking log with base ‘e’, that is, ‘e’ raise to what is equal to the given argument.
Code----------------------------------------------------------
double a = 1;
double c = Math.log(a); //c = 0, since ‘e’ raise to 0 is 1
Another method is Math.IEEEremainder() which takes in two double value arguments and computes the remainder of the first argument divided by the second and returns it in IEEE 754 standard.
There are several other lesser-used methods as well such as scalb(), log10(), and so on.
Context and Application
This topic as well as related concepts such as other classes in Java are significant in professional exams and syllabus for both undergraduate and graduate courses, especially for:
Bachelors in Computer science.
Masters in Computer Science and related fields.
Practice Problem
1. Which of the following methods is used to get a non-negative number?
- Absolute()
- Modulus()
- Mod()
- abs()
Answer- Option d
Explanation- Method Math.abs() returns the absolute value of the argument. Absolute value is also known as modulus function.
2. Which of the following methods can convert Degrees to Radians?
- DegToRads()
- Toradians()
- toRadians()
- convertDegrees()
Answer- Option c
Explanation- toRadians() method returns given degrees to radian conversion. Degree and radian are two units of measuring angles. Since trigonometric calculations are accepted in the radian unit, Math.toRadians() method is used for the conversion.
3. Which of the following methods returns the cube root of the given argument?
- cbrt()
- cube()
- cuberoot()
- square root ()
Answer- Option a
Explanation- cbrt() method returns the cube root of the given float or double. Math.cbrt() method is used to find the cube root and all other options will return an error.
4. Which of the following is true for the copySign() method?
- Returns the second argument with the sign of the first argument
- Returns the first argument with the sign of the second argument
- Returns the first argument with its own sign
- Returns the second argument with its own sign
Answer- Option b
Explanation- Math.copySign() method takes two input argument. It returns the first argument with the sign of the second argument.
5. Which of the following is/are correct for IEEEremainder() method?
- Takes in two double value arguments
- Takes in one floating-point value argument and one double value argument
- Takes in two floating-point value arguments
- Takes in two integer values
Answer- Option a
Explanation: Math.IEEremainder() method returns the remainder. It takes two input arguments and both are double values.
Common Mistakes
Math methods can help in efficient programming. Common mistakes include passing arguments of incorrect type, passing incorrect number of arguments and assigning the value returned to variable of different type. In addition, the often encountered mistake is to use a function without importing appropriate package.
Related Concepts
Java has several other classes such as Java.util.Random; which generates numbers pseudorandomly. Hence, some related concepts to enhance understandability are:
- StrictMath class
- Java.util.object
Want more help with your computer science homework?
*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.
Search. Solve. Succeed!
Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.
Math class and its different methods Homework Questions from Fellow Students
Browse our recently answered Math class and its different methods homework questions.
Search. Solve. Succeed!
Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.