Java.lang.StrictMath.copySign() 方法

java.lang.StrictMath.copySign(floatmagnitude, float sign) 方法返回第一个浮点参数和第二个浮点参数的符号。 对于此方法,NaN 符号参数始终被视为正数。


声明

以下是 java.lang.StrictMath.copySign() 方法的声明。

public static float copySign(float magnitude, float sign)

参数

  • magnitude − 这是提供结果大小的参数

  • sign − 这是提供结果符号的参数


返回值

该方法返回一个带有大小和符号的值。


异常

NA


示例

下面的例子展示了 java.lang.StrictMath.copySign() 方法的使用。

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);
   }
}

让我们编译并运行上面的程序,这将产生下面的结果 −

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