Menu Close

Go – How to check negative number in go

Here, We will learn how to check negative number in go. We can get it by using Signbit() function in math package in go golang.

Function prototype:

func Signbit(x float64) bool

Return value:

Signbit() function in math package returns 
whether x is negative or negative zero.

Example with code:

package main

import (
  "fmt"
  "math"
)

func main() {

  no := math.Signbit(1.2)
  fmt.Println(no)

  no = math.Signbit(-2.7)
  fmt.Println(no)

  no = math.Signbit(0.0)
  fmt.Println(no)

  no = math.Signbit(-2.9)
  fmt.Println(no)

  no = math.Signbit(2)
  fmt.Println(no)
}

Output:

false
true
false
true
false

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 *