StrictMath copySign() in Java

  1. The copySign(float mvalue, float sign) is an inbuilt method of StrictMath class in Java which is used to get the first floating-point argument with the sign of the second floating-point argument. A NaN sign argument in this functin is considered as a positive.
    Syntax :

    public static float copySign(float mvalue, float sign)

    Parameters : This method accepts two parameters:

    • mvalue: This is of float type which provides the magnitude of the result.
    • sign: This is of float type which provides the sign of the result.

    Return Value: The method returns a value with a magnitude and sign.



    Examples :

    Input: 
    mvalue = 5
    sign = -1
    
    Output = -5.0
    

    Below program illustrate the java.lang.StrictMath.copySign(float mvalue, float sign) method:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java praogram to illustrate the
    // java.lang.StrictMath.copySign(float mvalue, float sign)
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            float a1 = 7;
            float a2 = -1;
            float a3 = 1;
      
            float svalue = StrictMath.copySign(a1, a2);
            System.out.println("The value of a1 with sign a2: "
                                                     + svalue);
      
        svalue = StrictMath.copySign(a1, a3); 
        System.out.println("The value of a1 with sign a3: "
                                                     + svalue);
        }
    }

    chevron_right

    
    

  2. The copySign(double magnitude, double sign) is an inbuilt method of StrictMath class in Java which is used to get the first double argument with the sign of the second double argument. A NaN sign argument in this functin is considered as a positive.
    Syntax :

    public static double copySign(double mvalue, double sign)

    Parameters : This method accepts two parameters:

    • mvalue: This is of double type which provides the magnitude of the result.
    • sign: This is of double type which provides the sign of the result.

    Return Value : The method returns a value with magnitude and sign.
    Examples :

    Input: 
    mvalue = 6.9
    sign = -1
    
    Output = -6.9
    

    Below program illustrate the java.lang.StrictMath.copySign(double magnitude, double sign) method:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java praogram to illustrate the
    // java.lang.StrictMath.copySign(double magnitude, double sign)
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            double a1 = 4.7, a2 = -1, a3 = 1, a4 = -62;
      
            /* Returns the first double argument with the 
             sign of the second double argument */
            double svalue = StrictMath.copySign(a1, a2);
            System.out.println("The value of a1 with sign a2 :" 
                                                      + svalue);
      
            svalue = StrictMath.copySign(a1, a3);
            System.out.println("The value of a1 with sign a3 :" 
                                                      + svalue);
      
            svalue = StrictMath.copySign(a2, a4);
            System.out.println("The value of a2 with sign a4 :" 
                                                      + svalue);
        }
    }

    chevron_right

    
    



My Personal Notes arrow_drop_up

lets make it a lil simple

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.