Java Math subtractExact(long x, long y) method


We will discuss the Java Math subtractExact(long x, long y) method in Java language and understand its functionalities and working.

The subtractExact()is an inbuilt function in the Java Math library. The function returns the difference between the two parameters passed as arguments in the function. The function returns an exception when the returned value overflows the range of values of a particular data type.

The syntax of the subtractExact() function

long a;
long b;

long subtractExact(long a, long b);

The parameters passed in the function are a and b which are of long data types. The function returns the value in long which is equal to the difference of the parameters passed in the function i.e. a−b. The function will return an exception showing overflow in a particular data type when the value returned by the function overflows the value of the data type of the passed parameters.

In this article, we will see the implementation of the subtractExact() function in the Java code and how it returns the value. We will be writing a Java code where two numbers will be given as an input, the input will be of long data type and we need to find the difference of the two numbers using the function subtractExact() provided in the Java Math library.

Let’s understand the problem with the below examples.

Input

a=30 , b=10

Output

20

Explanation − Here the input numbers given are 30 and 10. We need to find the difference of the two numbers i.e. a−b using the subtractExact() function. Hence the output will be 20 which is equal to a−b.

Input

a= -2  , b=-3

Output

1

Explanation − The input numbers are −2 and −3. Using the subtractExact(a,b) and passing these two numbers as parameters in the function, the function will return the value equal to a−b i.e. −2−(−3)=−2+3=1. Hence, 1 is the required output.

Let’s understand the use of the subtractExact() function in the Java code to see the implementation of the function.

Let’s understand the use of the subtractExact() function in the Java code to see the implementation of the function

We will be following the below steps to implement the subtractExact() function in our java code:

  • We will import the Java Math library using java.lang.Math.

  • After importing the Math library, we can use all the functions in the library including the subtractExact() function.

  • Initialise two variables of long data type.

  • Now we will create another variable to store the value returned by the function.

  • Using subtractExact() function, we will store the value in the variable. The function subtract the second parameter passed in the function from the first parameter.

  • Print the output of the function which will be equal to the difference of first parameter and second parameter.

Example

//Java Code to see the implementation of the subtractExact() function

import java.lang.Math; //to import the java Math library to use the subtractExact() function

public class Main
{
	public static void main(String[] args) {
		//initialise two variable of long data type
		long a;
		long b;
		
		//store the values in the variable
		a=56240389;
		b=846270;
		
		long ans; //to store the  output of the function
		//subtracts the second parameter from the first one i.e. a-b
		ans=Math.subtractExact(a,b); // passing a and b as parameter in the function
		
		//print the output
		System.out.println("The value returned by the function : "+ans);
		
		ans=Math.subtractExact(b,a); //will equal to b-a
		
		System.out.println("The value returned by the function : "+ans);

	}
}

Output

The value returned by the function : 55394119
The value returned by the function : -55394119

Time complexity− O(1) , because the function subtractExact() returns the output in constant time.

Space complexity − O(1) , because we are not using any extra space for the implementation of the function.

Java Code to see the overflow in subtractExact() function

The steps to follow to implement the function are given below:

  • We will import the Java Math library using java.lang.Math

  • Now create two variables of long data type.

  • We will store the minimum value of the long data type in one of the variables and any value in the second one.

  • Now initialise another variable of long data type to store the value returned by the function. We will pass the minimum value of long data type as a first parameter because the function subtract the second parameter passed in the function from the first one.

  • Subtracting the number from the minimum value of the long data type will cause an overflow in the long data type. Thus, the function will show an exception for the long overflow.

Example

//Java Code to see the implementation of the subtractExact() function

import java.lang.Math; //to import the java Math library to use the subtractExact() function

public class Main
{
	public static void main(String[] args) {
		//initialise two variable of long data type
		long a;
		long b;
		
		//store the values in the variable
		a=Long.MIN_VALUE;
		b=7;
		
		long ans; //to store the  output of the function
		//subtracts the second parameter from the first one i.e. a-b
		//which will cause overflow in long as a is the minimum value of long data type
		ans=Math.subtractExact(a,b); // passing a and b as parameter in the function
		
		//print the output
		System.out.println("The value returned by the function : "+ans);
		

	}
}

Output

The Code will not execute as the overflow in the function occurs hereby showing an exception of long overflow as below:

Exception in thread "main" java.lang.ArithmeticException: long overflow
        at java.base/java.lang.Math.subtractExact(Math.java:887)
        at Main.main(Main.java:19)

Conclusion

The Java Math subtractExact(long x, long y) method and the implementation of the function in Java and its functionalities was discussed in the article. We understand the implementation of subtractExact() function by writing a java code and also try to understand the overflow of value in the function.

I hope you have understood the Java Math subtractExact() method and its implementation in Java after reading this article.

Updated on: 28-Aug-2023

80 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements