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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d9050a8b1974209dc285b85f896030daed2e6319
4
- data.tar.gz: 239e8d8949dfadf97acd2b5d431d7c9722d93b9e
3
+ metadata.gz: c58fcab5f459dabf82d61393506c52f9ee2f8b6a
4
+ data.tar.gz: f899cd92c24b5cddcc24d10fb178c73c13e00a34
5
5
  SHA512:
6
- metadata.gz: 3ebf1ae8eaa28fbff80dadf844629f74e36593c094ee1b8a82aee615e1989b1d995a5512e573405422814c8bb581b5e44bad928372f91dccf40eff3584b6d854
7
- data.tar.gz: b8290d0f28ab189333ed4b16efdf5d5ea42f9d0d0b3d65d4822cda9e33ee234ee1da2ed544487b4be9814a4d4cd07aba2480d1ab714262e1c75dfee5812d99b7
6
+ metadata.gz: eb35817f7d71e8d69759ce1f4f2e74fc15b1e56a2ec1c106c70b55752687803637b1eec6a3c8e96f7faaae9bcf110ed7fd0de2788b4d672c2cb7eed9603b9616
7
+ data.tar.gz: 54fb82569099f421c32123cdcd2c5eb6f5bcc0c950709144b9ce362064ec6509720ee85024a7147377337add6f078316239cc0026da9bac92cc2fe2050a67e02
data/lib/unitwise.rb CHANGED
@@ -3,6 +3,7 @@ require 'memoizable'
3
3
  require 'parslet'
4
4
  require 'signed_multiset'
5
5
  require 'yaml'
6
+ require 'bigdecimal'
6
7
 
7
8
  require 'unitwise/version'
8
9
  require 'unitwise/base'
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
@@ -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
@@ -149,6 +149,10 @@ module Unitwise
149
149
  self.class.new(*args)
150
150
  end
151
151
 
152
+ def value=(value)
153
+ @value = value
154
+ end
155
+
152
156
  # Determine value of the unit after conversion to another unit
153
157
  # @api private
154
158
  def converted_value(other_unit)
@@ -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 Fixnum
18
+ # Set the scalar value for the prefix, always as a BigDecimal
19
19
  # @api semipublic
20
20
  def scalar=(value)
21
- @scalar = value.to_f
21
+ @scalar = BigDecimal(value.to_s)
22
22
  end
23
23
  end
24
24
  end
@@ -20,6 +20,10 @@ module Unitwise
20
20
  unit.atoms
21
21
  end
22
22
 
23
+ def value=(value)
24
+ @value = BigDecimal(value.to_s)
25
+ end
26
+
23
27
  # List the terms associated with this scale's unit.
24
28
  # @return [Array]
25
29
  # @api public
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) : 1)
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
@@ -1,3 +1,3 @@
1
1
  module Unitwise
2
- VERSION = '0.6.2'
2
+ VERSION = '0.7.0'
3
3
  end
@@ -53,7 +53,7 @@ module ScaleTests
53
53
  describe "#scalar" do
54
54
  it "must return value relative to terminal atoms" do
55
55
  subject.scalar.must_equal 4000
56
- mph.scalar.must_equal 26.8224
56
+ mph.scalar.must_almost_equal 26.8224
57
57
  cel.scalar.must_equal 295.15
58
58
  end
59
59
  end
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
@@ -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.must_equal 0.9144000000000001
106
- pi.scalar.must_equal 3.141592653589793
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.must_equal 96.56063999999999
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.must_equal 37
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.must_equal 531.27
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.round.must_equal 0
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.must_equal 3728.227153424004
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.must_equal 1.03561865372889
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.must_equal 122.13711922373341
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.must_equal -2.1371192237334
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.must_equal 3.280839895013123
170
+ convert.value.must_almost_equal 3.280839895
170
171
  end
171
172
 
172
173
  it "must not convert 'foo'" do
@@ -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.must_equal 6894757.293168359
32
- deg.scalar.must_equal 0.017453292519943295
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.6.2
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-06-08 00:00:00.000000000 Z
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