Saturday, December 13, 2025
HomeLanguagesJavaJava Math copySign() method with Examples

Java Math copySign() method with Examples

The java.lang.Math.copySign() method returns the first argument with the sign of the second argument.

Note:
Arguments can be of two types:

  • double type : copySign(double magt, double sign)
  • float type : copySign(float magt, float sign)

Syntax :

public static double copySign(DataType magt, DataType sign)
Parameter :
 magt: argument providing the magnitude of the result.
 sign : argument providing the sign of the result.
Return :
This method returns the magnitude of the first argument with the sign of the 
second argument.

Example :To show working of java.lang.Math.copySign() method.




// Java program to demonstrate working
// of java.lang.Math.copySign() method
import java.lang.Math;
  
class Gfg {
  
    // driver code
    public static void main(String args[])
    {
        double a = 34.543;
        double b = -123.44;
  
        // Input a, b
        // Output -34.543( a- Magnitude, b- Sign)
        System.out.println(Math.copySign(a, b));
  
        // Input b, a
        // Output 123.44( b- Magnitude, a- Sign)
        System.out.println(Math.copySign(b, a));
  
        float c = 87.56f;
        float d = -685.23f;
  
        // Input c, d
        // Output -87.56( c- Magnitude, d- Sign)
        System.out.println(Math.copySign(c, d));
  
        // Input d, c
        // Output 685.23( d- Magnitude, c- Sign)
        System.out.println(Math.copySign(d, c));
    }
}


Output:

-34.543
123.44
-87.56
685.23
RELATED ARTICLES

Most Popular

Dominic
32447 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6816 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12031 POSTS0 COMMENTS
Shaida Kate Naidoo
6951 POSTS0 COMMENTS
Ted Musemwa
7202 POSTS0 COMMENTS
Thapelo Manthata
6898 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS