pretty_round 0.0.1 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 715c35c7f940f136ab635fefb6f253f72ce4e712
4
- data.tar.gz: 6ff79a9c50c397001a2f12ac00d7076d03316162
3
+ metadata.gz: 81b0efd5424a3bcdeacf134caec4d82801f241ab
4
+ data.tar.gz: 5a3e997f35204b86c08c3e65566cfde0bdcf5389
5
5
  SHA512:
6
- metadata.gz: 6987805eeb0bb0183f77ef2a5605d8da58e5dcf5bc030548681269ddc36d93f9cb491de404d66626f5228100a321cef8c70c7c24fb8fe192d5ca696517deed77
7
- data.tar.gz: c4e508d1aaec2a2fa399ad5fdf597088d1d7407b5640ff852bb06237a48b1f97de66848725b9f643cb0bb4ab689f248d540e544ee94ce2ec9b90bfea49f91aa3
6
+ metadata.gz: e7873f9c7ee8c9e129bf565ed0938421892bad9fecfa8b473d20e1bfe6be0e91be532a367e18c674ed54c1d12a032dc6e5cb38924e6192bdd88d98ba3b3e76fd
7
+ data.tar.gz: de9fa9b940b5246a221e2be45485a49cc6fe9051b04a60b209d9cab2ea17979708384981b5ee0c6e1323175d3eafe448ee91a293b987b815b9fa214331f779f1
data/README.md CHANGED
@@ -16,47 +16,75 @@ require "pretty_round"
16
16
  x = 123.456
17
17
 
18
18
  # round with precision
19
- x.round(2) #=> 123.46 (ruby built-in)
20
19
  x.roundup(2) #=> 123.46
21
20
  x.rounddown(2) #=> 123.45
22
21
 
23
22
  # round to nearest multiple
24
- x.mroundup(10) #=> 130
25
- x.mrounddown(0.01) #=> 123.45
26
- x.mround(50) #=> 100
27
- x.mround(0.02) #=> 123.46
23
+ x.mceil(50) #=> 150
24
+ x.mfloor(25) #=> 100
25
+ x.mroundup(2) #=> 124
26
+ x.mrounddown(0.3) #=> 123.3
27
+ x.mround(9.87) #=> 128.31
28
28
 
29
29
  # round with significant digit
30
- x.sround(2) #=> 120
31
- x.sround(4) #=> 123.5
30
+ x.sceil(1) #=> 200
31
+ x.sfloor(2) #=> 120
32
+ x.sroundup(3) #=> 124
33
+ x.srounddown(4) #=> 123.4
34
+ x.sround(5) #=> 123.46
32
35
  ```
33
36
 
34
37
  Rounding direction
35
38
  ================
39
+ Also, try to run `examples/example.rb`
36
40
 
37
- `#roundup`, `#mroundup`
41
+ `#mceil`, `#sceil`
38
42
  ----------------
39
43
  These methods round up to the positive infinity direction.
44
+ Its behavior is same as built-in #ceil.
45
+ ```rb
46
+ 1.9.mceil(1) #=> 2
47
+ 1.1.mceil(1) #=> 2
48
+ -1.1.mceil(1) #=> -1
49
+ -1.9.mceil(1) #=> -1
50
+ ```
51
+
52
+ `#mfloor`, `#sfloor`
53
+ ----------------
54
+ These methods round down to the negative infinity direction.
55
+ Its behavior is same as built-in #floor.
56
+ ```rb
57
+ 1.9.mfloor(1) #=> 1
58
+ 1.1.mfloor(1) #=> 1
59
+ -1.1.mfloor(1) #=> -2
60
+ -1.9.mfloor(1) #=> -2
61
+ ```
62
+
63
+ `#roundup`, `#mroundup`, `#sroundup`
64
+ ----------------
65
+ These methods round up to far from 0 direction.
40
66
  ```rb
41
67
  1.9.mroundup(1) #=> 2
42
68
  1.1.mroundup(1) #=> 2
43
- -1.1.mroundup(1) #=> -1
44
- -1.9.mroundup(1) #=> -1
69
+ -1.1.mroundup(1) #=> -2
70
+ -1.9.mroundup(1) #=> -2
45
71
  ```
