Tutorial Gateway

  • C
  • C#
  • Python
  • SQL
  • Java
  • JS
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs
  • MySQL

Java copySign Function

The Java copySign Function is one of the Java Math Library functions used to find the absolute value of the first argument. This Java copysign function returns the absolute value along with the sign specified in the second argument.

Java copySign Function syntax

The basic syntax of the Math.copySign Function in Java Programming language is as shown below:

Math.copySign(data_type magnitude, data_type sign);
  • magnitude: Please specify the magnitude value here. Math.copySign Function will find the absolute value of this argument. If you find it challenging to understand the absolute values, please refer Java abs Method article.
  • sign: Please specify the Sign value here. Math.copySign Function uses this argument sign (Positive or Negative) and returns the output.

For example, if magnitude = 10.45 and sign = – 5.40, then Math.copySign(10.45, -5.40) = -10.45. It is because the absolute value of 10.45 is 10.45 and the second argument sign is Negative. So, the output is -10.45

Java Programming provides two different Math.copySign Java functions to find the Maximum or Largest value from the given two arguments. The following Java copySign function will accept positive or negative double value as the first and second argument and returns the first floating value along with the sign specified in the second argument.

static double copySign(double magnitude,  double sign); //Return Type is double

// In order to use in program: 
Math.copySign(double magnitude,  double sign);

It accepts positive or negative float values and returns the first floating value along with the sign specified in the second.

static float copySign(float magnitude,  float sign); //Return Type is float

// In order to use in program: 
Math.copySign(float magnitude,  float sign);

Java copySign Function Example

In this example, We use math.copySign function to find the absolute values of different data types and display the output along with sign.

// Java Math.copySign Function
package MathFunctions;

public class CopySign {
	public static void main(String[] args) {
		double b = 10.9666, c = 14.6674, d = -9.474, e = -14.9865;
		float g = 1.23f, h = 4.809f, i = -7.89f, j = -6.54f;
		
		System.out.println("CopySign result of Positive Values: " + Math.copySign(10, 20));
		System.out.println("CopySign result of Negative Values: " + Math.copySign(-10, -20));
		System.out.println("CopySign result of both Positive & Negative Values: " + Math.copySign(10, -20));
		System.out.println("CopySign result of both Positive & Negative Values: " + Math.copySign(-5, 10));
		
		System.out.println("\nCopySign result of Positive Doubles: " + Math.copySign(b, c));
		System.out.println("CopySign result of Negative Double Values: " + Math.copySign(d, e));
		System.out.println("CopySign result of both Positive & Negative Double Values: " + Math.copySign(c, d));
		System.out.println("CopySign result of both Positive & Negative Double Values: " + Math.copySign(e, b));
		
		System.out.println("\nCopySign result of Positive Float Values: " + Math.copySign(g, h));
		System.out.println("CopySign result of Negative Float Values: " + Math.copySign(i, j));
		System.out.println("CopySign result of both Positive & Negative Float Values: " + Math.copySign(h, i));
		System.out.println("CopySign result of both Positive & Negative Float Values: " + Math.copySign(j, h));
	}
}
Java copySign Function 1

We used the Math.copySign Function directly on both Positive and negative values.

System.out.println("CopySign result of Positive Values: " + Math.copySign(10, 20));
System.out.println("CopySign result of Negative Values: " + Math.copySign(-10, -20));
System.out.println("CopySign result of both Positive & Negative Values: " + Math.copySign(10, -20));
System.out.println("CopySign result of both Positive & Negative Values: " + Math.copySign(-5, 10));

Next, We used the Java Math.copySign Function on variable b, c, d, and e (they belongs to double type). The following statements will call the copySign method of double type ( static double copySign(double magnitude, double sign) ) to display the result.

System.out.println("\nCopySign result of Positive Doubles: " + Math.copySign(b, c));
System.out.println("CopySign result of Negative Double Values: " + Math.copySign(d, e));
System.out.println("CopySign result of both Positive & Negative Double Values: " + Math.copySign(c, d));
System.out.println("CopySign result of both Positive & Negative Double Values: " + Math.copySign(e, b));

Next, We used the copySign Math function on variable g, h, i, and j (they belong to float type). The following statements call the copySign method of float type ( static float copySign(float magnitude, float sign) ) to display the result.

System.out.println("\nCopySign result of Positive Float Values: " + Math.copySign(g, h));
System.out.println("CopySign result of Negative Float Values: " + Math.copySign(i, j));
System.out.println("CopySign result of both Positive & Negative Float Values: " + Math.copySign(h, i));
System.out.println("CopySign result of both Positive & Negative Float Values: " + Math.copySign(j, h));

Filed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial

Copyright © 2021ยท All Rights Reserved by Suresh.
About | Contact | Privacy Policy