Menu Close

Go – How to get value with the magnitude of x and the sign of y in go

Here, We will learn how to get value with the magnitude of x and the sign of y in go. We can get it by using Copysign() function in math package in go golang.

Function prototype:

func Copysign(x, y float64) float64

Return value:

Copysign() function in math package returns
a value with the magnitude of x and the sign of y.

Example with code:

package main

import (
  "fmt"
  "math"
)

func main() {

  no := math.Copysign(2.2, -1)
  fmt.Printf("%.2f\n", no)

  no = math.Copysign(1.2, -2)
  fmt.Printf("%.2f\n", no)

  no = math.Copysign(0.2, 1)
  fmt.Printf("%.2f\n", no)

  no = math.Copysign(-2.2, -1)
  fmt.Printf("%.2f\n", no)

  no = math.Copysign(-5, 1)
  fmt.Printf("%.2f\n", no)
}

Output:

-2.20
-1.20
0.20
-2.20
5.00

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, math

Leave a Reply

Your email address will not be published. Required fields are marked *