File: pi.rbi

package info (click to toggle)
nqp 2024.09%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,972 kB
  • sloc: java: 28,087; perl: 3,479; ansic: 451; makefile: 202; javascript: 68; sh: 1
file content (25 lines) | stat: -rw-r--r-- 520 bytes parent folder | download | duplicates (2)
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() ; 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) / 1000000000.0)

pi = pi_over_4 * 4
puts pi