Java Math copySign() method with Examples
The java.lang.Math.copySign() method returns the first argument with the sign of the second argument.
Note:
Arguments can be of two types:
- double type : copySign(double magt, double sign)
- float type : copySign(float magt, float sign)
Syntax :
public static double copySign(DataType magt, DataType sign)
Parameter :
magt: argument providing the magnitude of the result.
sign : argument providing the sign of the result.
Return :
This method returns the magnitude of the first argument with the sign of the
second argument.
Example :To show working of java.lang.Math.copySign() method.
import java.lang.Math;
class Gfg {
public static void main(String args[])
{
double a = 34.543 ;
double b = - 123.44 ;
System.out.println(Math.copySign(a, b));
System.out.println(Math.copySign(b, a));
float c = 87 .56f;
float d = - 685 .23f;
System.out.println(Math.copySign(c, d));
System.out.println(Math.copySign(d, c));
}
}
|
Output:
-34.543
123.44
-87.56
685.23
Last Updated :
13 Apr, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...