-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconcurrent_pi_ex
executable file
·55 lines (42 loc) · 1.16 KB
/
concurrent_pi_ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
#
# Example that corresponds to "Concurrent pi" on https://go.dev/play/.
# License for the corresponding code https://go.dev/LICENSE?m=text.
readonly DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
. ${DIR}/../../gobash
function bcs() {
# Unit function for bc with specific scale.
local -r exp="${1}"
shift 1
bc <<< "scale=4; ${exp}"
}
function pi() {
local -r n="${1}"
shift 1
local -r ch=$(Chan)
for k in $(seq 0 "${n}"); do
( term "$ch" "${k}" ) &
done
local f="3.0"
for k in $(seq 0 "${n}"); do
local v=$($ch recv)
f=$(bcs "${f} + ${v}")
done
echo "${f}"
}
function term() {
local -r ch="${1}"
local -r k="${2}"
shift 2
local -r ke=$(bcs "-1^${k}")
local -r k2=$(bcs "2 * ${k} + 2")
local -r k3=$(bcs "2 * ${k} + 3")
local -r k4=$(bcs "2 * ${k} + 4")
local val=$(bcs "4 * ${ke} / (${k2} * ${k3} * ${k4})")
$ch send "${val}"
}
function main() {
printf "MATH_PI %g\n" $MATH_PI
printf "Nilakantha %g\n" $(pi 10)
}
main