Golang math.Ldexp() 函数的使用及示例

Golang math.Ldexp() 函数的使用及示例

Go 语言通过 math 包提供了内置的支持,用于执行数字操作的基本常量和数学函数。这个包提供了 Ldexp() 函数 ,用于查找 Frexp 的倒数。或者换句话说,此函数返回 frac × 2**exp。因此,您需要在程序中使用 import 关键字添加 math 包才能访问 Ldexp() 函数。

语法:

func Ldexp(frac float64, exp int) float64
  • 如果 Ldexp(±0, exp),那么此函数将返回 ±0。
  • 如果 Ldexp(±Inf, exp),那么此函数将返回 ±Inf。
  • 如果 Ldexp(NaN, exp),那么此函数将返回 NaN。

    示例 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)
} 

输出:

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

示例 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)
  
} 

输出:

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程