Java Abs abs(final double d)

Here you can find the source of abs(final double d)

Description

Returns the absolute value of d (without branching).

License

Open Source License

Parameter

Parameter Description
d a parameter

Declaration

public static final double abs(final double d) 

Method Source Code

//package com.java2s;
/*/*from  www. j a  v  a2  s .  com*/
 * ====================================================
 * Copyright (C) 2013 by Idylwood Technologies, LLC. All rights reserved.
 *
 * Developed at Idylwood Technologies, LLC.
 * Permission to use, copy, modify, and distribute this
 * software is freely granted, provided that this notice
 * is preserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The License should have been distributed to you with the source tree.
 * If not, it can be found at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: Charles Cooper
 * Date: 2013
 * ====================================================
 */

public class Main {
    /**
     * Returns the absolute value of d (without branching).
     * Faster than OpenJDK implementation.
     * @param d
     * @return
     */
    public static final double abs(final double d) {
        return Double.longBitsToDouble(Long.MAX_VALUE & Double.doubleToRawLongBits(d));
    }

    /**
     * Returns the absolute value of f (without branching).
     * Faster than OpenJDK implementation.
     * @param f
     * @return
     */
    public static final float abs(final float f) {
        return Float.intBitsToFloat(Integer.MAX_VALUE & Float.floatToRawIntBits(f));
    }

    /**
     * Returns the absolute value of l (without branching).
     * Faster than OpenJDK implementation.
     * @param l
     * @return
     */
    public static final long abs(final long l) {
        final long sign = l >>> 63;
        return (l ^ (~sign + 1)) + sign;
    }

    /**
     * Returns the absolute value of i (without branching).
     * Faster than OpenJDK implementation.
     * @param d
     * @return
     */
    public static final int abs(final int i) {
        final int sign = i >>> 31;
        return (i ^ (~sign + 1)) + sign;
    }
}

Related

  1. abs(double[] in)
  2. abs(double[] in)
  3. abs(double[] v)
  4. abs(double[][] A)
  5. abs(final byte x)
  6. abs(final double value)
  7. abs(final double x)
  8. abs(final int pNumber)
  9. abs(float f)