Java Abs abs(final int pNumber)

Here you can find the source of abs(final int pNumber)

Description

A replacement for Math.abs , that never returns negative values.

License

Open Source License

Parameter

Parameter Description
pNumber a number

Exception

Parameter Description
ArithmeticException if pNumber == Integer.MIN_VALUE

Return

the absolute value of pNumber

Declaration

public static int abs(final int pNumber) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  w ww.j  a v a 2s  .  c o m
     * A replacement for {@code Math.abs}, that never returns negative values.
     * {@code Math.abs(long)} does this for {@code Long.MIN_VALUE}.
     *
     * @see Math#abs(long)
     * @see Long#MIN_VALUE
     *
     * @param pNumber a number
     * @return the absolute value of {@code pNumber}
     *
     * @throws ArithmeticException if {@code pNumber == Long.MIN_VALUE}
     */
    public static long abs(final long pNumber) {
        if (pNumber == Long.MIN_VALUE) {
            throw new ArithmeticException("long overflow: 9223372036854775808");
        }

        return (pNumber < 0) ? -pNumber : pNumber;
    }

    /**
     * A replacement for {@code Math.abs}, that never returns negative values.
     * {@code Math.abs(int)} does this for {@code Integer.MIN_VALUE}.
     *
     * @see Math#abs(int)
     * @see Integer#MIN_VALUE
     *
     * @param pNumber a number
     * @return the absolute value of {@code pNumber}
     *
     * @throws ArithmeticException if {@code pNumber == Integer.MIN_VALUE}
     */
    public static int abs(final int pNumber) {
        if (pNumber == Integer.MIN_VALUE) {
            throw new ArithmeticException("int overflow: 2147483648");
        }

        return (pNumber < 0) ? -pNumber : pNumber;
    }
}

Related

  1. abs(double[][] A)
  2. abs(final byte x)
  3. abs(final double d)
  4. abs(final double value)
  5. abs(final double x)
  6. abs(float f)
  7. abs(float value)
  8. abs(float value)
  9. abs(float[] items)