ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.