Home » Java programming language

Java Math Class static double copySign(double d1 , double d2) with example

Java Math Class copySign(double d1 , double d2) method: Here, we are going to learn about the copySign(double d1 , double d2) method of Math Class with its syntax and example.
Submitted by Preeti Jain, on September 01, 2019

Math Class copySign(double d1 , double d2)

  • This method is available in java.lang package.
  • This method is used to return the first floating-point argument along with returning the sign of the second floating-point argument.
  • This is a static method so it is accessible with the class name too.
  • The return type of this method is double that means it returns the first argument with the sign of second argument.
  • In this method we pass two parameters as arguments of double type: First Parameter – it is the double floating-point argument number which is to be returned, and the Second Parameter – it is also a double floating-point number which is not to be returned but its sign will be returned with the first argument.
  • This method does not throw any exception.
  • This is an overloaded method so two versions of this method are available like one is double type argument and another is of the float type argument.

Syntax:

    public static double copySign(double d1 , double d2){
    }

Parameter(s):

  • d1 – value to be returned.
  • d2 – value whose sign to be returned with the value.

Return value:

The return type of this method is double, The return type of this method is double that means it returns the first argument with the sign of second argument.

Java program to demonstrate example of copySign(double d1 , double d2) method

// Java program to demonstrate the example of 
// copySign(double d1 , double d2) method of Math Class

class CopySignMethod {
    public static void main(String[] args) {
        // Here we are declaring few variables
        double d1 = 100.6;
        double d2 = -200.6;

        // Display previous value of d1 and d2
        System.out.println("Old value of d1 before implementation is: " + d1);
        System.out.println("Old value of d2 before implementation is :" + d2);

        // Here , we will get (-100.6) because we are passing parameter 
        // (100.6,-200.6)  so it will return first argument value and 
        // return the sign of the second argument [d1=100.6 , d2= -200.6]
        System.out.println("New value after implementation is :" + Math.copySign(d1, d2));

        // Here , we will get (200.6) because we are passing parameter 
        // (-200.6,100.6)  so it will return first argument value and 
        // return the sign of the second argument
        System.out.println("New value after implementation is :" + Math.copySign(d2, d1));
    }
}

Output

E:\Programs>javac CopySignMethod.java

E:\Programs>java CopySignMethod
Old value of d1 before implementation is :100.6
Old value of d2 before implementation is :-200.6

New value after implementation is : -100.6
New value after implementation is : 200.6


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.