Open In App

math.Ldexp() Function in Golang with Examples

Last Updated : 28 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. This package provides Ldexp() function which is used to find the inverse of Frexp. Or in other words, this function return frac × 2**exp. So, you need to add a math package in your program with the help of the import keyword to access the Ldexp() function.

Syntax:

func Ldexp(frac float64, exp int) float64
  • If Ldexp(±0, exp), then this function will return ±0.
  • If Ldexp(±Inf, exp), then this function will return ±Inf.
  • If Ldexp(NaN, exp), then this function will return NaN.

Example 1:




// Golang program to illustrate 
// math.Ldexp() Function
package main
  
import (
    "fmt"
    "math"
)
  
// Main function
func main() {
  
    // Finding the inverse of Frexp
    // Using Ldexp() function
    res_1 := math.Ldexp(1.1, 5)
    res_2 := math.Ldexp(math.Inf(-1), 2)
    res_3 := math.Ldexp(+0, 1)
    res_4 := math.Ldexp(2, 5)
    res_5 := math.Ldexp(math.NaN(), 1)
  
    // Displaying the result
    fmt.Printf("\nResult 1: %f", res_1)
    fmt.Printf("\nResult 2: %f", res_2)
    fmt.Printf("\nResult 3: %f", res_3)
    fmt.Printf("\nResult 4: %f", res_4)
    fmt.Printf("\nResult 5: %f", res_5)
}


Output:

Result 1: 35.200000
Result 2: -Inf
Result 3: 0.000000
Result 4: 64.000000
Result 5: NaN

Example 2:




// Golang program to illustrate
// math.Ldexp() Function
package main
  
import (
    "fmt"
    "math"
)
  
// Main function
func main() {
  
    // Finding the inverse of Frexp
    // Using Ldexp() function
    nvalue_1 := math.Ldexp(2, 5)
    nvalue_2 := math.Ldexp(6, 2)
  
    // Finding the sum of inverse of Frexp
    res := nvalue_1 + nvalue_2
    fmt.Println("Result 1: ", nvalue_1)
    fmt.Println("Result 2: ", nvalue_2)
    fmt.Println("Sum of Result 1 and Result 2: ", res)
  
}


Output:

Result 1:  64
Result 2:  24
Sum of Result 1 and Result 2:  88


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads