Home » Java programming language

# Implementation of Methods of Math Class in Java

Learn: **Implementation of Basic Mathematical function in Java**, in this article we will learn **how to use java.lang.Math Package to perform some basic mathematical operations** by the help of already available methods.

Submitted by Mayank Singh, on July 24, 2017

**The different methods which we will discuss in the following article are:**

**1) Signum:**

If the number we are talking about is greater than zero than the signum function return 1, similarly it returns -1 if the number is smaller than zero, and returns zero if the number we are talking about is also zero.

In terms of programming if the parameter which we pass in the signum method is not a number than this method returns **"NaN"** which stands for Not a Number.

**Syntax for the Signum Method is:**

public static double signum(double d) OR public static float signum(float f)

Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.

**Parameters:**

d ,f - the floating-point value whose signum is to be returned

**Returns:**

the signum function of the argument

**2) round:**

This method is used to round of the decimal numbers to the nearest value.

**Syntax of the Round Method is:**

public static long round(double a)

Returns the closest long to the argument, with ties rounding to positive infinity.

**Special cases:**

- If the argument is NaN, the result is 0.
- If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE, the result is equal to the value of Long.MIN_VALUE.
- If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE.

**Parameters:**

a - a floating-point value to be rounded to a long.

**Returns:**

the value of the argument rounded to the nearest long value.

**3) max:**

This method is used to return the maximum between two numbers.

**Syntax of max Method is:**

public static double max(double x,double y)

Returns the greater of two double values. That is, the result is the argument closer to positive infinity. If the arguments have the same value, the result is that same value. If either value is NaN, then the result is NaN. Unlike the numerical comparison operators, this method considers negative zero to be strictly smaller than positive zero. If one argument is positive zero and the other negative zero, the result is positive zero.

**Parameters:**

x - an argument, y - another argument.

**Returns:**

the larger of x and y.

**4) min:**

This method is used to return the minimum between two numbers.

**Syntax of min Method is:**

public static double min(double a, double b)

Returns the smaller of two double values. That is, the result is the value closer to negative infinity. If the arguments have the same value, the result is that same value. If either value is NaN, then the result is NaN. Unlike the numerical comparison operators, this method considers negative zero to be strictly smaller than positive zero. If one argument is positive zero and the other is negative zero, the result is negative zero.

**Parameters:**

a - an argument, b - another argument.

**Returns:**

the smaller of a and b.

**5) abs:**

This method is used to convert any number to its absolute value; it is the same modulus function which we have studied in mathematics.

**Syntax of abs Method is:**

public static double abs(double a)

Returns the absolute value of a double value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned. Special cases:

- If the argument is positive zero or negative zero, the result is positive zero.
- If the argument is infinite, the result is positive infinity.
- If the argument is NaN, the result is NaN.

In other words, the result is the same as the value of the expression:

Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)

**Parameters:**

a - the argument whose absolute value is to be determined.

**Returns:**

the absolute value of the argument.

**6) cbrt:**

This method is used to find out the cube root of any number.

**Syntax of cbrt Method is:**

public static double cbrt(double a)

Returns the cube root of a double value. For positive finite x, cbrt(-x) == -cbrt(x); that is, the cube root of a negative value is the negative of the cube root of that value's magnitude. Special cases:

- If the argument is NaN, then the result is NaN.
- If the argument is infinite, then the result is an infinity with the same sign as the argument.
- If the argument is zero, then the result is a zero with the same sign as the argument.

The computed result must be within 1 ulp of the exact result.

**Parameters:**

a - a value.

**Returns:**

the cube root of a.

**7) pow:**

This method is used to find out the value when one number is raised to another number.

**Syntax of pow Method is:**

public static double pow(double a, double b)

Returns the value of the first argument raised to the power of the second argument.

**Parameters:**

a - the base, b - the exponent.

**Returns:**

the value a^{b}.

**8) exp:**

This method is used to find out the value by raising the number on to base e.

**Syntax of exp Method is:**

public static double exp(double a)

Returns Euler's number e raised to the power of a double value. Special cases:

- If the argument is NaN, the result is NaN.
- If the argument is positive infinity, then the result is positive infinity.
- If the argument is negative infinity, then the result is positive zero.

The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

**Parameters:**

a - the exponent to raise e to.

**Returns:**

the value **e ^{a}**, where

**e**is the base of the natural logarithms.

**9) log:**

This method is used to find out the Logarithmic Value of any number, **NOTE** that logarithmic value of negative values is not defined and thus this methods return NaN in such case.

**Syntax of log Method is:**

public static double log(double a)

Returns the natural logarithm (base e) of a double value. Special cases:

