Golang complx.Phase() 函数及示例
在 Go 语言中,cmplx 包提供基本常量以及复数的数学函数。Go 语言中的 Phase() 函数 用于返回给定数字的相位(也称为参数)。 此外,此函数在 cmplx 包下定义。这里需要导入 “math/cmplx” 包以使用这些函数。
语法:
func Phase(x complex128) float64
返回值在 [-π,π] 范围内。
示例 1:
// Golang program to illustrate the complex.Phase() function
package main
import (
"fmt"
"math"
"math/cmplx"
)
// 主函数
func main() {
// 使用函数
fmt.Printf("%.1f", cmplx.Phase(1i*math.Pi)+1)
}
输出:
2.6
示例 2:
// Golang program to illustrate the complex.Phase() function
package main
import (
"fmt"
"math"
"math/cmplx"
)
// 主函数
func main() {
// 使用函数
fmt.Printf("%.1f, ", cmplx.Phase(1i*math.Pi))
fmt.Printf("%.1f", cmplx.Phase(2i*math.Pi)+3)
}
输出:
1.6, 4.6
极客教程