Open In App

Copy the Sign of Given Number in Golang

Last Updated : 01 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. You are allowed to copy the sign of the specified number with the help of Copysign() function provided by the math package. So, you need to add a math package in your program with the help of the import keyword to access the Copysign() function.

Syntax:

func Copysign(a, b float64) float64

Here, this function returns a value with the magnitude of a and the sign of b.

Example 1:




// Golang program to illustrate how to
// copy the sign of the given number
  
package main
  
import (
    "fmt"
    "math"
)
  
// Main function
func main() {
  
    // Copying the sign of the
    // given numbers
    // Using Copysign() function
    res_1 := math.Copysign(2, -3)
    res_2 := math.Copysign(-8, 1)
    res_3 := math.Copysign(-67, -2)
  
    // Displaying the result
    fmt.Printf("Result 1: %.1f", res_1)
    fmt.Printf("\nResult 2: %.1f", res_2)
    fmt.Printf("\nResult 3: %.1f", res_3)
  
}


Output:

Result 1: -2.0
Result 2: 8.0
Result 3: -67.0

Example 2:




// Golang program to illustrate how to
// copy the sign of the given number
  
package main
  
import (
    "fmt"
    "math"
)
  
// Main function
func main() {
  
    // Copying the sign of 
    // the given numbers
    // Using Copysign() function
    nvalue_1 := math.Copysign(3, -1)
    nvalue_2 := math.Copysign(-4, 3)
    res := nvalue_1 + nvalue_2
    fmt.Printf("%.1f + %.1f = %.1f",
          nvalue_1, nvalue_2, res)
  
}


Output:

-3.0 + 4.0 = 1.0


Similar Reads

How to copy one slice into another slice in Golang?
A Slice is a variable-length sequence that stores elements of a similar type, you are not allowed to store different type of elements in the same slice. In Slice, you can copy one slice into another slice using the copy() function provided by the Go language. Or in other words, copy() function allows you to copy the elements of one slice into anoth
5 min read
How to Copy an Array into Another Array in Golang?
Arrays in Golang or Go programming language is much similar to other programming languages. In the program, sometimes we need to store a collection of data of the same type, like a list of student marks. Such type of collection is stored in a program using an Array. An array is a fixed-length sequence that is used to store homogeneous elements in t
4 min read
reflect.Copy() Function in Golang with Examples
Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Copy() Function in Golang is used to copies the contents of the source into destination until either destination has been filled or source has been exhausted. To a
2 min read
How to copy a map to another map in Golang?
Maps in Golang is a collection of unordered pairs of key-value. It is widely used because it provides fast lookups and values that can retrieve, update, or delete with the help of keys. In Map, you can copy a map to another map using the for loop provided by the Go language. In for loop, we fetch the index value 1 by 1 with the element and assign i
2 min read
io.Copy() Function in Golang with Examples
In Go language, io packages supply fundamental interfaces to the I/O primitives. And its principal job is to enclose the ongoing implementations of such king of primitives. The Copy() function in Go language is used to copy from the stated src i.e, source to the dst i.e, destination till either the EOF i.e, end of file is attained on src or an erro
3 min read
Copy an Array by Value and Reference into Another Array in Golang
Array in Golang is a numbered sequence of elements of the same type. The size of the array is fixed. We can access the elements by their index. You can declare an array of size n and type T specifying this way mentioned below. var array[n]T Go doesn't have an inbuilt function to copy an array to another array. There are two ways of copying an array
2 min read
How to Copy Struct Type Using Value and Pointer Reference in Golang?
A structure or struct in Golang is a user-defined data type that allows to combine data types of different kinds and act as a record. A struct variable in Golang can be copied to another variable easily using the assignment statement(=). Any changes made to the second struct will not be reflected back to the first struct. Example 1: // Golang progr
2 min read
Finding the Square Root of Given Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. You are allowed to find the square root of the specified number with the help of the Sqrt() function provided by the math package. So, you need to add a math package in your program with the hel
2 min read
Finding Log1p() of the Given Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. You are allowed to find the natural logarithm of 1 plus its specified argument with the help of the Log1p() function provided by the math package. So, you need to add a math package in your prog
2 min read
Finding Binary Exponent of Given Number in Golang
Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. You are allowed to find the binary exponent of the specified number with the help of Logb() function provided by the math package. So, you need to add a math package in your program with the hel
2 min read
Article Tags :