- If the argument is NaN or less than zero, then the result is NaN.
- If the argument is positive infinity, then the result is positive infinity.
- If the argument is positive zero or negative zero, then the result is negative infinity.

The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

**Parameters:**

a - a value.

**Returns:**

the value ln a, the natural logarithm of a.

**10) log10:**

This method is used to find out the Logarithmic value of a number when the base is 10, it will also return NaN if the number of which we are talking about is negative.

**Syntax of log10 Method is:**

public static double log10(double a)

Returns the base 10 logarithm of a double value. Special cases:

- If the argument is NaN or less than zero, then the result is NaN.
- If the argument is positive infinity, then the result is positive infinity.
- If the argument is positive zero or negative zero, then the result is negative infinity.
- If the argument is equal to 10
^{n}for integer n, then the result is n.

The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

**Parameters:**

a - a value.

**Returns:**

the base 10 logarithm of a.

**Syntax source: ** https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#log-double-

### Consider the Program to illustrate the different methods of java.lang.Math class:

import java.util.Scanner; import java.lang.*; class Mathematical { public static void main(String args[]) { double x; double y; Scanner KB=new Scanner(System.in); System.out.println("Enter First Number:"); x=KB.nextDouble(); System.out.println("Enter Second Number"); y=KB.nextDouble(); //Signum Method int r1=(int)Math.signum(x); int r2=(int)Math.signum(y); System.out.println("Signum of "+x+" is "+r1); System.out.println("Signum of "+y+" is "+r2); //Round method double round1=Math.round(x); double round2=Math.round(y); System.out.println("Rounding of "+x+" to nearest decimal place "+round1); System.out.println("Rounding of "+y+" to nearest decimal place "+round2); //Finding Maximum between two numbers double max=Math.max(x,y); System.out.println("Maximum of Two Numbers "+x+" & "+y+" is "+max); //Finding Minimum between two numbers double min=Math.min(x,y); System.out.println("Minimum of Two Numbers "+x+" & "+y+" is "+min); //Finding out the Absoute Values double abs1=Math.abs(x); double abs2=Math.abs(y); System.out.println("Absoute Value of "+x+" is "+abs1); System.out.println("Absoute Value of "+y+" is "+abs2); //Finding the Cube Roots of the given Numbers double c1=Math.cbrt(x); double c2=Math.cbrt(y); System.out.println("Cube Root of "+x+" is "+c1); System.out.println("Cube Root of "+y+" is "+c2); //Using Power Function in Java double pow1=Math.pow(x,2); double pow2=Math.pow(y,3); System.out.println("Number "+x+" when raised to the power of 2, the result is "+pow1); System.out.println("Number "+y+" when raised to the power of 3, the result is "+pow2); //Using Exponent(exp) Method , the result is of the form that //e(2.71) raised to the power of any other value double exp1=Math.exp(x); double exp2=Math.exp(y); System.out.println("e raised to the power "+x+" is "+exp1); System.out.println("e raised to the power "+y+" is "+exp2); //Using Logarithm Method in Java when base is e double log1=Math.log(x); double log2=Math.log(y); System.out.println("Logarithm of "+x+" is "+log1); System.out.println("Logarithm of "+y+" is "+log2); //Using Logarithm Method in Java when base is 10 double logof1=Math.log10(x); double logof2=Math.log10(y); System.out.println("Logarithm with base 10 of "+x+" is "+log1); System.out.println("Logarithm with base 10 of "+y+" is "+log2); } }

Output

Enter First Number: 64.25458 Enter Second Number -64.2659 Signum of 64.25458 is 1 Signum of -64.2659 is -1 Rounding of 64.25458 to nearest decimal place 64.0 Rounding of -64.2659 to nearest decimal place -64.0 Maximum of Two Numbers 64.25458 & -64.2659 is 64.25458 Minimum of Two Numbers 64.25458 & -64.2659 is -64.2659 Absoute Value of 64.25458 is 64.25458 Absoute Value of -64.2659 is 64.2659 Cube Root of 64.25458 is 4.005296733058854 Cube Root of -64.2659 is -4.005531929246174 Number 64.25458 when raised to the power of 2, the result is 4128.6510509764 Number -64.2659 when raised to the power of 3, the result is -265424.9729393972 e raised to the power 64.25458 is 8.042841886138147E27 e raised to the power -64.2659 is 1.2293463481170128E-28 Logarithm of 64.25458 is 4.162853005281435 Logarithm of -64.2659 is NaN Logarithm with base 10 of 64.25458 is 4.162853005281435 Logarithm with base 10 of -64.2659 is NaN

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.