Java Math copySign Function

copySign Function

Java Math Class

Java Math class provides several methods to perform several operations on math calculations like max(), min(), sin(), cos(), round(), ceil(), floor(), copySign() etc.
The java.lang.Math class contains various methods for performing basic numeric operations

Here, in the page we will discuss about the copySign() Method in java.

Java copySign Function :

Java Collection contains Math class and The class Math contains methods for performing several basic and advance numeric operations such as the exponential, logarithm, square root, and trigonometric functions.
The copySign Function takes two arguments, say num1 and num2, and copies the sign of 2nd argument i.e. num2 and assign it to the first argument i.e. num1. It is a static method, so we can call it using math function.

Syntax:

Math.copySign(number1,number2);

Definition of Parameters:

Return Type :

clear() Function in ArrayList

clone() Function in ArrayList

add() Function in ArrayList

addall() Function in ArrayList

Example For Double and Float Values:

Run
import java.util.*;

public class Main{
  public static void main(String[] args) {

    // Example of double arguments
    double num1 = 8.65;
    double num2 = -9.40;

    // Example of float arguments
    float num3 = -2.3f;
    float num4 = 4.4f;
    
    // Printing values after performing operation
    System.out.println("Operation on Double : " + Math.copySign(num1, num2));
    System.out.println("Operation on Float : " + Math.copySign(num3, num4));

  }
}
Output:

Operation on Double : -8.65
Operation on Float : 2.3

In the above Example, We had taken two double values and two float values. The copySign Function returns the first argument for both of the values with the sign of their 2nd arguments.

Example for Integer Values:

Run
import java.util.*;

public class Main{
  public static void main(String[] args) {

    // Example of Integer arguments
    int num1 = 8;
    int num2 = -9;

    // Printing values after performing operation
    System.out.println("Operation on Integer Values : " + Math.copySign(num1, num2));
    
    // Printing the data type of output given by copySign Function
    System.out.println("Output is of " + ((Object)Math.copySign(num1, num2)).getClass().getSimpleName() + " Type.");
  }
}
Output:

Operation on Integer Values : -8.0
Output is of Float Type.

In the above Example, We had taken two Integer values. The copySign function returns the floating point value of the 1st argument with the sign of 2nd argument.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription