ADVERTISEMENT
ADVERTISEMENT

Golang math.Copysign() Function with Examples

Golang | math.Copysign() Function: Here, we are going to learn about the Copysign() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 29, 2021

math.Copysign()

The Copysign() function is an inbuilt function of the math package which is used to get a value with the magnitude of x and the sign of y. Where, x is the first parameter, and y is the second parameter.

It accepts two parameters and returns a value with the magnitude of x and the sign of y.

Syntax:

func Copysign(x, y float64) float64

Parameter(s):

  • x, y : The values whose magnitude and sign are to be copied.

Return Value:

The return type of Copysign() function is a float64, it returns a value with the magnitude of x and the sign of y.

Example 1:

// Golang program to demonstrate the
// example of math.Copysign() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Copysign(1.49, -2))
	fmt.Println(math.Copysign(2.50, 3.5))
	fmt.Println(math.Copysign(-2.51, 2.3))

	fmt.Println(math.Copysign(1, 0))
	fmt.Println(math.Copysign(1, 0))
	fmt.Println(math.Copysign(-1, -1))
}

Output:

-1.49
2.5
2.51
1
1
-1

Example 2:

// Golang program to demonstrate the
// example of math.Copysign() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var y float64
	var CopysignXY float64

	x = 1.49
	y = -2.5
	CopysignXY = math.Copysign(x, y)
	fmt.Println("Copysign(", x, ",", y, ") = ", CopysignXY)

	x = -1.49
	y = 2.5
	CopysignXY = math.Copysign(x, y)
	fmt.Println("Copysign(", x, ",", y, ") = ", CopysignXY)

	x = -1.49
	y = -2.5
	CopysignXY = math.Copysign(x, y)
	fmt.Println("Copysign(", x, ",", y, ") = ", CopysignXY)

	x = 1
	y = 0
	CopysignXY = math.Copysign(x, y)
	fmt.Println("Copysign(", x, ",", y, ") = ", CopysignXY)
}

Output:

Copysign( 1.49 , -2.5 ) =  -1.49
Copysign( -1.49 , 2.5 ) =  1.49
Copysign( -1.49 , -2.5 ) =  -1.49
Copysign( 1 , 0 ) =  1

Golang math Package Constants and Functions »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.