46
72
 
47
- `#rounddown`, `#mrounddown`
73
+ `#rounddown`, `#mrounddown`, `#srounddown`
48
74
  ----------------
49
- These methods round down to the negative infinity direction.
75
+ These methods round down to near from 0 direction.
50
76
  ```rb
51
77
  1.9.mrounddown(1) #=> 1
52
78
  1.1.mrounddown(1) #=> 1
53
- -1.1.mrounddown(1) #=> -2
54
- -1.9.mrounddown(1) #=> -2
79
+ -1.1.mrounddown(1) #=> -1
80
+ -1.9.mrounddown(1) #=> -1
55
81
  ```
56
82
 
83
+
57
84
  `#mround`, `#sround`
58
85
  ----------------
59
86
  These methods round off the absolute value, dos not round even.
87
+ Its behavior is same as built-in #round.
60
88
  ```rb
61
89
  1.9.mround(1) #=> 2
62
90
  1.1.mround(1) #=> 1
@@ -64,7 +92,3 @@ These methods round off the absolute value, dos not round even.
64
92
  -1.9.mround(1) #=> -2
65
93
  ```
66
94
 
67
- ----
68
- Also, try to run `examples/example.rb`
69
-
70
-
data/examples/example.rb CHANGED
@@ -1,39 +1,32 @@
1
1
 
2
2
  require "pretty_round"
3
3
 
4
- xs = %w[2.1 2.5 2.9 -2.1 -2.5 -2.9].map(&:to_r)
4
+ puts "* Rounding to nearerst multiple"
5
+ xs = %w[2.9 2.5 2.1 -2.1 -2.5 -2.9].map(&:to_r)
5
6
  ns = %w[0.1 0.2 1 2 -0.1 -0.2 -1 -2].map(&:to_r)
6
7
 
7
- header = " "*6 + "|" + ns.map{|x| x.to_f.to_s.rjust(6)}.join + "\n" + "-"*(6+1+6*ns.size)
8
-
9
- puts "#mroundup"
10
- puts header
11
- xs.each do |x|
12
- puts ("%6.1f"%x) + "|" + ns.map{|n| x.mroundup(n)}.map{|n| ("%6.1f"%n)}.join
8
+ %i[mceil mfloor mroundup mrounddown mround].each do |mthd|
9
+ puts "##{mthd}"
10
+ puts " "*6 + "|" + ns.map{|x| x.to_f.to_s.rjust(6)}.join
11
+ puts "-"*(6+1+6*ns.size)
12
+ xs.each do |x|
13
+ puts ("%6.1f"%x) + "|" + ns.map{|n| x.send(mthd, n)}.map{|n| ("%6.1f"%n)}.join
14
+ end
15
+ puts
13
16
  end
14
17
  puts
15
18
 
16
- puts "#mrounddown"
17
- puts header
18
- xs.each do |x|
19
- puts ("%6.1f"%x) + "|" + ns.map{|n| x.mrounddown(n)}.map{|n| ("%6.1f"%n)}.join
20
- end
21
- puts
22
-
23
- puts "#mround"
24
- puts header
25
- xs.each do |x|
26
- puts ("%6.1f"%x) + "|" + ns.map{|n| x.mround(n)}.map{|n| ("%6.1f"%n)}.join
27
- end
28
- puts
29
19
 
20
+ puts "* Rounding with significant digit"
21
+ xs = [0.12345, 123.45, 12345, -0.12345, -123.45, -12345]
22
+ ns = [*1..5]
30
23
 
31
- puts "#sround"
32
- xs = [0.1234, 12.34, 1234, -0.1234, -12.34, -1234]
33
- ns = [*-1..5]
34
- puts " "*10 + "|" + ns.map{|x| x.to_s.center(10)}.join
35
- puts "-"*(10+1+10*ns.size)
36
- xs.each do |x|
37
- puts ("% 10.4f"%x) + "|" + ns.map{|n| x.sround(n)}.map{|n| ("% 10.4f"%n)}.join
24
+ %i[sceil sfloor sroundup srounddown sround].each do |mthd|
25
+ puts "##{mthd}"
26
+ puts " "*13 + "|" + ns.map{|x| x.to_s.center(13)}.join
27
+ puts "-"*(13+1+13*ns.size)
28
+ xs.each do |x|
29
+ puts ("% 13.5f"%x) + "|" + ns.map{|n| x.send(mthd, n)}.map{|n| ("% 13.5f"%n)}.join
30
+ end
31
+ puts
38
32
  end
39
-
data/lib/pretty_round.rb CHANGED
@@ -1,38 +1,69 @@
1
1
 
2
2
  class Numeric
3
- # Ceiling with given precision.
4
- def roundup(d=0)
5
- x = 10**(-d)
6
- self.quo(x).ceil * x
3
+ # Rounding up with given precision.
4
+ # This method round up to far from 0 direction.
5
+ def roundup(digit=0)
6
+ x = 10**(-digit)
7
+ if self > 0
8
+ quo(x).ceil * x
9
+ else
10
+ quo(x).floor * x
11
+ end
7
12
  end
8
13
 
9
- # Flooring with given precision.
10
- def rounddown(d=0)
11
- x = 10**(-d)
12
- self.quo(x).floor * x
14
+ # Rounding down with given precision.
15
+ # This method round down to near to 0 direction.
16
+ def rounddown(digit=0)
17
+ x = 10**(-digit)
18
+ if self > 0
19
+ quo(x).floor * x
20
+ else
21
+ quo(x).ceil * x
22
+ end
13
23
  end
14
24
 
15
25
 
16
26
  # Return nearest multiple of given number that is equal to or greater than self.
17
27
  # This method round up to the positive infinity direction.
18
- def mroundup(num)
19
- div, mod = divmod(num)
28
+ def mceil(num)
29
+ #
30
+ # MEMO: maybe ruby bug:
31
+ # 123.456.divmod(1/10r) => [123456, 4.996003610813204e-16]
32
+ #
33
+ #div, mod = divmod(num)
34
+ div = self.div(num); mod = self - div*num
20
35
  x = num * div; y = x + (mod.zero? ? 0 : num)
21
36
  [x, y].max
22
37
  end
23
38
 
24
39
  # Return nearest multiple of given number that is equal to or less than self.
25
40
  # This method round down to the negative infinity direction.
26
- def mrounddown(num)
27
- div, mod = divmod(num)
41
+ def mfloor(num)
42
+ div = self.div(num); mod = self - div*num
28
43
  x = num * div; y = x + (mod.zero? ? 0 : num)
29
44
  [x, y].min
30
45
  end
31
46
 
47
+ # Return nearest multiple of given number that the absolute is equal to or greater than self.
48
+ # This method round up to far from 0 direction.
49
+ def mroundup(num)
50
+ div = self.div(num); mod = self - div*num
51
+ x = num * div; y = x + (mod.zero? ? 0 : num)
52
+ [x, y].max_by(&:abs)
53
+ end
54
+
55
+ # Return nearest multiple of given number that the absolute is equal to or less than self.
56
+ # This method round down to near to 0 direction.
57
+ def mrounddown(num)
58
+ div = self.div(num); mod = self - div*num
59
+ x = num * div; y = x + (mod.zero? ? 0 : num)
60
+ [x, y].min_by(&:abs)
61
+ end
62
+
32
63
  # Retuen nearest multiple of given number.
33
64
  # When self is median of multiple of given number, return the multiple that have greater absolute.
34
65
  def mround(num)
35
- div, mod = divmod(num)
66
+ div = self.div(num); mod = self - div*num
36
67
  x = num * div; y = x + (mod.zero? ? 0 : num)
37
68
  if mod.quo(num).abs * 2 == 1
38
69
  [x, y].max_by(&:abs)
