目录

static float copySign(float magnitude, float sign)

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

声明 (Declaration)

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

public static float copySign(float magnitude, float sign)

参数 (Parameters)

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

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

返回值 (Return Value)

此方法返回带幅度和符号的值。

异常 (Exception)

NA

例子 (Example)

以下示例显示了java.lang.StrictMath.copySign()方法的用法。

package com.iowiki;
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
↑回到顶部↑
WIKI教程 @2018