easystats 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/easystats/version.rb +1 -1
  2. data/lib/easystats.rb +31 -11
  3. metadata +3 -3
@@ -1,3 +1,3 @@
1
1
  module Easystats
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/lib/easystats.rb CHANGED
@@ -4,8 +4,13 @@
4
4
  data = self
5
5
  sum_of_numbers = 0
6
6
 
7
- data.each do |num|
8
- sum_of_numbers += num
7
+ # Get the total sum only if there are actually numbers to total
8
+ if data.count > 0
9
+ data.each do |num|
10
+ sum_of_numbers += num
11
+ end
12
+ else
13
+ sum_of_numbers = 0
9
14
  end
10
15
 
11
16
  sum_of_numbers
@@ -14,7 +19,13 @@
14
19
  # take in an array of numbers and calculate the mean (average)
15
20
  def mean
16
21
  data = self
17
- self.sum / data.count.to_f
22
+
23
+ # Check to make sure there are numbers to avoid division by 0
24
+ if data.count > 0
25
+ self.sum / data.count.to_f
26
+ else
27
+ 0
28
+ end
18
29
  end
19
30
 
20
31
  # this is an internat function (technically the developer can use it but should have no need)
@@ -24,32 +35,41 @@
24
35
 
25
36
  deviations = Array.new
26
37
  average = self.mean
27
- sum_of_deviations = 0
38
+ sum_of_deviations_squared = 0
28
39
 
29
40
  data.each do |num|
30
41
  deviations.push((num-average)**2)
31
42
  end
32
43
 
33
44
  deviations.each do |num|
34
- sum_of_deviations += num
45
+ sum_of_deviations_squared += num
35
46
  end
36
47
 
37
- sum_of_deviations
48
+ sum_of_deviations_squared
38
49
  end
39
50
 
40
51
  # take in an array of numbers and calculate the standard deviation
41
52
  def standard_deviation
42
53
  data = self
43
- sum_of_deviations = self.sum_of_deviations_squared
54
+ sum_of_deviations_squared = self.sum_of_deviations_squared
44
55
 
45
- Math::sqrt(sum_of_deviations / (data.count-1))
56
+ if data.count > 1
57
+ Math::sqrt(sum_of_deviations_squared / (data.count-1))
58
+ else
59
+ 0
60
+ end
46
61
  end
47
62
 
48
63
  def variance
49
64
  data = self
50
65
  average = self.mean
51
66
  sum_of_deviations = self.sum_of_deviations_squared
52
- sum_of_deviations / data.count.to_f
67
+
68
+ if data.count > 0
69
+ sum_of_deviations / data.count.to_f
70
+ else
71
+ 0
72
+ end
53
73
  end
54
74
 
55
75
  # take in the array of numbers and calculate the median
@@ -117,7 +137,7 @@
117
137
  end
118
138
 
119
139
  if no_mode == true
120
- "There is no mode"
140
+ 0
121
141
  else
122
142
  data.each do |num|
123
143
  if tmp["#{num}"].to_i > most_times
@@ -136,7 +156,7 @@
136
156
  end
137
157
 
138
158
  if no_mode == true
139
- "There is no mode"
159
+ 0
140
160
  else
141
161
  highest_value
142
162
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 5
9
- version: 0.0.5
8
+ - 6
9
+ version: 0.0.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Matthew Grigajtis
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-27 00:00:00 -05:00
17
+ date: 2011-01-28 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies: []
20
20