📜  Java中的长signum()方法

📅  最后修改于: 2022-05-13 01:55:09.165000             🧑  作者: Mango

Java中的长signum()方法

符号函数也称为符号函数,是一种提取实数符号的奇数数学函数。
Java.lang.Long.signum() 方法用于获取指定long值的signum函数。对于正值、负值和零,该方法分别返回 1、-1 和 0。

句法 :

public static int signum(long num)

参数:该方法接受一个长型参数num ,要对其执行signum操作。

返回值:该方法返回指定long值的signum函数。如果指定值为:

  • 负数,该方法返回 -1。
  • 零,该方法返回 0。
  • 正,该方法返回 1。

例子:

Input: (Long) 2731766
Output: 1

Input: (Long) -233611 
Output: -1

Input: (Long) 0
Output: 0

下面的程序说明了Java.lang.Long.signum() 方法:
方案一:

// Java program to illustrate the
// Java.lang.Long.signum() Method
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        // It will return 1 as long value is greater than 1
        System.out.println(Long.signum(36565531));
  
        // It will return -1 as long value is less than 1
        System.out.println(Long.signum(-628127));
  
        // Returns 0 as long value is equal to 0
        System.out.println(Long.signum(0));
    }
}
输出:
1
-1
0

程序 2:用于十进制值和字符串。

// Java program to illustrate the
// Java.lang.Long.signum() Method
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
  
        // It will return compile time error
        System.out.println(Long.signum(36565.531));
  
        // It will return compile time error
        System.out.println(Long.signum("628127"));
    }
}
输出:
prog.java:10: error: incompatible types: possible lossy conversion from double to long
    System.out.println(Long.signum(36565.531));
                                   ^
prog.java:13: error: incompatible types: String cannot be converted to long
    System.out.println(Long.signum("628127"));
                                   ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
2 errors