當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.lang.StrictMath.copySign()用法及代碼示例


描述

這個java.lang.StrictMath.copySign(double magnitude, double sign) 方法返回第一個浮點參數和第二個浮點參數的符號。對於此方法,始終將 NaN 符號參數視為正數。

聲明

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

public static double copySign(double magnitude, double sign)

參數

  • magnitude- 這是提供結果大小的參數

  • sign- 這是提供結果符號的參數

返回值

此方法返回一個帶有大小和符號的值。

異常

NA

示例

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

package com.tutorialspoint;

import java.lang.*;

public class StrictMathDemo {

   public static void main(String[] args) {

      double d1 = 3.8 , d2 = -1, d3 = 1 , d4 = -14;
   
      /* returns the first double argument with the sign of the
         second double argument */

      double signedValue = StrictMath.copySign(d1, d2); 
      System.out.println("value of d1 with sign d2:" + signedValue);

      signedValue = StrictMath.copySign(d1, d3);
      System.out.println("value of d1 with sign d3:" + signedValue);

      signedValue = StrictMath.copySign(d2, d4);
      System.out.println("value of d2 with sign d4:" + signedValue);
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果 -

value of d1 with sign d2:-3.8
value of d1 with sign d3:3.8
value of d2 with sign d4:-1.0

相關用法


注:本文由純淨天空篩選整理自 Java.lang.StrictMath.copySign() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。