File: pi.rbi

package info (click to toggle)
nqp 2014.07-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,596 kB
  • ctags: 7,993
  • sloc: ansic: 22,689; java: 20,240; cpp: 4,956; asm: 3,976; perl: 950; python: 267; sh: 245; makefile: 14
file content (25 lines) | stat: -rwxr-xr-x 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