unitwise 0.6.2 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/unitwise.rb +1 -0
- data/lib/unitwise/atom.rb +2 -2
- data/lib/unitwise/functional.rb +11 -11
- data/lib/unitwise/measurement.rb +4 -0
- data/lib/unitwise/prefix.rb +2 -2
- data/lib/unitwise/scale.rb +4 -0
- data/lib/unitwise/term.rb +3 -3
- data/lib/unitwise/unit.rb +3 -3
- data/lib/unitwise/version.rb +1 -1
- data/test/support/scale_tests.rb +1 -1
- data/test/test_helper.rb +11 -1
- data/test/unitwise/atom_test.rb +2 -2
- data/test/unitwise/measurement_test.rb +10 -9
- data/test/unitwise/unit_test.rb +2 -2
- data/unitwise.gemspec +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c58fcab5f459dabf82d61393506c52f9ee2f8b6a
|
4
|
+
data.tar.gz: f899cd92c24b5cddcc24d10fb178c73c13e00a34
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb35817f7d71e8d69759ce1f4f2e74fc15b1e56a2ec1c106c70b55752687803637b1eec6a3c8e96f7faaae9bcf110ed7fd0de2788b4d672c2cb7eed9603b9616
|
7
|
+
data.tar.gz: 54fb82569099f421c32123cdcd2c5eb6f5bcc0c950709144b9ce362064ec6509720ee85024a7147377337add6f078316239cc0026da9bac92cc2fe2050a67e02
|
data/lib/unitwise.rb
CHANGED
data/lib/unitwise/atom.rb
CHANGED
@@ -103,11 +103,11 @@ module Unitwise
|
|
103
103
|
# @return [Numeric]
|
104
104
|
# @api public
|
105
105
|
def scalar(magnitude = 1)
|
106
|
-
base? ? magnitude : scale.scalar(magnitude)
|
106
|
+
base? ? BigDecimal(magnitude.to_s) : scale.scalar(magnitude)
|
107
107
|
end
|
108
108
|
|
109
109
|
def magnitude(scalar = scalar)
|
110
|
-
special? ? scale.magnitude(scalar) : 1
|
110
|
+
special? ? scale.magnitude(scalar) : BigDecimal('1')
|
111
111
|
end
|
112
112
|
|
113
113
|
# An atom may have a complex scale with several base atoms at various
|
data/lib/unitwise/functional.rb
CHANGED
@@ -19,7 +19,7 @@ module Unitwise
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def self.from_degf(x)
|
22
|
-
5.0 / 9 * (x + 459.67)
|
22
|
+
5.0 / 9.0 * (x + 459.67)
|
23
23
|
end
|
24
24
|
|
25
25
|
def self.to_hpX(x)
|
@@ -27,23 +27,23 @@ module Unitwise
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def self.from_hpX(x)
|
30
|
-
10 ** -x
|
30
|
+
10.0 ** -x
|
31
31
|
end
|
32
32
|
|
33
33
|
def self.to_hpC(x)
|
34
|
-
-log(x) / log(100)
|
34
|
+
-log(x) / log(100.0)
|
35
35
|
end
|
36
36
|
|
37
37
|
def self.from_hpC(x)
|
38
|
-
100 ** -x
|
38
|
+
100.0 ** -x
|
39
39
|
end
|
40
40
|
|
41
41
|
def self.to_tan100(x)
|
42
|
-
100 * tan(x)
|
42
|
+
100.0 * tan(x)
|
43
43
|
end
|
44
44
|
|
45
45
|
def self.from_tan100(x)
|
46
|
-
atan(x / 100)
|
46
|
+
atan(x / 100.0)
|
47
47
|
end
|
48
48
|
|
49
49
|
def self.to_ph(x)
|
@@ -55,11 +55,11 @@ module Unitwise
|
|
55
55
|
end
|
56
56
|
|
57
57
|
def self.to_ld(x)
|
58
|
-
Math.log(x) / Math.log(2)
|
58
|
+
Math.log(x) / Math.log(2.0)
|
59
59
|
end
|
60
60
|
|
61
61
|
def self.from_ld(x)
|
62
|
-
2 ** x
|
62
|
+
2.0 ** x
|
63
63
|
end
|
64
64
|
|
65
65
|
def self.to_ln(x)
|
@@ -75,15 +75,15 @@ module Unitwise
|
|
75
75
|
end
|
76
76
|
|
77
77
|
def self.from_lg(x)
|
78
|
-
10 ** x
|
78
|
+
10.0 ** x
|
79
79
|
end
|
80
80
|
|
81
81
|
def self.to_2lg(x)
|
82
|
-
2 * log10(x)
|
82
|
+
2.0 * log10(x)
|
83
83
|
end
|
84
84
|
|
85
85
|
def self.from_2lg(x)
|
86
|
-
10 ** (x / 2)
|
86
|
+
10.0 ** (x / 2.0)
|
87
87
|
end
|
88
88
|
|
89
89
|
attr_reader :function_name
|
data/lib/unitwise/measurement.rb
CHANGED
data/lib/unitwise/prefix.rb
CHANGED
@@ -15,10 +15,10 @@ module Unitwise
|
|
15
15
|
Unitwise.data_file 'prefix'
|
16
16
|
end
|
17
17
|
|
18
|
-
# Set the scalar value for the prefix, always as a
|
18
|
+
# Set the scalar value for the prefix, always as a BigDecimal
|
19
19
|
# @api semipublic
|
20
20
|
def scalar=(value)
|
21
|
-
@scalar = value.
|
21
|
+
@scalar = BigDecimal(value.to_s)
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
data/lib/unitwise/scale.rb
CHANGED
data/lib/unitwise/term.rb
CHANGED
@@ -59,8 +59,8 @@ module Unitwise
|
|
59
59
|
# @param magnitude [Numeric] The magnitude to calculate the scalar for.
|
60
60
|
# @return [Numeric] The unitless linear scalar value.
|
61
61
|
# @api public
|
62
|
-
def scalar(magnitude = 1)
|
63
|
-
calculate(atom ? atom.scalar(magnitude) :
|
62
|
+
def scalar(magnitude = 1.0)
|
63
|
+
calculate(atom ? atom.scalar(magnitude) : magnitude)
|
64
64
|
end
|
65
65
|
|
66
66
|
# Calculate the magnitude for this term
|
@@ -68,7 +68,7 @@ module Unitwise
|
|
68
68
|
# @return [Numeric] The magnitude on this scale.
|
69
69
|
# @api public
|
70
70
|
def magnitude(scalar = scalar)
|
71
|
-
calculate(atom ? atom.magnitude(scalar) : 1)
|
71
|
+
calculate(atom ? atom.magnitude(scalar) : 1.0)
|
72
72
|
end
|
73
73
|
|
74
74
|
# The base units this term is derived from
|
data/lib/unitwise/unit.rb
CHANGED
@@ -46,14 +46,14 @@ module Unitwise
|
|
46
46
|
end
|
47
47
|
memoize :root_terms
|
48
48
|
|
49
|
-
def scalar(magnitude = 1)
|
50
|
-
terms.reduce(1) do |prod, term|
|
49
|
+
def scalar(magnitude = 1.0)
|
50
|
+
terms.reduce(1.0) do |prod, term|
|
51
51
|
prod * term.scalar(magnitude)
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
55
|
def magnitude(scalar = scalar)
|
56
|
-
terms.reduce(1) do |prod, term|
|
56
|
+
terms.reduce(1.0) do |prod, term|
|
57
57
|
prod * term.magnitude(scalar)
|
58
58
|
end
|
59
59
|
end
|
data/lib/unitwise/version.rb
CHANGED
data/test/support/scale_tests.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -6,4 +6,14 @@ end
|
|
6
6
|
require 'minitest/autorun'
|
7
7
|
require 'minitest/pride'
|
8
8
|
|
9
|
-
require 'unitwise'
|
9
|
+
require 'unitwise'
|
10
|
+
|
11
|
+
module Minitest::Assertions
|
12
|
+
def assert_almost_equal(expected, actual)
|
13
|
+
message = "Expected #{actual} to be almost equal to #{expected}"
|
14
|
+
range = 0.00001
|
15
|
+
assert expected + range > actual && expected - range < actual, message
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Numeric.infect_an_assertion :assert_almost_equal, :must_almost_equal
|
data/test/unitwise/atom_test.rb
CHANGED
@@ -102,8 +102,8 @@ describe Unitwise::Atom do
|
|
102
102
|
describe "#scalar" do
|
103
103
|
it "must return scalar relative to terminal atom" do
|
104
104
|
second.scalar.must_equal 1
|
105
|
-
yard.scalar.
|
106
|
-
pi.scalar.
|
105
|
+
yard.scalar.must_almost_equal 0.9144
|
106
|
+
pi.scalar.must_almost_equal 3.141592653589793
|
107
107
|
end
|
108
108
|
end
|
109
109
|
|
@@ -13,7 +13,7 @@ describe Unitwise::Measurement do
|
|
13
13
|
|
14
14
|
describe "#convert_to" do
|
15
15
|
it "must convert to a similar unit code" do
|
16
|
-
mph.convert_to('km/h').value.
|
16
|
+
mph.convert_to('km/h').value.must_almost_equal 96.56063
|
17
17
|
end
|
18
18
|
it "must raise an error if the units aren't similar" do
|
19
19
|
lambda { mph.convert_to('N') }.must_raise Unitwise::ConversionError
|
@@ -25,13 +25,13 @@ describe Unitwise::Measurement do
|
|
25
25
|
k.convert_to('Cel').value.must_equal 100
|
26
26
|
end
|
27
27
|
it "must convert special units to special units" do
|
28
|
-
f.convert_to('Cel').value.
|
28
|
+
f.convert_to('Cel').value.must_almost_equal 37
|
29
29
|
end
|
30
30
|
it "must convert special units to non-special units" do
|
31
|
-
cel.convert_to("[degR]").value.
|
31
|
+
cel.convert_to("[degR]").value.must_almost_equal(531.27)
|
32
32
|
end
|
33
33
|
it "must convert derived units to special units" do
|
34
|
-
r.convert_to("Cel").value.
|
34
|
+
r.convert_to("Cel").value.must_almost_equal(0)
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -43,7 +43,7 @@ describe Unitwise::Measurement do
|
|
43
43
|
end
|
44
44
|
it "must multiply similar units" do
|
45
45
|
mult = mph * kmh
|
46
|
-
mult.value.
|
46
|
+
mult.value.must_almost_equal 3728.22715342
|
47
47
|
mult.unit.must_equal Unitwise::Unit.new("([mi_i]/h).([mi_i]/h)")
|
48
48
|
end
|
49
49
|
it "must multiply unsimilar units" do
|
@@ -66,7 +66,7 @@ describe Unitwise::Measurement do
|
|
66
66
|
end
|
67
67
|
it "must divide by the value of similar units" do
|
68
68
|
div = kmh / mph
|
69
|
-
div.value.
|
69
|
+
div.value.must_almost_equal 1.03561865
|
70
70
|
div.unit.to_s.must_equal '1'
|
71
71
|
end
|
72
72
|
it "must divide dissimilar units" do
|
@@ -79,7 +79,7 @@ describe Unitwise::Measurement do
|
|
79
79
|
describe "#+" do
|
80
80
|
it "must add values when units are similar" do
|
81
81
|
added = mph + kmh
|
82
|
-
added.value.
|
82
|
+
added.value.must_almost_equal 122.13711922
|
83
83
|
added.unit.must_equal mph.unit
|
84
84
|
end
|
85
85
|
it "must raise an error when units are not similar" do
|
@@ -90,7 +90,7 @@ describe Unitwise::Measurement do
|
|
90
90
|
describe "#-" do
|
91
91
|
it "must add values when units are similar" do
|
92
92
|
added = mph - kmh
|
93
|
-
added.value.
|
93
|
+
added.value.must_almost_equal(-2.1371192)
|
94
94
|
added.unit.must_equal mph.unit
|
95
95
|
end
|
96
96
|
it "must raise an error when units are not similar" do
|
@@ -126,6 +126,7 @@ describe Unitwise::Measurement do
|
|
126
126
|
|
127
127
|
describe "equality" do
|
128
128
|
let(:m) { Unitwise::Measurement.new(1,'m') }
|
129
|
+
let(:feet) { Unitwise::Measurement.new(20, 'foot') }
|
129
130
|
let(:mm) { Unitwise::Measurement.new(1000,'mm') }
|
130
131
|
let(:foot) { Unitwise::Measurement.new(1,'foot') }
|
131
132
|
let(:g) { Unitwise::Measurement.new(1,'gram') }
|
@@ -166,7 +167,7 @@ describe Unitwise::Measurement do
|
|
166
167
|
it "must convert 'to_foot'" do
|
167
168
|
convert = meter.to_foot
|
168
169
|
convert.must_be_instance_of Unitwise::Measurement
|
169
|
-
convert.value.
|
170
|
+
convert.value.must_almost_equal 3.280839895
|
170
171
|
end
|
171
172
|
|
172
173
|
it "must not convert 'foo'" do
|
data/test/unitwise/unit_test.rb
CHANGED
@@ -28,8 +28,8 @@ describe Unitwise::Unit do
|
|
28
28
|
it "must return value relative to terminal atoms" do
|
29
29
|
ms2.must_respond_to :scalar
|
30
30
|
ms2.scalar.must_equal 1
|
31
|
-
psi.scalar.
|
32
|
-
deg.scalar.
|
31
|
+
psi.scalar.must_almost_equal 6894757.293168361
|
32
|
+
deg.scalar.must_almost_equal 0.0174532925199433
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
data/unitwise.gemspec
CHANGED
@@ -25,6 +25,7 @@ Gem::Specification.new do |gem|
|
|
25
25
|
gem.add_dependency 'memoizable', '~> 0.4'
|
26
26
|
|
27
27
|
if RUBY_VERSION > '1.8.7'
|
28
|
+
gem.add_dependency 'bigdecimal', '~> 1.2' unless RUBY_PLATFORM == 'java'
|
28
29
|
gem.add_dependency 'parslet', '~> 1.5'
|
29
30
|
gem.add_development_dependency 'nokogiri', '~> 1.5'
|
30
31
|
gem.add_development_dependency 'coveralls', '~> 0.6'
|
@@ -32,7 +33,6 @@ Gem::Specification.new do |gem|
|
|
32
33
|
gem.add_dependency 'parslet', '~> 1.5.0'
|
33
34
|
gem.add_development_dependency 'nokogiri', '~> 1.5.10'
|
34
35
|
end
|
35
|
-
|
36
36
|
gem.add_development_dependency 'pry', '~> 0.9'
|
37
37
|
gem.add_development_dependency 'minitest', '~> 5.0'
|
38
38
|
gem.add_development_dependency 'rake', '~> 10.0'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unitwise
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Lewis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: liner
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0.4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bigdecimal
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.2'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.2'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: parslet
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|