×

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

Java String Methods

Java String Methods Java String class is the most important class of the java.lang package. It is used to handle the String related operations. It contains a lot of built-in Java...

2 minutes read.

Java LinkedHashMap

LinkedHashMap implements the Map interface. It inherits the HashMap class. Some essential features of LinkedHashMap are: It contains the values based on the keys. It maintains the order of insertion. It contains unique...

5 minutes read.

Decagonal Numbers in Java

This section explains what is a decagonal number and how to write Java programmes that compute decagonal numbers. Both academics and Java programmer interviews regularly question about the Decagonal number...

3 minutes read.

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

JDBC vs ODBC

Difference Between JDBC and ODBC ODBC: ODBC (Open Database Connectivity) is the accepted method for accessing databases among organisations and programmers. A database is linked to other programmes, such as word processors, spreadsheets,...

4 minutes read.

Generics in Java

Generics in Java Parameterizedtypes mean generic. Generics allow types (Character, Integer, String, …, etc., as well as user-defined types) to act as parameters to interfaces, classes, and methods. Generics in Java...

9 minutes read.

Minimum Difference Between Groups of Size Two in Java

There is given an array with various integers in it. The goal is to divide the elements into distinct groups, each of which has just two, so that the difference...

3 minutes read.

Merge Sort in Java

Merge Sort in Java Merge sort in Java uses the divide and conquer approach to sort the given array/ list. There are three steps involved in the merge sort. 1) Divide the...

5 minutes read.

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple...

6 minutes read.

Interfaces and Classes in Strings in Java

CharBuffer: CharBuffer is utilized to implement the CharSequence interface. With the help of the mentioned class, we can allow character buffers to be utilized instead of CharSequences. We can consider the illustration...

4 minutes read.

Leap Year Program in Java

Leap Year Program in Java: A leap year is a calendar year that have an extra day i.e. 366 days instead of 365 days is called leap year. In other...

2 minutes read.

Java Math toDegrees() Method

The toDegrees() method of Java Math class converts a radian angle to an approximately equivalent angle measured in degrees. Syntax: public static double toDegrees(double angrad) Parameters: The parameter ‘angrad ‘represents an angle measured in...

2 minutes read.

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.Constructor chaining is the process of calling one constructor from another constructor using the same object....

2 minutes read.

Java Pop

The array, linked list, stack, queue, and other data structures are supported by Java programming. The insertion, deletion, and element searching operations are available for every data structure. And Java...

4 minutes read.

Java copy file

There are for the most part 3 methods for duplicating documents utilizing java language. They are as given underneath: Utilizing File StreamUtilizing FileChannel ClassUtilizing Files class. 1. Using File Stream: Here we are...

5 minutes read.

How to Convert String to char in Java

How to Convert String to char in Java There are two methods to convert String to char are: Using charAt() method Using tocharArray() method Using charAt() method This is the method of String class that...

3 minutes read.

Applet Life Cycle in Java

In this article, we are going to acknowledge you about what a applet is, what is its life cycle and stages in life cycle, along with the syntax and example...

4 minutes read.

Java If Keyword

Definition: The if statement specifies a section of Java code that will run if an if statement's condition is false. The following conditional statements can be used in Java: To provide a block...

3 minutes read.

Java LDAP Authentication

WHAT IS LDAP? Clients can communicate with directory services by sending requests and receiving responses using the Lightweight Directory Access Protocol (LDAP). The term "LDAP server" refers to a directory service...

7 minutes read.

Singleton Design Pattern in Java

In the singleton pattern, a class that only has one instance and offers a universal point of access is taken into consideration. It can also be defined in another way, a...

4 minutes read.