@@ -41,9 +72,38 @@ class Numeric
41
72
  end
42
73
  end
43
74
 
44
- # Rounding with given significant digit.
45
- def sround(digit, base=10)
46
- numdigit = Math.log(abs, base).floor + 1
47
- mround(base**(numdigit-digit))
75
+ alias :mtruncate :mrounddown
76
+
77
+
78
+ # Ceiling with given significant digit.
79
+ def sceil(digit)
80
+ numdigit = Math.log10(abs).floor + 1
81
+ mceil(10**(numdigit - digit))
82
+ end
83
+
84
+ # Flooring with given significant digit.
85
+ def sfloor(digit)
86
+ numdigit = Math.log10(abs).floor + 1
87
+ mfloor(10**(numdigit - digit))
88
+ end
89
+
90
+ # Rounding up with given significant digit.
91
+ def sroundup(digit)
92
+ numdigit = Math.log10(abs).floor + 1
93
+ mroundup(10**(numdigit - digit))
94
+ end
95
+
96
+ # Rounding down with given significant digit.
97
+ def srounddown(digit)
98
+ numdigit = Math.log10(abs).floor + 1
99
+ mrounddown(10**(numdigit - digit))
48
100
  end
101
+
102
+ # Rounding off with given significant digit.
103
+ def sround(digit)
104
+ numdigit = Math.log10(abs).floor + 1
105
+ mround(10**(numdigit - digit))
106
+ end
107
+
108
+ alias :struncate :srounddown
49
109
  end
@@ -11,8 +11,8 @@ class TEST_PrettyRound < Minitest::Test
11
11
  def test_roundup
12
12
  assert_ep 2, 1.9.roundup
13
13
  assert_ep 2, 1.1.roundup
14
- assert_ep -1, -1.1.roundup
15
- assert_ep -1, -1.9.roundup
14
+ assert_ep -2, -1.1.roundup
15
+ assert_ep -2, -1.9.roundup
16
16
 
17
17
  x = 123.456
18
18
  assert_ep 124, x.roundup
@@ -20,16 +20,16 @@ class TEST_PrettyRound < Minitest::Test
20
20
  assert_ep 200, x.roundup(-2)
21
21
 
22
22
  x = -123.456
23
- assert_ep -123, x.roundup
24
- assert_ep -123.45, x.roundup(2)
25
- assert_ep -100, x.roundup(-2)
23
+ assert_ep -124, x.roundup
24
+ assert_ep -123.46, x.roundup(2)
25
+ assert_ep -200, x.roundup(-2)
26
26
  end
27
-
27
+
28
28
  def test_rounddown
29
29
  assert_ep 1, 1.9.rounddown
30
30
  assert_ep 1, 1.1.rounddown
31
- assert_ep -2, -1.1.rounddown
32
- assert_ep -2, -1.9.rounddown
31
+ assert_ep -1, -1.1.rounddown
32
+ assert_ep -1, -1.9.rounddown
33
33
 
34
34
  x = 123.456
35
35
  assert_ep 123, x.rounddown
@@ -37,43 +37,77 @@ class TEST_PrettyRound < Minitest::Test
37
37
  assert_ep 100, x.rounddown(-2)
38
38
 
39
39
  x = -123.456
