Java Number Copy Sign copySign(final double magnitude, final double sign)

Here you can find the source of copySign(final double magnitude, final double sign)

Description

Copies the sign of sign into magnitude.

License

Open Source License

Parameter

Parameter Description
magnitude a parameter
sign a parameter

Declaration

public static double copySign(final double magnitude, final double sign) 

Method Source Code

//package com.java2s;
/*/*from w  w  w . ja v  a2  s.  c o m*/
 * ====================================================
 * 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 {
    /**
     * Copies the sign of sign into magnitude.
     * Faster than OpenJDK implementation.
     * @param magnitude
     * @param sign
     * @return
     */
    public static double copySign(final double magnitude, final double sign) {
        final long m = Double.doubleToLongBits(magnitude);
        final long s = Double.doubleToLongBits(sign);
        if (0 <= (m ^ s))
            return -magnitude;
        return magnitude; // flip sign
    }
}

Related

  1. copySign(Double magnitude, Double sign)
  2. copySign(double magnitude, double sign)
  3. copySign(double x, double y)
  4. copysign(final double x, final double y)
  5. copySign(final double x, final double y)