Golang math.Float32bits() Function with Examples

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

math.Float32bits()

The Float32bits() function is an inbuilt function of the math package which is used to get the IEEE 754 binary representation of f, with the sign bit of f and the result in the same bit position. Where f is the given parameter.

It accepts a parameter (f) and returns the IEEE 754 binary representation of f.

Syntax:

func Float32bits(f float32) uint32

Parameter(s):

  • f : The value of float32 type whose IEEE 754 binary representation is to be found.

Return Value:

The return type of Float32bits() function is a uint32, it returns the IEEE 754 binary representation of the given parameter.

Example 1:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Float32bits(0))
	fmt.Println(math.Float32bits(5))
	fmt.Println(math.Float32bits(-5))
	fmt.Println(math.Float32bits(10.5))
	fmt.Println(math.Float32bits(-10.5))
	fmt.Println(math.Float32bits(0.01))
}

Output:

0
1084227584
3231711232
1093140480
3240624128
1008981770

Example 2:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float32
	var Float32bitsX uint32

	x = 0
	Float32bitsX = math.Float32bits(x)
	fmt.Println("Float32bits(", x, ") = ", Float32bitsX)

	x = 0.5
	Float32bitsX = math.Float32bits(x)
	fmt.Println("Float32bits(", x, ") = ", Float32bitsX)

	x = 6
	Float32bitsX = math.Float32bits(x)
	fmt.Println("Float32bits(", x, ") = ", Float32bitsX)

	x = -6
	Float32bitsX = math.Float32bits(x)
	fmt.Println("Float32bits(", x, ") = ", Float32bitsX)

	x = -0.15
	Float32bitsX = math.Float32bits(x)
	fmt.Println("Float32bits(", x, ") = ", Float32bitsX)
}

Output:

Float32bits( 0 ) =  0
Float32bits( 0.5 ) =  1056964608
Float32bits( 6 ) =  1086324736
Float32bits( -6 ) =  3233808384
Float32bits( -0.15 ) =  3189348762

Golang math Package Constants and Functions »





Comments and Discussions!

Load comments ↻






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