Open In App

StrictMath copySign() in Java

Improve
Improve
Like Article
Like
Save
Share
Report
  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 function 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:




    // 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);
        }
    }

    
    

  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 function 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:




    // 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);
        }
    }

    
    



Last Updated : 17 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads