Java Number Sign signum(final int n)

Here you can find the source of signum(final int n)

Description

Returns the signum function of the argument; zero if the argument is zero, 1 if the argument is greater than zero, -1 if the argument is less than zero.

License

Open Source License

Parameter

Parameter Description
n the integer value whose signum is to be returned

Return

the signum function of the argument

Declaration

public static int signum(final int n) 

Method Source Code

//package com.java2s;
/* Util.java//w  w  w  . j  a v a  2  s . c om
 *
 * Copyright (C) 2015, Tom? Pecina <tomas@pecina.cz>
 *
 * This file is part of cz.pecina.retro, retro 8-bit computer emulators.
 *
 * This application is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This application is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.         
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Returns the signum function of the argument; zero if the argument is
     * zero, 1 if the argument is greater than zero, -1 if the argument is
     * less than zero.
     *
     * @param  n the integer value whose signum is to be returned
     * @return   the signum function of the argument
     */
    public static int signum(final int n) {
        if (n > 0) {
            return 1;
        } else if (n < 0) {
            return -1;
        } else {
            return 0;
        }
    }

    /**
     * Returns the signum function of the argument; zero if the argument is
     * zero, 1 if the argument is greater than zero, -1 if the argument is
     * less than zero.
     *
     * @param  n the long value whose signum is to be returned
     * @return   the signum function of the argument
     */
    public static long signum(final long n) {
        if (n > 0) {
            return 1;
        } else if (n < 0) {
            return -1;
        } else {
            return 0;
        }
    }
}

Related

  1. signum(double value)
  2. signum(double z)
  3. signum(final double x)
  4. signum(final int a)
  5. signum(final int i)
  6. signum(final int value)
  7. signum(float f)
  8. signum(float val)
  9. signum(int a)