40
- assert_ep -124, x.rounddown
41
- assert_ep -123.46, x.rounddown(2)
42
- assert_ep -200, x.rounddown(-2)
40
+ assert_ep -123, x.rounddown
41
+ assert_ep -123.45, x.rounddown(2)
42
+ assert_ep -100, x.rounddown(-2)
43
+ end
44
+
45
+ def test_mceil
46
+ assert_ep 2, 1.9.mceil(1)
47
+ assert_ep 2, 1.1.mceil(1)
48
+ assert_ep -1, -1.1.mceil(1)
49
+ assert_ep -1, -1.9.mceil(1)
50
+
51
+ x = 123.456
52
+ assert_ep 124, x.mceil(-2)
53
+ assert_ep 123.46, x.mceil(-0.01)
54
+ assert_ep 125, x.mceil(25)
55
+
56
+ x = -123.456
57
+ assert_ep -122, x.mceil(-2)
58
+ assert_ep -123.45, x.mceil(-0.01)
59
+ assert_ep -100, x.mceil(25)
60
+ end
61
+
62
+ def test_mfloor
63
+ assert_ep 1, 1.9.mfloor(1)
64
+ assert_ep 1, 1.1.mfloor(1)
65
+ assert_ep -2, -1.1.mfloor(1)
66
+ assert_ep -2, -1.9.mfloor(1)
67
+
68
+ x = 123.456
69
+ assert_ep 122, x.mfloor(-2)
70
+ assert_ep 123.45, x.mfloor(-0.01)
71
+ assert_ep 100, x.mfloor(25)
72
+
73
+ x = -123.456
74
+ assert_ep -124, x.mfloor(-2)
75
+ assert_ep -123.46, x.mfloor(-0.01)
76
+ assert_ep -125, x.mfloor(25)
43
77
  end
44
78
 
45
79
  def test_mroundup
46
80
  assert_ep 2, 1.9.mroundup(1)
47
81
  assert_ep 2, 1.1.mroundup(1)
48
- assert_ep -1, -1.1.mroundup(1)
49
- assert_ep -1, -1.9.mroundup(1)
82
+ assert_ep -2, -1.1.mroundup(1)
83
+ assert_ep -2, -1.9.mroundup(1)
50
84
 
51
85
  x = 123.456
52
- assert_ep 124, x.mroundup(-1)
86
+ assert_ep 124, x.mroundup(-2)
53
87
  assert_ep 123.46, x.mroundup(-0.01)
54
- assert_ep 200, x.mroundup(100)
88
+ assert_ep 125, x.mroundup(25)
55
89
 
56
90
  x = -123.456
57
- assert_ep -123, x.mroundup(-1)
58
- assert_ep -123.45, x.mroundup(-0.01)
59
- assert_ep -100, x.mroundup(100)
91
+ assert_ep -124, x.mroundup(-2)
92
+ assert_ep -123.46, x.mroundup(-0.01)
93
+ assert_ep -125, x.mroundup(25)
60
94
  end
61
-
95
+
62
96
  def test_mrounddown
63
97
  assert_ep 1, 1.9.mrounddown(1)
64
98
  assert_ep 1, 1.1.mrounddown(1)
65
- assert_ep -2, -1.1.mrounddown(1)
66
- assert_ep -2, -1.9.mrounddown(1)
99
+ assert_ep -1, -1.1.mrounddown(1)
100
+ assert_ep -1, -1.9.mrounddown(1)
67
101
 
68
102
  x = 123.456
69
- assert_ep 123, x.mrounddown(-1)
103
+ assert_ep 122, x.mrounddown(-2)
70
104
  assert_ep 123.45, x.mrounddown(-0.01)
71
- assert_ep 100, x.mrounddown(100)
105
+ assert_ep 100, x.mrounddown(25)
72
106
 
73
107
  x = -123.456
74
- assert_ep -124, x.mrounddown(-1)
75
- assert_ep -123.46, x.mrounddown(-0.01)
76
- assert_ep -200, x.mrounddown(100)
108
+ assert_ep -122, x.mrounddown(-2)
109
+ assert_ep -123.45, x.mrounddown(-0.01)
110
+ assert_ep -100, x.mrounddown(25)
77
111
  end
78
112
 
79
113
  def test_mround
@@ -83,28 +117,85 @@ class TEST_PrettyRound < Minitest::Test
83
117
  assert_ep -2, -1.9.mround(1)
84
118
 
85
119
  x = 123.456
86
- assert_ep 123, x.mround(-1)
120
+ assert_ep 124, x.mround(-2)
87
121
  assert_ep 123.46, x.mround(-0.01)
88
- assert_ep 100, x.mround(100)
122
+ assert_ep 125, x.mround(25)
89
123
 
90
124
  x = -123.456
