Home » Java programming language

Java Math Class static float copySign(float f1 , float f2) with example

Java Math Class float copySign(float f1 , float f2) method: Here, we are going to learn about the float copySign(float f1 , float f2) method of Math Class with its syntax and example.
Submitted by Preeti Jain, on September 02, 2019

Math Class float copySign(float f1 , float f2)

  • This method is available in java.lang package.
  • This method is used to return the first floating-point argument along with the sign of the second floating-point argument.
  • This is a static method so it is accessible with the class name too.
  • In this method we pass two parameters as arguments: the first parameter – it is the floating-point argument number which is to be returned, and the Second parameter – it is also a 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 is available like one is double type argument and another is of the float type argument.

Syntax:

    public static float copySign(float f1 , float f2){
    }

Parameter(s):

  • f1 – value to be returned.
  • f2 – value whose sign to be returned with the value.

Return value:

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

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

// Java program to demonstrate the example of 
// copySign(float f1 , float f2) method of Math Class

public class CopySignMethod {
    public static void main(String[] args) {
        // Here we are declaring few variables
        float f1 = 100.6f;
        float f2 = -200.6f;

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


        // 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 [f1=100.6 , f2= -200.6]
        System.out.println("New value after implementation is :" + Math.copySign(f1, f2));

        // 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(f2, f1));
    }
}

Output

E:\Programs>javac CopySignMethod.java

E:\Programs>java CopySignMethod
Old value of f1 before implementation is :100.6
Old value of f2 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.