Golang math.Signbit() Function with Examples

Golang | math.Signbit() Function: Here, we are going to learn about the Signbit() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 03, 2021

math.Signbit()

The Signbit() function is an inbuilt function of the math package which is used to check whether the given value is negative or negative zero.

It accepts a parameter (x), and checks whether x is negative or negative zero.

Syntax:

func Signbit(x float64) bool

Parameter(s):

  • x : The value whose sign bit is to be checked.

Return Value:

The return type of Signbit() function is a bool, it returns true if the given value is negative or negative zero; false, otherwise.

Example 1:

// Golang program to demonstrate the
// example of math.Signbit() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Signbit(-1))
	fmt.Println(math.Signbit(-123.45))
	fmt.Println(math.Signbit(-150.0))

	fmt.Println(math.Signbit(15.50))
	fmt.Println(math.Signbit(20.65))

	fmt.Println(math.Signbit(0))
	fmt.Println(math.Signbit(math.Inf(-1)))
	fmt.Println(math.Signbit(math.NaN()))
}

Output:

true
true
true
false
false
false
true
false

Example 2:

// Golang program to demonstrate the
// example of math.Signbit() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64

	x = 1.0
	if math.Signbit(x) == true {
		fmt.Println(x, "is negative")
	} else {
		fmt.Println(x, "is not negative")
	}

	x = -123.50
	if math.Signbit(x) == true {
		fmt.Println(x, "is negative")
	} else {
		fmt.Println(x, "is not negative")
	}

	x = math.Inf(-1)
	if math.Signbit(x) == true {
		fmt.Println(x, "is negative")
	} else {
		fmt.Println(x, "is not negative")
	}
}

Output:

1 is not negative
-123.5 is negative
-Inf is negative

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





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