当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Golang math.Float64frombits()用法及代码示例


Go语言为基本常数和数学函数提供了内置支持,以借助数学包对数字进行运算。该程序包提供了Float64frombits()函数,该函数返回与IEEE 754二进制表示形式a对应的浮点数,其中a的符号位和结果位于相同的位位置。因此,您需要借助import关键字在程序中添加数学包,以访问Float64frombits()函数。

用法:

func Float64frombits(a uint64) float64

范例1:

// Golang program to illustrate how to find 
// the floating-point number corresponding 
// to the IEEE 754 binary representation 
package main 
  
import ( 
    "fmt"
    "math"
) 
  
// Main function 
func main() { 
  
    // Finding floating-point number corresponding 
    // to the IEEE 754 binary representation  
    // of the given numbers 
    // Using Float64frombits() function 
    res_1:= math.Float64frombits(2) 
    res_2:= math.Float64frombits(1) 
    res_3:= math.Float64frombits(0) 
    res_4:= math.Float64frombits(23) 
  
    // Displaying the result 
    fmt.Println("Result 1:", res_1) 
    fmt.Println("Result 2:", res_2) 
    fmt.Println("Result 3:", res_3) 
    fmt.Println("Result 4:", res_4) 
  
}

输出:

Result 1: 1e-323
Result 2: 5e-324
Result 3: 0
Result 4: 1.14e-322

范例2:

// Golang program to illustrate how to find 
// the floating-point number corresponding 
// to the IEEE 754 binary representation 
package main 
  
import ( 
    "fmt"
    "math"
) 
  
// Main function 
func main() { 
  
    // Finding floating-point number corresponding 
    // to the IEEE 754 binary representation 
    // of the given numbers 
    // Using Float64frombits() function 
    nvalue_1:= math.Float64frombits(3) 
    nvalue_2:= math.Float64frombits(50) 
  
    // Sum of the given values 
    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: 1.5e-323
Result 2: 2.47e-322
Sum of Result 1 and Result 2: 2.6e-322



相关用法


注:本文由纯净天空筛选整理自Kirti_Mangal大神的英文原创作品 math.Float64frombits() Function in Golang With Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。