ADVERTISEMENT
ADVERTISEMENT

Golang math.Frexp() Function with Examples

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

math.Frexp()

The Frexp() function is an inbuilt function of the math package which is used to break the given value (f) into a normalized fraction and an integral power of 2. It returns the frac and exp satisfying f == frac × 2**exp, with the absolute value of frac in the interval [½, 1).

It accepts a parameter (f) and returns the frac and exp satisfying f == frac × 2**exp.

Syntax:

func Frexp(f float64) (frac float64, exp int)

Parameter(s):

  • f : The value to be broken in a normalized fraction and an integral power of 2.

Return Value:

The return type of Frexp() function is (frac float64, exp int), it returns the frac and exp satisfying f == frac × 2**exp, with the absolute value of frac in the interval [½, 1].

Special cases:

  • Frexp(±0) = ±0, 0
    If the parameter is ±0, the function returns frac (±0) and exp (0).
  • Frexp(±Inf) = ±Inf, 0
    If the parameter is ±Inf, the function returns frac (±Inf) and exp (0).
  • Frexp(NaN) = NaN, 0
    If the parameter is NaN, the function returns frac (NaN) and exp (0).

Example 1:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Frexp(0))
	fmt.Println(math.Frexp(1))
	fmt.Println(math.Frexp(2))
	fmt.Println(math.Frexp(32))
	fmt.Println(math.Frexp(120.5))

	fmt.Println(math.Frexp(-1))
	fmt.Println(math.Frexp(-2))
	fmt.Println(math.Frexp(-32))
	fmt.Println(math.Frexp(-120.5))

	fmt.Println(math.Frexp(math.Inf(-1)))
	fmt.Println(math.Frexp(math.Inf(+1)))
	fmt.Println(math.Frexp(math.NaN()))
}

Output:

0 0
0.5 1
0.5 2
0.5 6
0.94140625 7
-0.5 1
-0.5 2
-0.5 6
-0.94140625 7
-Inf 0
+Inf 0
NaN 0

Example 2:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64

	x = 0
	FrxX, ExpX := math.Frexp(x)
	fmt.Println("Frexp(", x, ") = ", FrxX, ExpX)

	x = 1
	FrxX, ExpX = math.Frexp(x)
	fmt.Println("Frexp(", x, ") = ", FrxX, ExpX)

	x = 2
	FrxX, ExpX = math.Frexp(x)
	fmt.Println("Frexp(", x, ") = ", FrxX, ExpX)

	x = 32
	FrxX, ExpX = math.Frexp(x)
	fmt.Println("Frexp(", x, ") = ", FrxX, ExpX)

	x = -32
	FrxX, ExpX = math.Frexp(x)
	fmt.Println("Frexp(", x, ") = ", FrxX, ExpX)

	x = math.Inf(1)
	FrxX, ExpX = math.Frexp(x)
	fmt.Println("Frexp(", x, ") = ", FrxX, ExpX)

	x = math.NaN()
	FrxX, ExpX = math.Frexp(x)
	fmt.Println("Frexp(", x, ") = ", FrxX, ExpX)
}

Output:

Frexp( 0 ) =  0 0
Frexp( 1 ) =  0.5 1
Frexp( 2 ) =  0.5 2
Frexp( 32 ) =  0.5 6
Frexp( -32 ) =  -0.5 6
Frexp( +Inf ) =  +Inf 0
Frexp( NaN ) =  NaN 0

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.