java.lang.Math copySign()

Description

On this document we will be showing a java example on how to use the copySign() method of Math Class. The copySign() returns the first floating-point argument with the sign of the second floating-point argument. Make a note that the copySign() method is overloaded. Below are the two overloaded method of the copySign().

  • public static double copySign(double magnitude, double sign)
  • public static float copySign(float magnitude, float sign)

The two overloaded methods are basically the same, it’s just that they deal with different data type either float or double.

Most of the methods of the Math class is static and the copySign() method is no exception. Thus don’t forget that in order to call this method, you don’t have to create a new object. Use the method in the format Math.copySign(magnitude, sign).

This method is very simple, the magnitude argument will have the sign of the sign argument, simple as that. For instance if the magnitude is -12 and the sign value is 100, then the copySign method will return 12.

Method Syntax

public static double copySign(double magnitude, double sign)

public static float copySign(floatmagnitude, float sign)

Method Returns

The copySign() method returns a value with the magnitude of magnitude and the sign of sign.

Compatibility

Requires Java 1.0 and up

Java Math copySign() Example

Below is a java code demonstrates the use of copySign() method of Math class. The example presented might be simple however it shows the behaviour of the copySign() method.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of  
 * copySign() method of Math class
 */

public class MathCopySignExample {

	public static void main(String[] args) {
		
		// Ask for user input
		System.out.print("Enter the magnitude:");
		
		// use scanner to read the console input
		Scanner scan = new Scanner(System.in);
		
		// Assign the 1st input to String variable
		String magnitude = scan.nextLine();
		
		// ask for the second input
		System.out.print("Enter the sign to copy:");
		
		// Assign the 2nd input to String variable
		String sign = scan.nextLine();
		
		// close the scanner object
		scan.close();		
		
		// convert the values to double
		double doubleMagnitude = Double.parseDouble(magnitude);
		double doubleSign = Double.parseDouble(sign);		
			
		// get the result of copySign method
		double result = Math.copySign(doubleMagnitude,doubleSign);
		System.out.print("Result of the operation:"+result);
	}

}

The above java example source code demonstrates the use of copySign() method of Math class. We simply ask for two user input and we use the Scanner class to parse it. Since we have used the nextLine() method to get the console value which is having a return data type of String thus we have used the Double.parseDouble() to transform it into double. Alternatively if the requirements is to use float then you must use the Float.parseFloat() instead. This conversion is required because the argument for copySign() method only accepts either float or double.

Sample Output

Below is the sample output when you run the above example.

java lang Math copySign() example output