File: pi.rbi

package info (click to toggle)
nqp 2016.12%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,960 kB
  • ctags: 3,884
  • sloc: java: 25,027; perl: 910; makefile: 12; sh: 2
file content (25 lines) | stat: -rw-r--r-- 505 bytes parent folder | download | duplicates (4)
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
# naive approximation: pi = 4 * (1 - /1/3 + 1/5 ...)

LIMIT = 50000
def time() ; nqp::time_n() ; end
def sprintf(fmt, *args) ; nqp::sprintf(fmt, args) ; end

pi_over_4 = 0.0

print 'hang on a few moments...'
start_time = time()

# sum terms in reverse order to reduce accumulated error
n = LIMIT;

while n > 0 do
    m = 4.0*n - 1.0
    pi_over_4 += 1/(m - 2) - 1/m
    n -= 1
end

puts sprintf("(completed %d iterations in %.2f sec)",
             LIMIT, time() - start_time)

pi = pi_over_4 * 4
puts pi