pretty_round 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 715c35c7f940f136ab635fefb6f253f72ce4e712
4
+ data.tar.gz: 6ff79a9c50c397001a2f12ac00d7076d03316162
5
+ SHA512:
6
+ metadata.gz: 6987805eeb0bb0183f77ef2a5605d8da58e5dcf5bc030548681269ddc36d93f9cb491de404d66626f5228100a321cef8c70c7c24fb8fe192d5ca696517deed77
7
+ data.tar.gz: c4e508d1aaec2a2fa399ad5fdf597088d1d7407b5640ff852bb06237a48b1f97de66848725b9f643cb0bb4ab689f248d540e544ee94ce2ec9b90bfea49f91aa3
data/LICENSE ADDED
@@ -0,0 +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
+
data/README.md ADDED
@@ -0,0 +1,70 @@
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.round(2) #=> 123.46 (ruby built-in)
20
+ x.roundup(2) #=> 123.46
21
+ x.rounddown(2) #=> 123.45
22
+
23
+ # 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
28
+
29
+ # round with significant digit
30
+ x.sround(2) #=> 120
31
+ x.sround(4) #=> 123.5
32
+ ```
33
+
34
+ Rounding direction
35
+ ================
36
+
37
+ `#roundup`, `#mroundup`
38
+ ----------------
39
+ These methods round up to the positive infinity direction.
40
+ ```rb
41
+ 1.9.mroundup(1) #=> 2
42
+ 1.1.mroundup(1) #=> 2
43
+ -1.1.mroundup(1) #=> -1
44
+ -1.9.mroundup(1) #=> -1
45
+ ```
46
+
47
+ `#rounddown`, `#mrounddown`
48
+ ----------------
49
+ These methods round down to the negative infinity direction.
50
+ ```rb
51
+ 1.9.mrounddown(1) #=> 1
52
+ 1.1.mrounddown(1) #=> 1
53
+ -1.1.mrounddown(1) #=> -2
54
+ -1.9.mrounddown(1) #=> -2
55
+ ```
56
+
57
+ `#mround`, `#sround`
58
+ ----------------
59
+ These methods round off the absolute value, dos not round even.
60
+ ```rb
61
+ 1.9.mround(1) #=> 2
62
+ 1.1.mround(1) #=> 1
63
+ -1.1.mround(1) #=> -1
64
+ -1.9.mround(1) #=> -2
65
+ ```
66
+
67
+ ----
68
+ Also, try to run `examples/example.rb`
69
+
70
+
@@ -0,0 +1,39 @@
1
+
2
+ require "pretty_round"
3
+
4
+ xs = %w[2.1 2.5 2.9 -2.1 -2.5 -2.9].map(&:to_r)
5
+ ns = %w[0.1 0.2 1 2 -0.1 -0.2 -1 -2].map(&:to_r)
6
+
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
13
+ end
14
+ puts
15
+
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
+
30
+
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
38
+ end
39
+
@@ -0,0 +1,49 @@
1
+
2
+ class Numeric
3
+ # Ceiling with given precision.
4
+ def roundup(d=0)
5
+ x = 10**(-d)
6
+ self.quo(x).ceil * x
7
+ end
8
+
9
+ # Flooring with given precision.
10
+ def rounddown(d=0)
11
+ x = 10**(-d)
12
+ self.quo(x).floor * x
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 mroundup(num)
19
+ div, mod = divmod(num)
20
+ x = num * div; y = x + (mod.zero? ? 0 : num)
21
+ [x, y].max
22
+ end
23
+
24
+ # Return nearest multiple of given number that is equal to or less than self.
25
+ # This method round down to the negative infinity direction.
26
+ def mrounddown(num)
27
+ div, mod = divmod(num)
28
+ x = num * div; y = x + (mod.zero? ? 0 : num)
29
+ [x, y].min
30
+ end
31
+
32
+ # Retuen nearest multiple of given number.
33
+ # When self is median of multiple of given number, return the multiple that have greater absolute.
34
+ def mround(num)
35
+ div, mod = divmod(num)
36
+ x = num * div; y = x + (mod.zero? ? 0 : num)
37
+ if mod.quo(num).abs * 2 == 1
38
+ [x, y].max_by(&:abs)
39
+ else
40
+ [x, y].min_by{|t| (t - self).abs}
41
+ end
42
+ end
43
+
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))
48
+ end
49
+ end
@@ -0,0 +1,113 @@
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.1.roundup
14
+ assert_ep -1, -1.1.roundup
15
+ assert_ep -1, -1.9.roundup
16
+
17
+ x = 123.456
18
+ assert_ep 124, x.roundup
19
+ assert_ep 123.46, x.roundup(2)
20
+ assert_ep 200, x.roundup(-2)
21
+
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)
26
+ end
27
+
28
+ def test_rounddown
29
+ assert_ep 1, 1.9.rounddown
30
+ assert_ep 1, 1.1.rounddown
31
+ assert_ep -2, -1.1.rounddown
32
+ assert_ep -2, -1.9.rounddown
33
+
34
+ x = 123.456
35
+ assert_ep 123, x.rounddown
36
+ assert_ep 123.45, x.rounddown(2)
37
+ assert_ep 100, x.rounddown(-2)
38
+
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)
43
+ end
44
+
45
+ def test_mroundup
46
+ assert_ep 2, 1.9.mroundup(1)
47
+ assert_ep 2, 1.1.mroundup(1)
48
+ assert_ep -1, -1.1.mroundup(1)
49
+ assert_ep -1, -1.9.mroundup(1)
50
+
51
+ x = 123.456
52
+ assert_ep 124, x.mroundup(-1)
53
+ assert_ep 123.46, x.mroundup(-0.01)
54
+ assert_ep 200, x.mroundup(100)
55
+
56
+ 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)
60
+ end
61
+
62
+ def test_mrounddown
63
+ assert_ep 1, 1.9.mrounddown(1)
64
+ assert_ep 1, 1.1.mrounddown(1)
65
+ assert_ep -2, -1.1.mrounddown(1)
66
+ assert_ep -2, -1.9.mrounddown(1)
67
+
68
+ x = 123.456
69
+ assert_ep 123, x.mrounddown(-1)
70
+ assert_ep 123.45, x.mrounddown(-0.01)
71
+ assert_ep 100, x.mrounddown(100)
72
+
73
+ 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)
77
+ end
78
+
79
+ def test_mround
80
+ assert_ep 2, 1.9.mround(1)
81
+ assert_ep 1, 1.1.mround(1)
82
+ assert_ep -1, -1.1.mround(1)
83
+ assert_ep -2, -1.9.mround(1)
84
+
85
+ x = 123.456
86
+ assert_ep 123, x.mround(-1)
87
+ assert_ep 123.46, x.mround(-0.01)
88
+ assert_ep 100, x.mround(100)
89
+
90
+ x = -123.456
91
+ assert_ep -123, x.mround(-1)
92
+ assert_ep -123.46, x.mround(-0.01)
93
+ assert_ep -100, x.mround(100)
94
+ end
95
+
96
+ def test_sround
97
+ x = 123.456
98
+ assert_ep 0, x.sround(-1)
99
+ assert_ep 0, x.sround(0)
100
+ assert_ep 100, x.sround(1)
101
+ assert_ep 123, x.sround(3)
102
+ assert_ep 123.5, x.sround(4)
103
+ assert_ep 123.456, x.sround(6)
104
+
105
+ x = -123.456
106
+ assert_ep 0, x.sround(-1)
107
+ assert_ep 0, x.sround(0)
108
+ assert_ep -100, x.sround(1)
109
+ assert_ep -123, x.sround(3)
110
+ assert_ep -123.5, x.sround(4)
111
+ assert_ep -123.456, x.sround(6)
112
+ end
113
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pretty_round
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - diaphragm
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This gem provide useful numerical rounding methods.
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - README.md
21
+ - examples/example.rb
22
+ - lib/pretty_round.rb
23
+ - test/test_pretty_round.rb
24
+ homepage: https://github.com/diaphragm/ruby-pretty-round
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '1.8'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.4.5
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: pretty rounding methods library
48
+ test_files: []