pretty_round 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d43638c5621ed6510153ba224064c8e72a20265d
4
- data.tar.gz: e0a4bf7bc31b3abde7ec213e0391d8f88f94b243
2
+ SHA256:
3
+ metadata.gz: 7686e8cacf564521de57d2e567b3621bae97da5bad44581ea6741d004b9d49f3
4
+ data.tar.gz: '0018d0c5354ddc58f816675d3c40dfb5c43e276db6d831413a6e6214762409ef'
5
5
  SHA512:
6
- metadata.gz: e6abde717b245bdc2eba773eb7cc7fa09f221dbc09803fe6ee96376f812eb7fea0de0e3521a654e32f4308171f879ba7f926b2baa68f358ce83b37e5098f5834
7
- data.tar.gz: 2d607ad3107cc7890bedac69a1e96c7d8d774c3b99679b64646a8855986d0ed7f21fb6ef4f51cc2e154575f2920d6b9a86f70283b6c7863003b8fc69f3661715
6
+ metadata.gz: 9bbd6edc1553103440c5efb95b047024d7fa0ddebd30a837d7d5bf346cad72f101cf722baf5c593bd50081aaea046c009690a5a83ae075cf95a3bf4a85c91b2b
7
+ data.tar.gz: 35f4e8496f1733bae06955c74c6ae1422d48fc3e9d2ee85797734d73ad0847159736eae135ec7fb024e941be786bb6325b4a45677f683a4411fc20a8cc5446a0
data/LICENSE CHANGED
@@ -1,22 +1,22 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2015 diaphragm
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
22
-
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 diaphragm
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md CHANGED
@@ -1,89 +1,98 @@
1
- ruby-pretty-round
2
- ================
3
- This gem add useful numerical rounding methods to `Numeric` class as shown below.
4
- - round to nearest multiple
5
- - round with significant digits
6
-
7
- Install
8
- ================
9
- `$ gem install pretty_round`
10
-
11
- Usage
12
- ================
13
- ```rb
14
- require "pretty_round"
15
-
16
- x = 123.456
17
-
18
- # round with precision
19
- x.roundup #=> 124
20
- x.rounddown(2) #=> 123.45
21
-
22
- # round to nearest multiple
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
-
29
- # round with significant digit
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
35
- ```
36
-
37
- Rounding direction
38
- ================
39
- - `#*ceil` / `#*floor`: These methods round to the positive / negative infinity direction. Its behavior is same as built-in `#ceil`, `#floor`.
40
- - `#*roundup` / `#*rounddown`: These methods round to far from / near to 0 direction.
41
- - `#*round`: These methods round off the absolute value, does not round even. Its behavior is same as built-in `#round`.
42
- - `#*truncate`: These methods are alias of `#*rounddown`.
43
-
44
- Result table is shown below, including ruby built-in rounding methods for comparison.
45
- An axis of abscissas refers to the receiver and an axis of ordinates refers to the methods.
46
- ```
47
- | 1.9 1.1 -1.1 -1.9
48
- -----------------------------------
49
- .ceil | 2 2 -1 -1 (ruby built-in)
50
- .floor | 1 1 -2 -2 (ruby built-in)
51
- .roundup | 2 2 -2 -2
52
- .rounddown | 1 1 -1 -1
53
- .round | 2 1 -1 -2 (ruby built-in)
54
- .truncate | 1 1 -1 -1 (ruby built-in)
55
- .mceil(1) | 2 2 -1 -1
56
- .mfloor(1) | 1 1 -2 -2
57
- .mroundup(1) | 2 2 -2 -2
58
- .mrounddown(1)| 1 1 -1 -1
59
- .mround(1) | 2 1 -1 -2
60
- .mtruncate(1) | 1 1 -1 -1
61
- .sceil(1) | 2 2 -1 -1
62
- .sfloor(1) | 1 1 -2 -2
63
- .sroundup(1) | 2 2 -2 -2
64
- .srounddown(1)| 1 1 -1 -1
65
- .sround(1) | 2 1 -1 -2
66
- .struncate(1) | 1 1 -1 -1
67
- ```
68
-
69
- Also, try to run `examples/example.rb`
70
-
71
-
72
- Known Bugs
73
- ================
74
- Float presision
75
- ----------------
76
- ```rb
77
- 1.2.mrounddown(0.1) #=> 1.1
78
- ```
79
- Oops... Resulting 1.2 is expected.
80
-
81
- Becouse,
82
- ```rb
83
- 1.2.divmod(0.1) #=> [11, 0.0999999999999999]
84
- ```
85
-
86
- To avoid this bug, please use `Rational`.
87
- ```rb
88
- 1.2r.mrounddown(0.1r) #=> (6/5)
89
- ```
1
+ ruby-pretty-round
2
+ ================
3
+ This gem add useful numerical rounding methods to `Numeric` class as shown below.
4
+ - round to nearest multiple
5
+ - round with significant digits
6
+
7
+ Install
8
+ ================
9
+ `$ gem install pretty_round`
10
+
11
+ Usage
12
+ ================
13
+ ```rb
14
+ require "pretty_round"
15
+
16
+ x = 123.456
17
+
18
+ # round with precision
19
+ x.roundup #=> 124
20
+ x.rounddown(2) #=> 123.45
21
+
22
+ # round to nearest multiple
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
+
29
+ # round with significant digit
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
35
+ ```
36
+
37
+ Refinements
38
+ ----------------
39
+ When `require 'pretty_round'`, rounding methods are included in **global scope**.
40
+ To include in limited scope, please load `pretty_round/core` and use refinements.
41
+ ```rb
42
+ require 'pretty_round/core'
43
+ using PrettyRound
44
+ ```
45
+
46
+ Rounding direction
47
+ ================
48
+ - `#*ceil` / `#*floor`: These methods round to the positive / negative infinity direction. Its behavior is same as built-in `#ceil`, `#floor`.
49
+ - `#*roundup` / `#*rounddown`: These methods round to far from / near to 0 direction.
50
+ - `#*round`: These methods round off the absolute value, does not round even. Its behavior is same as built-in `#round`.
51
+ - `#*truncate`: These methods are alias of `#*rounddown`.
52
+
53
+ Result table is shown below, including ruby built-in rounding methods for comparison.
54
+ An axis of abscissas refers to the receiver and an axis of ordinates refers to the methods.
55
+ ```
56
+ | 1.9 1.1 -1.1 -1.9
57
+ -----------------------------------
58
+ .ceil | 2 2 -1 -1 (ruby built-in)
59
+ .floor | 1 1 -2 -2 (ruby built-in)
60
+ .roundup | 2 2 -2 -2
61
+ .rounddown | 1 1 -1 -1
62
+ .round | 2 1 -1 -2 (ruby built-in)
63
+ .truncate | 1 1 -1 -1 (ruby built-in)
64
+ .mceil(1) | 2 2 -1 -1
65
+ .mfloor(1) | 1 1 -2 -2
66
+ .mroundup(1) | 2 2 -2 -2
67
+ .mrounddown(1)| 1 1 -1 -1
68
+ .mround(1) | 2 1 -1 -2
69
+ .mtruncate(1) | 1 1 -1 -1
70
+ .sceil(1) | 2 2 -1 -1
71
+ .sfloor(1) | 1 1 -2 -2
72
+ .sroundup(1) | 2 2 -2 -2
73
+ .srounddown(1)| 1 1 -1 -1
74
+ .sround(1) | 2 1 -1 -2
75
+ .struncate(1) | 1 1 -1 -1
76
+ ```
77
+
78
+ Also, try to run `examples/example.rb`
79
+
80
+
81
+ Known Bugs
82
+ ================
83
+ Float presision
84
+ ----------------
85
+ ```rb
86
+ 1.2.mrounddown(0.1) #=> 1.1
87
+ ```
88
+ Oops... Resulting 1.2 is expected.
89
+
90
+ Becouse,
91
+ ```rb
92
+ 1.2.divmod(0.1) #=> [11, 0.0999999999999999]
93
+ ```
94
+
95
+ To avoid this bug, please use `Rational`.
96
+ ```rb
97
+ 1.2r.mrounddown(0.1r) #=> (6/5)
98
+ ```
data/examples/example.rb CHANGED
@@ -1,32 +1,32 @@
1
-
2
- require "pretty_round"
3
-
4
- puts "* Rounding to nearerst multiple"
5
- xs = %w[2.9 2.5 2.1 -2.1 -2.5 -2.9].map(&:to_r)
6
- ns = %w[0.1 0.2 1 2 -0.1 -0.2 -1 -2].map(&:to_r)
7
-
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
16
- end
17
- puts
18
-
19
-
20
- puts "* Rounding with significant digit"
21
- xs = [0.12345, 123.45, 12345, -0.12345, -123.45, -12345]
22
- ns = [*1..5]
23
-
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
32
- end
1
+
2
+ require "pretty_round"
3
+
4
+ puts "* Rounding to nearerst multiple"
5
+ xs = %w[2.9 2.5 2.1 -2.1 -2.5 -2.9].map(&:to_r)
6
+ ns = %w[0.1 0.2 1 2 -0.1 -0.2 -1 -2].map(&:to_r)
7
+
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
16
+ end
17
+ puts
18
+
19
+
20
+ puts "* Rounding with significant digit"
21
+ xs = [0.12345, 123.45, 12345, -0.12345, -123.45, -12345]
22
+ ns = [*1..5]
23
+
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
32
+ end
data/examples/example2.rb CHANGED
@@ -1,19 +1,19 @@
1
-
2
- require "pretty_round"
3
-
4
- xs = [1.9, 1.1, -1.1, -1.9]
5
-
6
- puts " "*14 + "|" + xs.map{|x| x.to_s.rjust(5)}.join
7
- puts "-"*(14+1+xs.size*5)
8
-
9
- %i[ceil floor roundup rounddown round truncate].each do |mthd|
10
- puts ".#{mthd}".ljust(14)+ "|" + xs.map{|x| x.send(mthd)}.map{|x| x.to_i.to_s.rjust(5)}.join
11
- end
12
-
13
- %i[mceil mfloor mroundup mrounddown mround mtruncate].each do |mthd|
14
- puts ".#{mthd}(1)".ljust(14)+ "|" + xs.map{|x| x.send(mthd,1)}.map{|x| x.to_i.to_s.rjust(5)}.join
15
- end
16
-
17
- %i[sceil sfloor sroundup srounddown sround struncate].each do |mthd|
18
- puts ".#{mthd}(1)".ljust(14)+ "|" + xs.map{|x| x.send(mthd,1)}.map{|x| x.to_i.to_s.rjust(5)}.join
19
- end
1
+
2
+ require "pretty_round"
3
+
4
+ xs = [1.9, 1.1, -1.1, -1.9]
5
+
6
+ puts " "*14 + "|" + xs.map{|x| x.to_s.rjust(5)}.join
7
+ puts "-"*(14+1+xs.size*5)
8
+
9
+ %i[ceil floor roundup rounddown round truncate].each do |mthd|
10
+ puts ".#{mthd}".ljust(14)+ "|" + xs.map{|x| x.send(mthd)}.map{|x| x.to_i.to_s.rjust(5)}.join
11
+ end
12
+
13
+ %i[mceil mfloor mroundup mrounddown mround mtruncate].each do |mthd|
14
+ puts ".#{mthd}(1)".ljust(14)+ "|" + xs.map{|x| x.send(mthd,1)}.map{|x| x.to_i.to_s.rjust(5)}.join
15
+ end
16
+
17
+ %i[sceil sfloor sroundup srounddown sround struncate].each do |mthd|
18
+ puts ".#{mthd}(1)".ljust(14)+ "|" + xs.map{|x| x.send(mthd,1)}.map{|x| x.to_i.to_s.rjust(5)}.join
19
+ end
@@ -0,0 +1,113 @@
1
+
2
+ module PrettyRound
3
+ # Rounding up with given precision.
4
+ # This method round up to far from 0 direction.
5
+ def roundup(digit=0)
6
+ abs.ceil(digit) * (positive? ? 1 : -1)
7
+ end
8
+
9
+ # Rounding down with given precision.
10
+ # This method round down to near to 0 direction.
11
+ def rounddown(digit=0)
12
+ abs.floor(digit) * (positive? ? 1 : -1)
13
+ end
14
+
15
+
16
+ # Return nearest multiple of given number that is equal to or greater than self.
17
+ # This method round up to the positive infinity direction.
18
+ def mceil(num)
19
+ if (x = num * div(num)) == self
20
+ self
21
+ else
22
+ [x, x+num].max
23
+ end
24
+ end
25
+
26
+ # Return nearest multiple of given number that is equal to or less than self.
27
+ # This method round down to the negative infinity direction.
28
+ def mfloor(num)
29
+ if (x = num * div(num)) == self
30
+ self
31
+ else
32
+ [x, x+num].min
33
+ end
34
+ end
35
+
36
+ # Return nearest multiple of given number that the absolute is equal to or greater than self.
37
+ # This method round up to far from 0 direction.
38
+ def mroundup(num)
39
+ if (x = num * div(num)) == self
40
+ self
41
+ else
42
+ [x, x+num].max_by(&:abs)
43
+ end
44
+ end
45
+
46
+ # Return nearest multiple of given number that the absolute is equal to or less than self.
47
+ # This method round down to near to 0 direction.
48
+ def mrounddown(num)
49
+ if (x = num * div(num)) == self
50
+ self
51
+ else
52
+ [x, x+num].min_by(&:abs)
53
+ end
54
+ end
55
+
56
+ # Retuen nearest multiple of given number.
57
+ # When self is median of multiple of given number, return the multiple that have greater absolute.
58
+ def mround(num)
59
+ if (x = num * div(num)) == self
60
+ self
61
+ elsif x + x+num == self + self # if self is median
62
+ [x, x+num].max_by(&:abs)
63
+ else
64
+ [x, x+num].min_by{|t| (t - self).abs}
65
+ end
66
+ end
67
+
68
+ alias_method :mtruncate, :mrounddown
69
+
70
+
71
+ # Ceiling with given significant digit.
72
+ def sceil(digit)
73
+ return self if zero?
74
+ selfdigit = Math.log10(abs).floor + 1
75
+ ceil(digit - selfdigit)
76
+ end
77
+
78
+ # Flooring with given significant digit.
79
+ def sfloor(digit)
80
+ return self if zero?
81
+ selfdigit = Math.log10(abs).floor + 1
82
+ floor(digit - selfdigit)
83
+ end
84
+
85
+ # Rounding up with given significant digit.
86
+ def sroundup(digit)
87
+ return self if zero?
88
+ selfdigit = Math.log10(abs).floor + 1
89
+ abs.ceil(digit - selfdigit) * (positive? ? 1 : -1)
90
+ end
91
+
92
+ # Rounding down with given significant digit.
93
+ def srounddown(digit)
94
+ return self if zero?
95
+ selfdigit = Math.log10(abs).floor + 1
96
+ abs.floor(digit - selfdigit) * (positive? ? 1 : -1)
97
+ end
98
+
99
+ # Rounding off with given significant digit.
100
+ def sround(digit)
101
+ return self if zero?
102
+ selfdigit = Math.log10(abs).floor + 1
103
+ round(digit - selfdigit)
104
+ end
105
+
106
+ alias_method :struncate, :srounddown
107
+ end
108
+
109
+ module PrettyRound
110
+ refine Numeric do
111
+ include(PrettyRound)
112
+ end
113
+ end
data/lib/pretty_round.rb CHANGED
@@ -1,112 +1,3 @@
1
-
2
- class Numeric
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
12
- end
13
-
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
23
- end
24
-
25
-
26
- # Return nearest multiple of given number that is equal to or greater than self.
27
- # This method round up to the positive infinity direction.
28
- def mceil(num)
29
- if (x = num * div(num)) == self
30
- self
31
- else
32
- [x, x+num].max
33
- end
34
- end
35
-
36
- # Return nearest multiple of given number that is equal to or less than self.
37
- # This method round down to the negative infinity direction.
38
- def mfloor(num)
39
- if (x = num * div(num)) == self
40
- self
41
- else
42
- [x, x+num].min
43
- end
44
- end
45
-
46
- # Return nearest multiple of given number that the absolute is equal to or greater than self.
47
- # This method round up to far from 0 direction.
48
- def mroundup(num)
49
- if (x = num * div(num)) == self
50
- self
51
- else
52
- [x, x+num].max_by(&:abs)
53
- end
54
- end
55
-
56
- # Return nearest multiple of given number that the absolute is equal to or less than self.
57
- # This method round down to near to 0 direction.
58
- def mrounddown(num)
59
- if (x = num * div(num)) == self
60
- self
61
- else
62
- [x, x+num].min_by(&:abs)
63
- end
64
- end
65
-
66
- # Retuen nearest multiple of given number.
67
- # When self is median of multiple of given number, return the multiple that have greater absolute.
68
- def mround(num)
69
- if (x = num * div(num)) == self
70
- self
71
- elsif x + x +num == self + self # if self is median
72
- [x, x+num].max_by(&:abs)
73
- else
74
- [x, x+num].min_by{|t| (t - self).abs}
75
- end
76
- end
77
-
78
- alias :mtruncate :mrounddown
79
-
80
-
81
- # Ceiling with given significant digit.
82
- def sceil(digit)
83
- selfdigit = Math.log10(abs).floor + 1
84
- mceil(10**(selfdigit - digit))
85
- end
86
-
87
- # Flooring with given significant digit.
88
- def sfloor(digit)
89
- selfdigit = Math.log10(abs).floor + 1
90
- mfloor(10**(selfdigit - digit))
91
- end
92
-
93
- # Rounding up with given significant digit.
94
- def sroundup(digit)
95
- selfdigit = Math.log10(abs).floor + 1
96
- mroundup(10**(selfdigit - digit))
97
- end
98
-
99
- # Rounding down with given significant digit.
100
- def srounddown(digit)
101
- selfdigit = Math.log10(abs).floor + 1
102
- mrounddown(10**(selfdigit - digit))
103
- end
104
-
105
- # Rounding off with given significant digit.
106
- def sround(digit)
107
- selfdigit = Math.log10(abs).floor + 1
108
- mround(10**(selfdigit - digit))
109
- end
110
-
111
- alias :struncate :srounddown
112
- end
1
+
2
+ require_relative './pretty_round/core'
3
+ Numeric.include(PrettyRound)
@@ -1,251 +1,273 @@
1
- require 'minitest'
2
- require 'pretty_round'
3
-
4
- Minitest.autorun
5
-
6
- class TEST_PrettyRound < Minitest::Test
7
- def assert_ep(expect, target)
8
- assert_in_epsilon(expect, target, Float::EPSILON)
9
- end
10
-
11
- def test_roundup
12
- assert_ep 2, 1.9.roundup
13
- assert_ep 2, 1.5.roundup
14
- assert_ep 2, 1.1.roundup
15
- assert_ep -2, -1.1.roundup
16
- assert_ep -2, -1.5.roundup
17
- assert_ep -2, -1.9.roundup
18
-
19
- x = 123.456
20
- assert_ep 124, x.roundup
21
- assert_ep 123.46, x.roundup(2)
22
- assert_ep 200, x.roundup(-2)
23
-
24
- x = -123.456
25
- assert_ep -124, x.roundup
26
- assert_ep -123.46, x.roundup(2)
27
- assert_ep -200, x.roundup(-2)
28
- end
29
-
30
- def test_rounddown
31
- assert_ep 1, 1.9.rounddown
32
- assert_ep 1, 1.5.rounddown
33
- assert_ep 1, 1.1.rounddown
34
- assert_ep -1, -1.1.rounddown
35
- assert_ep -1, -1.5.rounddown
36
- assert_ep -1, -1.9.rounddown
37
-
38
- x = 123.456
39
- assert_ep 123, x.rounddown
40
- assert_ep 123.45, x.rounddown(2)
41
- assert_ep 100, x.rounddown(-2)
42
-
43
- x = -123.456
44
- assert_ep -123, x.rounddown
45
- assert_ep -123.45, x.rounddown(2)
46
- assert_ep -100, x.rounddown(-2)
47
- end
48
-
49
-
50
- def test_mceil
51
- assert_ep 2, 1.9.mceil(1)
52
- assert_ep 2, 1.5.mceil(1)
53
- assert_ep 2, 1.1.mceil(1)
54
- assert_ep -1, -1.1.mceil(1)
55
- assert_ep -1, -1.5.mceil(1)
56
- assert_ep -1, -1.9.mceil(1)
57
-
58
- x = 123.456
59
- assert_ep 124, x.mceil(-2)
60
- assert_ep 123.46, x.mceil(-0.01)
61
- assert_ep 125, x.mceil(25)
62
-
63
- x = -123.456
64
- assert_ep -122, x.mceil(-2)
65
- assert_ep -123.45, x.mceil(-0.01)
66
- assert_ep -100, x.mceil(25)
67
- end
68
-
69
- def test_mfloor
70
- assert_ep 1, 1.9.mfloor(1)
71
- assert_ep 1, 1.5.mfloor(1)
72
- assert_ep 1, 1.1.mfloor(1)
73
- assert_ep -2, -1.1.mfloor(1)
74
- assert_ep -2, -1.5.mfloor(1)
75
- assert_ep -2, -1.9.mfloor(1)
76
-
77
- x = 123.456
78
- assert_ep 122, x.mfloor(-2)
79
- assert_ep 123.45, x.mfloor(-0.01)
80
- assert_ep 100, x.mfloor(25)
81
-
82
- x = -123.456
83
- assert_ep -124, x.mfloor(-2)
84
- assert_ep -123.46, x.mfloor(-0.01)
85
- assert_ep -125, x.mfloor(25)
86
- end
87
-
88
- def test_mroundup
89
- assert_ep 2, 1.9.mroundup(1)
90
- assert_ep 2, 1.5.mroundup(1)
91
- assert_ep 2, 1.1.mroundup(1)
92
- assert_ep -2, -1.1.mroundup(1)
93
- assert_ep -2, -1.5.mroundup(1)
94
- assert_ep -2, -1.9.mroundup(1)
95
-
96
- x = 123.456
97
- assert_ep 124, x.mroundup(-2)
98
- assert_ep 123.46, x.mroundup(-0.01)
99
- assert_ep 125, x.mroundup(25)
100
-
101
- x = -123.456
102
- assert_ep -124, x.mroundup(-2)
103
- assert_ep -123.46, x.mroundup(-0.01)
104
- assert_ep -125, x.mroundup(25)
105
- end
106
-
107
- def test_mrounddown
108
- assert_ep 1, 1.9.mrounddown(1)
109
- assert_ep 1, 1.5.mrounddown(1)
110
- assert_ep 1, 1.1.mrounddown(1)
111
- assert_ep -1, -1.1.mrounddown(1)
112
- assert_ep -1, -1.5.mrounddown(1)
113
- assert_ep -1, -1.9.mrounddown(1)
114
-
115
- x = 123.456
116
- assert_ep 122, x.mrounddown(-2)
117
- assert_ep 123.45, x.mrounddown(-0.01)
118
- assert_ep 100, x.mrounddown(25)
119
-
120
- x = -123.456
121
- assert_ep -122, x.mrounddown(-2)
122
- assert_ep -123.45, x.mrounddown(-0.01)
123
- assert_ep -100, x.mrounddown(25)
124
- end
125
-
126
- def test_mround
127
- assert_ep 2, 1.9.mround(1)
128
- assert_ep 2, 1.5.mround(1)
129
- assert_ep 1, 1.1.mround(1)
130
- assert_ep -1, -1.1.mround(1)
131
- assert_ep -2, -1.5.mround(1)
132
- assert_ep -2, -1.9.mround(1)
133
-
134
- x = 123.456
135
- assert_ep 124, x.mround(-2)
136
- assert_ep 123.46, x.mround(-0.01)
137
- assert_ep 125, x.mround(25)
138
-
139
- x = -123.456
140
- assert_ep -124, x.mround(-2)
141
- assert_ep -123.46, x.mround(-0.01)
142
- assert_ep -125, x.mround(25)
143
- end
144
-
145
- def test_mtruncate
146
- assert_ep 1, 1.9.mtruncate(1)
147
- assert_ep 1, 1.5.mtruncate(1)
148
- assert_ep 1, 1.1.mtruncate(1)
149
- assert_ep -1, -1.1.mtruncate(1)
150
- assert_ep -1, -1.5.mtruncate(1)
151
- assert_ep -1, -1.9.mtruncate(1)
152
-
153
- x = 123.456
154
- assert_ep 122, x.mtruncate(-2)
155
- assert_ep 123.45, x.mtruncate(-0.01)
156
- assert_ep 100, x.mtruncate(25)
157
-
158
- x = -123.456
159
- assert_ep -122, x.mtruncate(-2)
160
- assert_ep -123.45, x.mtruncate(-0.01)
161
- assert_ep -100, x.mtruncate(25)
162
- end
163
-
164
-
165
- def test_sceil
166
- x = 123.456
167
- assert_ep 200, x.sceil(1)
168
- assert_ep 124, x.sceil(3)
169
- assert_ep 123.5, x.sceil(4)
170
- assert_ep 123.456, x.sceil(6)
171
-
172
- x = -123.456
173
- assert_ep -100, x.sceil(1)
174
- assert_ep -123, x.sceil(3)
175
- assert_ep -123.4, x.sceil(4)
176
- assert_ep -123.456, x.sceil(6)
177
- end
178
-
179
- def test_sfloor
180
- x = 123.456
181
- assert_ep 100, x.sfloor(1)
182
- assert_ep 123, x.sfloor(3)
183
- assert_ep 123.4, x.sfloor(4)
184
- assert_ep 123.456, x.sfloor(6)
185
-
186
- x = -123.456
187
- assert_ep -200, x.sfloor(1)
188
- assert_ep -124, x.sfloor(3)
189
- assert_ep -123.5, x.sfloor(4)
190
- assert_ep -123.456, x.sfloor(6)
191
- end
192
-
193
- def test_sroundup
194
- x = 123.456
195
- assert_ep 200, x.sroundup(1)
196
- assert_ep 124, x.sroundup(3)
197
- assert_ep 123.5, x.sroundup(4)
198
- assert_ep 123.456, x.sroundup(6)
199
-
200
- x = -123.456
201
- assert_ep -200, x.sroundup(1)
202
- assert_ep -124, x.sroundup(3)
203
- assert_ep -123.5, x.sroundup(4)
204
- assert_ep -123.456, x.sroundup(6)
205
- end
206
-
207
- def test_srounddown
208
- x = 123.456
209
- assert_ep 100, x.srounddown(1)
210
- assert_ep 123, x.srounddown(3)
211
- assert_ep 123.4, x.srounddown(4)
212
- assert_ep 123.456, x.srounddown(6)
213
-
214
- x = -123.456
215
- assert_ep -100, x.srounddown(1)
216
- assert_ep -123, x.srounddown(3)
217
- assert_ep -123.4, x.srounddown(4)
218
- assert_ep -123.456, x.srounddown(6)
219
- end
220
-
221
- def test_sround
222
- assert_ep 2, 1.5.sround(1)
223
- assert_ep -2, -1.5.sround(1)
224
-
225
- x = 123.456
226
- assert_ep 100, x.sround(1)
227
- assert_ep 123, x.sround(3)
228
- assert_ep 123.5, x.sround(4)
229
- assert_ep 123.456, x.sround(6)
230
-
231
- x = -123.456
232
- assert_ep -100, x.sround(1)
233
- assert_ep -123, x.sround(3)
234
- assert_ep -123.5, x.sround(4)
235
- assert_ep -123.456, x.sround(6)
236
- end
237
-
238
- def test_struncate
239
- x = 123.456
240
- assert_ep 100, x.struncate(1)
241
- assert_ep 123, x.struncate(3)
242
- assert_ep 123.4, x.struncate(4)
243
- assert_ep 123.456, x.struncate(6)
244
-
245
- x = -123.456
246
- assert_ep -100, x.struncate(1)
247
- assert_ep -123, x.struncate(3)
248
- assert_ep -123.4, x.struncate(4)
249
- assert_ep -123.456, x.struncate(6)
250
- end
251
- end
1
+ require 'minitest'
2
+ # require_relative '../lib/pretty_round'
3
+ require_relative '../lib/pretty_round/core'
4
+ using PrettyRound
5
+
6
+ Minitest.autorun
7
+
8
+ class TEST_PrettyRound < Minitest::Test
9
+ def assert_ep(expect, target)
10
+ assert_in_epsilon(expect, target, Float::EPSILON)
11
+ end
12
+
13
+ def test_roundup
14
+ assert_ep 2, 1.9.roundup
15
+ assert_ep 2, 1.5.roundup
16
+ assert_ep 2, 1.1.roundup
17
+ assert_ep -2, -1.1.roundup
18
+ assert_ep -2, -1.5.roundup
19
+ assert_ep -2, -1.9.roundup
20
+
21
+ x = 123.456
22
+ assert_ep 124, x.roundup
23
+ assert_ep 123.46, x.roundup(2)
24
+ assert_ep 200, x.roundup(-2)
25
+
26
+ x = -123.456
27
+ assert_ep -124, x.roundup
28
+ assert_ep -123.46, x.roundup(2)
29
+ assert_ep -200, x.roundup(-2)
30
+ end
31
+
32
+ def test_rounddown
33
+ assert_ep 1, 1.9.rounddown
34
+ assert_ep 1, 1.5.rounddown
35
+ assert_ep 1, 1.1.rounddown
36
+ assert_ep -1, -1.1.rounddown
37
+ assert_ep -1, -1.5.rounddown
38
+ assert_ep -1, -1.9.rounddown
39
+
40
+ x = 123.456
41
+ assert_ep 123, x.rounddown
42
+ assert_ep 123.45, x.rounddown(2)
43
+ assert_ep 100, x.rounddown(-2)
44
+
45
+ x = -123.456
46
+ assert_ep -123, x.rounddown
47
+ assert_ep -123.45, x.rounddown(2)
48
+ assert_ep -100, x.rounddown(-2)
49
+ end
50
+
51
+
52
+ def test_mceil
53
+ assert_ep 2, 1.9.mceil(1)
54
+ assert_ep 2, 1.5.mceil(1)
55
+ assert_ep 2, 1.1.mceil(1)
56
+ assert_ep -1, -1.1.mceil(1)
57
+ assert_ep -1, -1.5.mceil(1)
58
+ assert_ep -1, -1.9.mceil(1)
59
+
60
+ x = 123.456
61
+ assert_ep 124, x.mceil(-2)
62
+ assert_ep 123.46, x.mceil(-0.01)
63
+ assert_ep 125, x.mceil(25)
64
+
65
+ x = -123.456
66
+ assert_ep -122, x.mceil(-2)
67
+ assert_ep -123.45, x.mceil(-0.01)
68
+ assert_ep -100, x.mceil(25)
69
+ end
70
+
71
+ def test_mfloor
72
+ assert_ep 1, 1.9.mfloor(1)
73
+ assert_ep 1, 1.5.mfloor(1)
74
+ assert_ep 1, 1.1.mfloor(1)
75
+ assert_ep -2, -1.1.mfloor(1)
76
+ assert_ep -2, -1.5.mfloor(1)
77
+ assert_ep -2, -1.9.mfloor(1)
78
+
79
+ x = 123.456
80
+ assert_ep 122, x.mfloor(-2)
81
+ assert_ep 123.45, x.mfloor(-0.01)
82
+ assert_ep 100, x.mfloor(25)
83
+
84
+ x = -123.456
85
+ assert_ep -124, x.mfloor(-2)
86
+ assert_ep -123.46, x.mfloor(-0.01)
87
+ assert_ep -125, x.mfloor(25)
88
+ end
89
+
90
+ def test_mroundup
91
+ assert_ep 2, 1.9.mroundup(1)
92
+ assert_ep 2, 1.5.mroundup(1)
93
+ assert_ep 2, 1.1.mroundup(1)
94
+ assert_ep -2, -1.1.mroundup(1)
95
+ assert_ep -2, -1.5.mroundup(1)
96
+ assert_ep -2, -1.9.mroundup(1)
97
+
98
+ x = 123.456
99
+ assert_ep 124, x.mroundup(-2)
100
+ assert_ep 123.46, x.mroundup(-0.01)
101
+ assert_ep 125, x.mroundup(25)
102
+
103
+ x = -123.456
104
+ assert_ep -124, x.mroundup(-2)
105
+ assert_ep -123.46, x.mroundup(-0.01)
106
+ assert_ep -125, x.mroundup(25)
107
+ end
108
+
109
+ def test_mrounddown
110
+ assert_ep 1, 1.9.mrounddown(1)
111
+ assert_ep 1, 1.5.mrounddown(1)
112
+ assert_ep 1, 1.1.mrounddown(1)
113
+ assert_ep -1, -1.1.mrounddown(1)
114
+ assert_ep -1, -1.5.mrounddown(1)
115
+ assert_ep -1, -1.9.mrounddown(1)
116
+
117
+ x = 123.456
118
+ assert_ep 122, x.mrounddown(-2)
119
+ assert_ep 123.45, x.mrounddown(-0.01)
120
+ assert_ep 100, x.mrounddown(25)
121
+
122
+ x = -123.456
123
+ assert_ep -122, x.mrounddown(-2)
124
+ assert_ep -123.45, x.mrounddown(-0.01)
125
+ assert_ep -100, x.mrounddown(25)
126
+ end
127
+
128
+ def test_mround
129
+ assert_ep 2, 1.9.mround(1)
130
+ assert_ep 2, 1.5.mround(1)
131
+ assert_ep 1, 1.1.mround(1)
132
+ assert_ep -1, -1.1.mround(1)
133
+ assert_ep -2, -1.5.mround(1)
134
+ assert_ep -2, -1.9.mround(1)
135
+
136
+ x = 123.456
137
+ assert_ep 124, x.mround(-2)
138
+ assert_ep 123.46, x.mround(-0.01)
139
+ assert_ep 125, x.mround(25)
140
+
141
+ x = -123.456
142
+ assert_ep -124, x.mround(-2)
143
+ assert_ep -123.46, x.mround(-0.01)
144
+ assert_ep -125, x.mround(25)
145
+ end
146
+
147
+ def test_mtruncate
148
+ assert_ep 1, 1.9.mtruncate(1)
149
+ assert_ep 1, 1.5.mtruncate(1)
150
+ assert_ep 1, 1.1.mtruncate(1)
151
+ assert_ep -1, -1.1.mtruncate(1)
152
+ assert_ep -1, -1.5.mtruncate(1)
153
+ assert_ep -1, -1.9.mtruncate(1)
154
+
155
+ x = 123.456
156
+ assert_ep 122, x.mtruncate(-2)
157
+ assert_ep 123.45, x.mtruncate(-0.01)
158
+ assert_ep 100, x.mtruncate(25)
159
+
160
+ x = -123.456
161
+ assert_ep -122, x.mtruncate(-2)
162
+ assert_ep -123.45, x.mtruncate(-0.01)
163
+ assert_ep -100, x.mtruncate(25)
164
+ end
165
+
166
+
167
+ def test_sceil
168
+ x = 123.456
169
+ assert_ep 200, x.sceil(1)
170
+ assert_ep 124, x.sceil(3)
171
+ assert_ep 123.5, x.sceil(4)
172
+ assert_ep 123.456, x.sceil(6)
173
+
174
+ x = -123.456
175
+ assert_ep -100, x.sceil(1)
176
+ assert_ep -123, x.sceil(3)
177
+ assert_ep -123.4, x.sceil(4)
178
+ assert_ep -123.456, x.sceil(6)
179
+ end
180
+
181
+ def test_sfloor
182
+ x = 123.456
183
+ assert_ep 100, x.sfloor(1)
184
+ assert_ep 123, x.sfloor(3)
185
+ assert_ep 123.4, x.sfloor(4)
186
+ assert_ep 123.456, x.sfloor(6)
187
+
188
+ x = -123.456
189
+ assert_ep -200, x.sfloor(1)
190
+ assert_ep -124, x.sfloor(3)
191
+ assert_ep -123.5, x.sfloor(4)
192
+ assert_ep -123.456, x.sfloor(6)
193
+ end
194
+
195
+ def test_sroundup
196
+ x = 123.456
197
+ assert_ep 200, x.sroundup(1)
198
+ assert_ep 124, x.sroundup(3)
199
+ assert_ep 123.5, x.sroundup(4)
200
+ assert_ep 123.456, x.sroundup(6)
201
+
202
+ x = -123.456
203
+ assert_ep -200, x.sroundup(1)
204
+ assert_ep -124, x.sroundup(3)
205
+ assert_ep -123.5, x.sroundup(4)
206
+ assert_ep -123.456, x.sroundup(6)
207
+ end
208
+
209
+ def test_srounddown
210
+ x = 123.456
211
+ assert_ep 100, x.srounddown(1)
212
+ assert_ep 123, x.srounddown(3)
213
+ assert_ep 123.4, x.srounddown(4)
214
+ assert_ep 123.456, x.srounddown(6)
215
+
216
+ x = -123.456
217
+ assert_ep -100, x.srounddown(1)
218
+ assert_ep -123, x.srounddown(3)
219
+ assert_ep -123.4, x.srounddown(4)
220
+ assert_ep -123.456, x.srounddown(6)
221
+ end
222
+
223
+ def test_sround
224
+ assert_ep 2, 1.5.sround(1)
225
+ assert_ep -2, -1.5.sround(1)
226
+
227
+ x = 123.456
228
+ assert_ep 100, x.sround(1)
229
+ assert_ep 123, x.sround(3)
230
+ assert_ep 123.5, x.sround(4)
231
+ assert_ep 123.456, x.sround(6)
232
+
233
+ x = -123.456
234
+ assert_ep -100, x.sround(1)
235
+ assert_ep -123, x.sround(3)
236
+ assert_ep -123.5, x.sround(4)
237
+ assert_ep -123.456, x.sround(6)
238
+ end
239
+
240
+ def test_struncate
241
+ x = 123.456
242
+ assert_ep 100, x.struncate(1)
243
+ assert_ep 123, x.struncate(3)
244
+ assert_ep 123.4, x.struncate(4)
245
+ assert_ep 123.456, x.struncate(6)
246
+
247
+ x = -123.456
248
+ assert_ep -100, x.struncate(1)
249
+ assert_ep -123, x.struncate(3)
250
+ assert_ep -123.4, x.struncate(4)
251
+ assert_ep -123.456, x.struncate(6)
252
+ end
253
+
254
+ def test_zero
255
+ x = 0
256
+ assert_ep x, x.roundup
257
+ assert_ep x, x.rounddown
258
+ assert_ep x, x.mceil(1)
259
+ assert_ep x, x.mfloor(1)
260
+ assert_ep x, x.mroundup(1)
261
+ assert_ep x, x.mrounddown(1)
262
+ assert_ep x, x.mround(1)
263
+ assert_ep x, x.mround(1)
264
+ assert_ep x, x.mtruncate(1)
265
+ assert_ep x, x.sceil(1)
266
+ assert_ep x, x.sfloor(1)
267
+ assert_ep x, x.sroundup(1)
268
+ assert_ep x, x.srounddown(1)
269
+ assert_ep x, x.sround(1)
270
+ assert_ep x, x.sround(1)
271
+ assert_ep x, x.struncate(1)
272
+ end
273
+ end
metadata CHANGED
@@ -1,16 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pretty_round
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
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-22 00:00:00.000000000 Z
11
+ date: 2019-01-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: This gem provide useful numerical rounding methods.
13
+ description: This gem provide useful numerical rounding methods such as nearest multiple
14
+ or significant digits.
14
15
  email:
15
16
  executables: []
16
17
  extensions: []
@@ -21,6 +22,7 @@ files:
21
22
  - examples/example.rb
22
23
  - examples/example2.rb
23
24
  - lib/pretty_round.rb
25
+ - lib/pretty_round/core.rb
24
26
  - test/test_pretty_round.rb
25
27
  homepage: https://github.com/diaphragm/ruby-pretty-round
26
28
  licenses:
@@ -34,7 +36,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
34
36
  requirements:
35
37
  - - ">="
36
38
  - !ruby/object:Gem::Version
37
- version: '1.8'
39
+ version: '2.3'
38
40
  required_rubygems_version: !ruby/object:Gem::Requirement
39
41
  requirements:
40
42
  - - ">="
@@ -42,7 +44,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
44
  version: '0'
43
45
  requirements: []
44
46
  rubyforge_project:
45
- rubygems_version: 2.4.8
47
+ rubygems_version: 2.7.8
46
48
  signing_key:
47
49
  specification_version: 4
48
50
  summary: pretty rounding methods library