Java Math copySign() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 6+

Definition

In Java, Math.copySign(magnitude, sign) returns a number with the size of magnitude and the sign of sign. Math.copySign(12.5, -1.0) is -12.5.

It reads the sign bit directly, so it can even see the sign of -0.0. Typical uses: giving a speed a direction, building rounding rules that are symmetric around zero, and detecting negative zero.

A static method of the java.lang.Math class, added in Java 6 (JDK 1.6). Call it on the class name, as in Math.copySign(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass the value whose size you keep, then the value whose sign you copy.

Java
double v = Math.copySign(12.5, -1.0);   // -12.5
double w = Math.copySign(-3.0, 2.0);    // 3.0
double z = Math.copySign(7.0, -0.0);    // -7.0
float f = Math.copySign(7.0f, -2f);     // -7.0
CallWhat it does
Math.copySign(5.0, -0.1)Returns -5.0.
Math.copySign(-5.0, 9.0)Returns 5.0 (sign replaced, not combined).
Math.copySign(1.0, x)1.0 or -1.0, telling apart 0.0 and -0.0.
Math.copySign(Math.floor(Math.abs(x) + 0.5), x)Round half away from zero.

The sign is copied, not multiplied: a negative magnitude with a positive sign becomes positive. This differs from Math.abs(m) * Math.signum(s), which gives 0 when s is zero.

Library declarations
Java
// java.lang.Math
public static double copySign(double magnitude, double sign)
public static float copySign(float magnitude, float sign)

Parameters

ParameterDescription
magnitudeSupplies the size (absolute value) of the result.
signSupplies only the sign of the result, including the sign of a zero.

Return Value

Return ValueDescription
double / float|magnitude| with the sign of sign.

Compare with Similar Methods

Sign-related methods side by side:

MethodHow it differs
signum(x)Reads the sign as -1.0, 0.0, or 1.0.
abs(x)Always clears the sign; same as copySign(x, 1.0).
-xFlips the sign instead of copying another one.
round(x)Rounds ties toward positive infinity, not away from zero.

Java Examples

Example 1 — Give a Speed the Direction of Travel

A car moves at a fixed speed; the direction value decides whether it goes forward or backward.

Java
public class MathCopySignSpeed {
    public static void main(String[] args) {
        double speed = 12.5;
        System.out.println("Reverse: " + Math.copySign(speed, -1.0));
        System.out.println("Forward: " + Math.copySign(-3.0, 2.0));
        System.out.println("Sign of -0.0 copied: " + Math.copySign(7.0, -0.0));
        System.out.println("float version: " + Math.copySign(7.0f, -2f));
    }
}

How It Works

  1. Size from the first argument. 12.5 keeps its size and takes the minus sign of -1.0, giving -12.5.
  2. Replaced, not combined. -3.0 has its own minus sign dropped and gets the plus sign of 2.0, so the result is 3.0.
  3. Zero has a sign too. -0.0 carries a minus sign bit, so 7.0 becomes -7.0; the float overload behaves the same way.

Example 2 — Round Half Away From Zero

Math.round sends -2.5 to -2. Many people expect -3. Build a symmetric rule with copySign.

Java
public class MathCopySignRounding {
    public static void main(String[] args) {
        double[] values = {2.5, -2.5, -1.2, -0.4};
        for (double v : values) {
            double away = Math.copySign(Math.floor(Math.abs(v) + 0.5), v);
            System.out.println(v + " -> " + away + " (Math.round: " + Math.round(v) + ")");
        }
    }
}

How It Works

  1. Round the size. Math.floor(Math.abs(v) + 0.5) rounds the positive size half-up: 2.5 becomes 3.0, 1.2 becomes 1.0, and 0.4 becomes 0.0.
  2. Put the sign back. copySign re-attaches the original sign, so -2.5 becomes -3.0, while Math.round(-2.5) moves toward positive infinity and gives -2.
  3. A signed zero appears. For -0.4 the rounded size is 0.0, and copying the minus sign produces -0.0, which still equals 0.0 in comparisons.

Example 3 — Tell 0.0 and -0.0 Apart

== cannot see the difference between the two zeros; copySign can.

Java
public class MathCopySignZero {
    public static void main(String[] args) {
        double a = 0.0;
        double b = -0.0;
        System.out.println("a == b? " + (a == b));
        System.out.println("sign of a: " + Math.copySign(1.0, a));
        System.out.println("sign of b: " + Math.copySign(1.0, b));
        System.out.println("signum(b): " + Math.signum(b));
    }
}

How It Works

  1. Equal but not identical. IEEE 754 defines 0.0 == -0.0 as true, so == cannot tell them apart.
  2. Copy the sign onto 1.0. copySign(1.0, b) reads the sign bit of -0.0 and returns -1.0, a clear yes/no answer.
  3. Why not signum? signum returns zeros unchanged, so it prints -0.0, which is less convenient to test than -1.0.

Frequently Asked Questions

It returns the first argument with the sign of the second argument. Math.copySign(3.0, -1.0) returns -3.0.
No. It replaces the sign. Math.copySign(-5.0, 2.0) is 5.0, because the sign comes only from the second argument.
Use Math.copySign(1.0, x), which returns -1.0 for -0.0 and 1.0 for 0.0. Comparing with == does not work, because 0.0 == -0.0 is true.
Math.copySign may treat a NaN sign as either positive or negative. Use StrictMath.copySign if you need it always treated as positive.
Java 6 (JDK 1.6).

Next: cos()

Cosine of an angle given in radians.

cos() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful