×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

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

Parameters

  • 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 »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.