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 check whether the sign of the specified number is negative or negative zero with the help of Signbit() function provided by the math package. If the sign of the given number is negative, then this function will return true. Otherwise, return false. So, you need to add a math package in your program with the help of the import keyword to access Signbit() function.
Syntax:
func Signbit(x float64) bool
Example 1:
// Golang program to illustrate Signbit() Function package main import ( "fmt"
"math"
) // Main function func main() { // Using Signbit() function
res_1 := math.Signbit(-6)
res_2 := math.Signbit(54)
res_3 := math.Signbit(math.Inf(-1))
res_4 := math.Signbit(math.NaN())
res_5 := math.Signbit(math.Pi)
// Displaying the result
fmt.Println( "Result 1: " , res_1)
fmt.Println( "Result 2: " , res_2)
fmt.Println( "Result 3: " , res_3)
fmt.Println( "Result 4: " , res_4)
fmt.Println( "Result 5: " , res_5)
} |
Output:
Result 1: true Result 2: false Result 3: true Result 4: false Result 5: false
Example 2:
// Golang program to illustrate Signbit() Function package main import ( "fmt"
"math"
) // Main function func main() { // Using Signbit() function
nvalue := math.Signbit(-34)
if nvalue == true {
fmt.Println( "Sign of the " +
"given number is negative" )
} else {
fmt.Println( "Sign of the given " +
"number is not negative" )
}
} |
Output:
Sign of the given number is negative