91
- assert_ep -123, x.mround(-1)
125
+ assert_ep -124, x.mround(-2)
92
126
  assert_ep -123.46, x.mround(-0.01)
93
- assert_ep -100, x.mround(100)
127
+ assert_ep -125, x.mround(25)
94
128
  end
95
129
 
130
+
131
+ def test_sceil
132
+ x = 123.456
133
+ assert_ep 200, x.sceil(1)
134
+ assert_ep 124, x.sceil(3)
135
+ assert_ep 123.5, x.sceil(4)
136
+ assert_ep 123.456, x.sceil(6)
137
+
138
+ x = -123.456
139
+ assert_ep -100, x.sceil(1)
140
+ assert_ep -123, x.sceil(3)
141
+ assert_ep -123.4, x.sceil(4)
142
+ assert_ep -123.456, x.sceil(6)
143
+ end
144
+
145
+ def test_sfloor
146
+ x = 123.456
147
+ assert_ep 100, x.sfloor(1)
148
+ assert_ep 123, x.sfloor(3)
149
+ assert_ep 123.4, x.sfloor(4)
150
+ assert_ep 123.456, x.sfloor(6)
151
+
152
+ x = -123.456
153
+ assert_ep -200, x.sfloor(1)
154
+ assert_ep -124, x.sfloor(3)
155
+ assert_ep -123.5, x.sfloor(4)
156
+ assert_ep -123.456, x.sfloor(6)
157
+ end
158
+
159
+ def test_sroundup
160
+ x = 123.456
161
+ assert_ep 200, x.sroundup(1)
162
+ assert_ep 124, x.sroundup(3)
163
+ assert_ep 123.5, x.sroundup(4)
164
+ assert_ep 123.456, x.sroundup(6)
165
+
166
+ x = -123.456
167
+ assert_ep -200, x.sroundup(1)
168
+ assert_ep -124, x.sroundup(3)
169
+ assert_ep -123.5, x.sroundup(4)
170
+ assert_ep -123.456, x.sroundup(6)
171
+ end
172
+
173
+ def test_srounddown
174
+ x = 123.456
175
+ assert_ep 0, x.srounddown(-1)
176
+ assert_ep 0, x.srounddown(0)
177
+ assert_ep 100, x.srounddown(1)
178
+ assert_ep 123, x.srounddown(3)
179
+ assert_ep 123.4, x.srounddown(4)
180
+ assert_ep 123.456, x.srounddown(6)
181
+
182
+ x = -123.456
183
+ assert_ep 0, x.srounddown(-1)
184
+ assert_ep 0, x.srounddown(0)
185
+ assert_ep -100, x.srounddown(1)
186
+ assert_ep -123, x.srounddown(3)
187
+ assert_ep -123.4, x.srounddown(4)
188
+ assert_ep -123.456, x.srounddown(6)
189
+ end
190
+
96
191
  def test_sround
97
192
  x = 123.456
98
- assert_ep 0, x.sround(-1)
99
- assert_ep 0, x.sround(0)
100
193
  assert_ep 100, x.sround(1)
101
194
  assert_ep 123, x.sround(3)
102
195
  assert_ep 123.5, x.sround(4)
103
196
  assert_ep 123.456, x.sround(6)
104
197
 
105
198
  x = -123.456
106
- assert_ep 0, x.sround(-1)
107
- assert_ep 0, x.sround(0)
108
199
  assert_ep -100, x.sround(1)
109
200
  assert_ep -123, x.sround(3)
110
201
  assert_ep -123.5, x.sround(4)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pretty_round
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - diaphragm
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-21 00:00:00.000000000 Z
11
+ date: 2015-10-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This gem provide useful numerical rounding methods.
14
14
  email:
@@ -41,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
41
  version: '0'
42
42
  requirements: []
43
43
  rubyforge_project:
44
- rubygems_version: 2.4.5
44
+ rubygems_version: 2.4.8
45
45
  signing_key:
46
46
  specification_version: 4
47
47
  summary: pretty rounding methods library