Golang complx.Polar()函数的例子
complx.Polar()函数 返回复数x的绝对值r和相位θ。相位θ将在[-Pi,Pi]范围内。
语法:
func Polar(x complex128) (r, θ float64)
在这里,x是一个复数。
返回值: 返回值在[-Pi,Pi]范围内。
例子1:
//Golang程序演示
//complx.Polar()函数
package main
//导入fmt,math和math/cmplx
import (
"fmt"
"math"
"math/cmplx"
)
//调用主函数
func main() {
//获取r和theta值
r,theta:=cmplx.Polar(7i)
//以Pi格式计算值。
p:=theta/math.Pi
//打印出r和p的值
fmt.Printf("rValue: %.1f,pValue: %.1f*Pi", r, p)
}
输出:
rValue: 7.0,pValue: 0.5*Pi
例子2:
//Golang程序演示
//complx.Polar()函数
package main
//导入fmt,math和math/cmplx
import (
"fmt"
"math"
"math/cmplx"
)
//调用主函数
func main() {
//我们也可以像这样创建一个复数。
n:=complex(5, 6)
//获取复数5+6i的r和theta值
r,theta:=cmplx.Polar(n)
//以Pi格式计算值。
p:=theta/math.Pi
//打印出r和p的值
fmt.Printf("rValue: %.1f,pValue: %.1f*Pi", r, p)
}
输出:
rValue: 7.8,pValue: 0.3*Pi
极客教程