×

Java Math copySign() Method

The copySign() method of Math class returns the first floating-point argument with the sign of the second argument.

Syntax:

public static float copySign(float magnitude, float sign)

public static double copySign(double magnitude, double sign)

Parameters:

The parameter ‘magnitude’ and ‘sign’ represents the magnitude and sign of the result.

Return Value:

The copySign() method returns a value with the magnitude of magnitude and the sign of sign.

Example 1:

public class JavaMathCopySignExample1 {
    public static void main(String[] args) {
        double a=8.1;
        double b=-1;
        //returns a value with the magnitude of 'a' and the sign of 'b'.
        System.out.println("Sign and magnitude of "+ a +" : "+Math
.copySign(a,b));
    }
}

Output:

Sign and magnitude of 8.1 : -8.1

Example 2:

public class JavaMathCopySignExample2 {
    public static void main(String[] args) {
        double a=-0.0d/0.0d;
        double b=-24;
        //return NaN value
        System.out.println("Sign for "+ a +" value : "+Math.copySign(a,b));
    }
}

Output:

Sign for NaN value : NaN

Example 3:

public class JavaMathCopySignExample3 {
    public static void main(String[] args) {
        float a=-0.0f;
        float b=3;
        //Returns the first floating-point argument with the sign of
the second floating-point argument
        System.out.println("Sign and magnitude of "+ a +" = "+Math
.copySign(a,b));
    }
}

Output:

Sign and magnitude of -0.0 = 0.0

Example 4:

public class JavaMathCopySignExample4 {
    public static void main(String[] args) {
        double x = -90d/0.0d;
        double y=9;
        System.out.println("Sign and magnitude = "+Math.copySign(x,y));
    }
}

Output:

Sign and magnitude = Infinity

Related Topics

Prepared statement in Java

Prepared statement: A prepared statement is a statement that is pre-compiled SQL statement, and it is a sub-interface of a statement. Compared to other statement objects in java, Prepared Statement objects have...

3 minutes read.

Even Odd Program in Java

Even Odd Program in Java The number that are completely divisible by 2 are even and the number that leaves remainder are odd numbers. For example, the numbers 2, 0, 4,...

5 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

How annotations work in Java

Annotations were introduced by sun microsystem and are available from the java 1.5 version. In general, configurations can be done either by XML or by annotations. Hibernate and spring framework users prefer...

4 minutes read.

Thread Program in Java

Thread Program in Java Thread program in Java is the continuation of multithreading program in Java. In this topic, we will learn about the usage of threads, race condition in multithreading,...

8 minutes read.

What is interpreter in Java?

The programming language Java is platform-neutral. Therefore, can use Java on any platform that supports the Java processor. The Piece of software transforms the Java bytecode contained in the class...

5 minutes read.

Constructor Program in Java

Constructor Program in Java In Java, a constructor is a piece of code that is used to create an object. A constructor is called implicitly when an object is created in...

7 minutes read.

ArrayDeque in Java

ArrayDeque The ArrayDeque is one of the essential concepts in java to implement the deque interface. It will allow us to apply a resizable array to implement the Deque interface. This...

8 minutes read.

How to Create an API in Java?

Introduction The API can be abbreviated as Application Programming Interface. An API is a combination of set of classes and interfaces. It is also equivalent to a simple java program. To...

12 minutes read.

CopyOnWriteArrayList in Java

The CopyOnWriteArrayList is used to implement the List Interface. It is the improved version of ArrayList. The operations like add, remove, set, update etc, these operations are done by creating...

5 minutes read.

Sleep Time in Java

Sleep is amethod in java that is related to thread class and it is a concept of multithreading and is used to stop the execution of the current thread a...

4 minutes read.

Java FileNotFoundException

FileNotFoundException is another exception class accessible in the java.io bundle. The exemption happens when we attempt to get to that document which isn't accessible in the framework. It is a checked...

5 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

Java Array Generic

Creating Generic Array in Java A collection of comparable sorts of data is kept in an array. In Java, making a generic array is challenging. The type information of an array's...

4 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

Permutation Coefficient in Java

In this tutorial, we will get familiar with the permutation coefficient in Java.  We will understand it through examples and see different approaches to solving the problem. A permutation is a...

5 minutes read.

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a...

3 minutes read.

Java Interface Keyword

An interface is also known as the blueprint in Java. It has constants of static values and methods of abstraction. The interface is a mechanism used by Java to declare...

3 minutes read.

Concurrent Modification Exception In Java

When an object is attempted to be updated concurrently when it is not allowed, the ConcurrentModificationException arises. This error typically occurs while using Java Collection classes. When another thread is iterating...

5 minutes read.