1 Star 0 Fork 0

黄梓健 / go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
pi.go 598 Bytes
一键复制 编辑 原始数据 按行查看 历史
Russ Cox 提交于 2016-01-06 15:12 . doc/play: update URL for concurrent pi
// Concurrent computation of pi.
// See https://goo.gl/la6Kli.
//
// This demonstrates Go's ability to handle
// large numbers of concurrent processes.
// It is an unreasonable way to calculate pi.
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(pi(5000))
}
// pi launches n goroutines to compute an
// approximation of pi.
func pi(n int) float64 {
ch := make(chan float64)
for k := 0; k <= n; k++ {
go term(ch, float64(k))
}
f := 0.0
for k := 0; k <= n; k++ {
f += <-ch
}
return f
}
func term(ch chan float64, k float64) {
ch <- 4 * math.Pow(-1, k) / (2*k + 1)
}
1
https://gitee.com/HUANG_ZI_JIAN/go.git
git@gitee.com:HUANG_ZI_JIAN/go.git
HUANG_ZI_JIAN
go
go
1d78139128d6

搜索帮助