Copysign()

Published Oct 3, 2023
Contribute to Docs

The Copysign() of the math package is an inbuilt function that returns a value based on the magnitude of the first argument and the sign of the second argument.

Syntax

import "math"

CpSgnXY = math.Copysign(x, y)

Where CpSgnXY is the value, of type float64, composed of the magnitude of x and the sign of y.

Example

The following code demonstrates the basic implementations of .Copysign():

package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Copysign(4.9, -1))
}

The output will be:

-4.9

Codebyte Example

The following example shows how the Copysign() function handles various arguments.

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn Go on Codecademy