×

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 DatagramSocket and Java DatagramPacket

Datagrams TCP/IP style networking specifies a serialized, predictable, and reliable stream of data in the form of a packet. Servers and clients communicate through a reliable channel, such as TCP socket, have a dedicated...

6 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.

Java vs Scala

Java : Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say...

4 minutes read.

Java Private keyword

A Java access modifier is a private keyword. It can be used to inner classes, methods, and variables. It is the type of access modifier that is most constrained. Privately declared...

4 minutes read.

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

Method Overloading In Java

If the same class consists of different methods with the same name and the methods can vary by different number of parameters then it is known as Method Overloading. Method...

2 minutes read.

Topological Sort In Java

Topological Sort in Java Topological sort is mainly used in the linear ordering of vertices in a Directed Acyclic Graph (DAG). Topological sort in Java illustrates how to do the linear ordering of...

1 minute read.

Program to Find the Common Elements between two Arrays in Java

In this article, we are going to learn how to find the common elements between two arrays using Java. Here, we use different approaches in Java to find the common...

3 minutes read.

Java Buffered Writer

BufferWriter Class: It is used to write the data more efficiently. This class is present in the java.io package, it inherits the data from the Writer class. Writer class is...

4 minutes read.

How to achieve Multiple Inheritance in Java

Multiple Inheritance is the type of inheritance where one class inherits the properties of more than one super class. For example, class Child inherits (extends) the classes Father and Mother....

4 minutes read.

Undo and Redo Operations in Java

Undo and redo operation are the most widely used operation while dealing with file. In this section, we will discuss how to implement undo and redo operation in Java. Undo Redo...

2 minutes read.

Java Location

PATH is an environmental variable that the system software now uses to find executable files (.exe) or Java binaries ( java or javac command). Once chosen, a route cannot be...

3 minutes read.

Java Beans

It is a Java class, It follows conventions they are: It must have a no-arg constructorIt must be serializable.It must provide the methods to get and set the properties Uses Of Java...

3 minutes read.

Class vs Object in Java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Java Identifiers

In Java, the symbolic notations used for identification are called identifiers. Identifiers can be the name for the class, variable name declaration, name of the package, constant name and many...

3 minutes read.

Java String indexOf() method

Java String indexOf() method returns index of a given character or substring present in a String. Methods Description int indexOf(int ch)     It returns index position for the given char value.int indexOf(int ch,...

2 minutes read.

Differences between Byte Code and Machine Code

This tutorial will discuss the differences between Byte Code and Machine Code. Before that, let's try to know what byte and machine code is. Introduction Every computer has a set of instructions...

4 minutes read.

Java String replace() method

Java String replace() method returns new String by replacing old characters with new characters or old CharSequence to new CharSequence. Syntax: public String replace(char oldChar, char newChar) public String replace(CharSequence target, CharSequence replacement) Parameters: oldChar...

2 minutes read.

Difference between Constructor and Method in Java

What is Constructor? In Constructor, we will discuss constructors and also will discuss default constructors and finally, we will discuss overloading constructors. A constructor is a method that is used to...

13 minutes read.

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.