Java Math copySign()

The syntax of the copySign() method is:

Math.copySign(arg1, arg2)

Here, copySign() is a static method. Hence, we are accessing the method using the class name, Math.


copySign() Parameters

The copySign() method takes two parameters.

  • arg1 - first argument whose sign is to be replaced
  • arg2 - second argument whose sign is copied to arg1

Note: The data types of arg1 and arg2 should be either float or double.


copySign() Return Values

  • returns the first argument, arg1 with sign of the second argument, arg2

Note: For arguments (arg1, -arg2), the method returns -arg1.


Example: Java Math.copySign()

Here, as you can see the copySign() method assigns the sign of second variables (y and b) to the first variables (x and a).