Copysign()
function is used to show the magnitude of x
and the sign of y
where x
and y
are the given inputs (parameters).
The standard math package of Go programming language has Copysign()
function. The purpose of this function is to get the return value (Output) with the magnitude of x
and sign of y
(where x
and y
are the arguments).
Syntax of Copysign()
Function in Go Language
The syntax of
Copysign()
function in Go Language is:
func Copysign(x, y float64) float64
Note: float64
is a data type in Go language which has IEEE-754 64-bit floating-point numbers.
Parameters of Copysign()
Function in Go Language
x
– Where x
is and y
are any Valid float64
Input values. This parameter is required.
Error Handling
If the
If there are no arguments (
x
or y
parameters are not numbers (numeric values) Copysign()
function will return an error
.If there are no arguments (
x
or y
– input values) passes to the function, then the compiler will produce an error
.
Return Value of Copysign()
Function in Go Language
Copysign()
function will return the magnitude of x
parameter and the sign of y
parameter.
GoLang Copysign()
Function Example 1
package main import "fmt" import "math" func main() { var x float64 x = math.Copysign(-20, 2) fmt.Println(x) }
Output:
20
GoLang Copysign()
Function Example 2
package main import "fmt" import "math" func main() { var x float64 x = math.Copysign(0, -4) fmt.Println(x) }
Output:
-0
GoLang Copysign()
Function Example 3
package main import "fmt" import "math" func main() { var x float64 x = math.Copysign(-17.6, -4) fmt.Println(x) }
Output:
-17.6