Fix for JRUBY-2882. Handle error messages related to constructors better
[jruby.git] / bench / shootout / moments.ruby
blobd0eb5565f22175dfa805e7153d202482db3a269b
1 #!/usr/bin/ruby
2 # -*- mode: ruby -*-
3 # $Id: moments.ruby,v 1.2 2005-06-10 00:57:22 igouy-guest Exp $
4 # http://www.bagley.org/~doug/shootout/
6 # throw away unused parameter sent by benchmark framework
7 ARGV.shift()
9 def main ()
10 sum = 0.0
11 nums = []
12 num = nil
14 for line in STDIN.readlines()
15 num = Float(line)
16 nums << num
17 sum += num
18 end
20 n = nums.length()
21 mean = sum/n;
22 deviation = 0.0
23 average_deviation = 0.0
24 standard_deviation = 0.0
25 variance = 0.0
26 skew = 0.0
27 kurtosis = 0.0
29 for num in nums
30 deviation = num - mean
31 average_deviation += deviation.abs()
32 variance += deviation**2;
33 skew += deviation**3;
34 kurtosis += deviation**4
35 end
36 average_deviation /= n
37 variance /= (n - 1)
38 standard_deviation = Math.sqrt(variance)
40 if (variance > 0.0)
41 skew /= (n * variance * standard_deviation)
42 kurtosis = kurtosis/(n * variance * variance) - 3.0
43 end
45 nums.sort()
46 mid = n / 2
48 if (n % 2) == 0
49 median = (nums.at(mid) + nums.at(mid-1))/2
50 else
51 median = nums.at(mid)
52 end
54 printf("n: %d\n", n)
55 printf("median: %f\n", median)
56 printf("mean: %f\n", mean)
57 printf("average_deviation: %f\n", average_deviation)
58 printf("standard_deviation: %f\n", standard_deviation)
59 printf("variance: %f\n", variance)
60 printf("skew: %f\n", skew)
61 printf("kurtosis: %f\n", kurtosis)
62 end
64 main()