How to use signum() to get the signum function of the argument, 0 if = 0, 1.0 if > 0 and -1.0 if < 0 in Java

1 Answer

0 votes
package javaapplication1;

public class JavaApplication1 {
 
    public static void main(String[] args) {
        
        double d1 = 3.14;
        double d2 = -1.87;
        double d3 = 0;
        double d4 = -0;
        
        // signum(double d)
        System.out.println("Math.signum(" + d1 + ") = " + Math.signum(d1));
        System.out.println("Math.signum(" + d2 + ") = " + Math.signum(d2));
        System.out.println("Math.signum(" + d3 + ") = " + Math.signum(d3));
        System.out.println("Math.signum(" + d4 + ") = " + Math.signum(d4));

    }
}
 
/*
run:

Math.signum(3.14) = 1.0
Math.signum(-1.87) = -1.0
Math.signum(0.0) = 0.0
Math.signum(0.0) = 0.0
 
*/

 



answered Sep 10, 2016 by avibootz
...