Golang math.Ldexp() Function with Examples

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

math.Ldexp()

The Ldexp() function is an inbuilt function of the math package which is used to get the inverse of Frexp. It returns frac × 2**exp.

It accepts two parameters (frac and exp) and returns the inverse of Frexp. It returns frac × 2**exp.

Syntax:

func Ldexp(frac float64, exp int) float64

Parameter(s):

  • frac : The fractional value to be used to get the result of the expression (frac × 2**exp).
  • exp : The exponent value to be used to get the result of the expression (frac × 2**exp).

Return Value:

The return type of Ldexp() function is a float64, it returns the inverse of Frexp. It returns frac × 2**exp.

Special cases:

  • Ldexp(±0, exp) = ±0
    If the value of frac is ±0, the function returns ±0.
  • Ldexp(±Inf, exp) = ±Inf
    If the value of frac is ±Inf, the function returns ±Inf.
  • Ldexp(NaN, exp) = NaN
    If the value of frac is NaN, the function returns NaN.

Example 1:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Ldexp(0, 0))
	fmt.Println(math.Ldexp(0.5, 1))
	fmt.Println(math.Ldexp(0.5, 2))
	fmt.Println(math.Ldexp(0.5, 6))
	fmt.Println(math.Ldexp(0.94140625, 7))

	fmt.Println(math.Ldexp(-0.5, 1))
	fmt.Println(math.Ldexp(-0.5, 2))
	fmt.Println(math.Ldexp(-0.5, 6))
	fmt.Println(math.Ldexp(-0.94140625, 7))

	fmt.Println(math.Ldexp(math.Inf(-1), 0))
	fmt.Println(math.Ldexp(math.Inf(+1), 0))
	fmt.Println(math.Ldexp(math.NaN(), 0))
}

Output:

0
1
2
32
120.5
-1
-2
-32
-120.5
-Inf
+Inf
NaN

Example 2:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var frac float64
	var exp int
	var result float64

	frac = 0
	exp = 0
	result = math.Ldexp(frac, exp)
	fmt.Println("Ldexp(", frac, ",", exp, ") = ", result)

	frac = 0.5
	exp = 1
	result = math.Ldexp(frac, exp)
	fmt.Println("Ldexp(", frac, ",", exp, ") = ", result)

	frac = 0.5
	exp = 2
	result = math.Ldexp(frac, exp)
	fmt.Println("Ldexp(", frac, ",", exp, ") = ", result)

	frac = 0.5
	exp = 6
	result = math.Ldexp(frac, exp)
	fmt.Println("Ldexp(", frac, ",", exp, ") = ", result)

	frac = -0.5
	exp = 6
	result = math.Ldexp(frac, exp)
	fmt.Println("Ldexp(", frac, ",", exp, ") = ", result)

	frac = math.Inf(1)
	exp = 0
	result = math.Ldexp(frac, exp)
	fmt.Println("Ldexp(", frac, ",", exp, ") = ", result)

	frac = math.NaN()
	exp = 0
	result = math.Ldexp(frac, exp)
	fmt.Println("Ldexp(", frac, ",", exp, ") = ", result)
}

Output:

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

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





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