Java.lang.StrictMath.copySign() Method

Advertisements


Description

The java.lang.StrictMath.copySign(float magnitude, float sign) method returns the first floating-point argument with the sign of the second floating-point argument. For this method, a NaN sign argument is always treated as if it were positive.

Declaration

Following is the declaration for java.lang.StrictMath.copySign() method

public static float copySign(float magnitude, float sign)

Parameters

  • magnitude -- This is the parameter providing the magnitude of the result

  • sign -- This is the parameter providing the sign of the result

Return Value

This method returns a value with magnitude and sign.

Exception

  • NA

Example

The following example shows the usage of java.lang.StrictMath.copySign() method.

package com.tutorialspoint;

import java.lang.*;

public class StrictMathDemo {

  public static void main(String[] args) {
  
    float f1 = 3 , f2 = -1, f3 = 1 , f4 = -14;

    /* returns the first floating-point argument with the sign of the 
    second floating-point argument */
       
    float signedValue = StrictMath.copySign(f1 , f2); 
    System.out.println("value of f1 with sign f2 : " + signedValue);
    
    signedValue = StrictMath.copySign(f1 , f3); 
    System.out.println("value of f1 with sign f3 : " + signedValue);
    
    signedValue = StrictMath.copySign(f2 , f4); 
    System.out.println("value of f2 with sign f4 : " + signedValue);
  }
}

Let us compile and run the above program, this will produce the following result:

value of f1 with sign f2 : -3.0
value of f1 with sign f3 : 3.0
value of f2 with sign f4 : -1.0



Advertisements
Advertisements