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


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


描述

這個java.lang.StrictMath.signum(double d) 方法返回參數的符號函數,即如果參數為零,則為零,如果參數大於零,則為 1.0,如果參數小於零,則為 -1.0。它包括這些情況

  • 如果第一個參數是 NaN,則返回 NaN。 .
  • 如果參數為正零或負零,則結果與參數相同。

聲明

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

public static double signum(double d)

參數

d─ 這是要返回其符號的浮點值。

返回值

此方法返回參數的符號函數。

異常

NA

示例

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

package com.tutorialspoint;

import java.lang.*;

public class StrictMathDemo {

   public static void main(String[] args) {

      double d1 = 102.20d, d2 = 0.0d, d3 = -0.0d;
   
      // returns 1.0 if the argument is greater than zero
      double retval = StrictMath.signum(d1);
      System.out.println("Value = " + retval);

      /* if the argument is positive zero, then the result is the
         same as the argument */
      retval = StrictMath.signum(d2);
      System.out.println("Value = " + retval);
    
      /* if the argument is negative zero, then the result is the 
         same as the argument */
      retval = StrictMath.signum(d3);
      System.out.println("Value = " + retval);
   }
}

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

Value = 1.0
Value = 0.0
Value = -0.0

相關用法


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