dimensional 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
1
  0.1.0
2
2
  0.1.1 Simpler conversion system-to-system and redefinition of preference semantics.
3
- 0.1.2 Cleanup tests to work with gem check -t
3
+ 0.1.2 Cleanup tests to work with gem check -t
4
+ 1.0.0 Production Release 1
5
+ 1.0.1 Use Rationals for default values and avoid the dreaded integer division = 0 problem
@@ -48,7 +48,7 @@ module Dimensional
48
48
  @name = name.to_s
49
49
  @system = system
50
50
  @dimension = dimension
51
- @reference_factor = options[:reference_factor] || 1
51
+ @reference_factor = options[:reference_factor] || Rational(1,1)
52
52
  @reference_units = options[:reference_units] || {}
53
53
  @abbreviation = options[:abbreviation]
54
54
  @detector = options[:detector] || /\A#{[name, abbreviation].compact.join('|')}\Z/
@@ -73,7 +73,7 @@ module Dimensional
73
73
  # The technique used is to multiply the bases' exponents by our exponent and then consolidate
74
74
  # resulting common bases by adding their exponents.
75
75
  def base
76
- return {self => 1} if base?
76
+ return {self => Rational(1,1)} if base?
77
77
  @base ||= reference_units.inject({}) do |summary0, (ru0, exp0)|
78
78
  t = ru0.base.inject({}){|summary1, (ru1, exp1)| summary1[ru1] = exp1 * exp0;summary1}
79
79
  summary0.merge(t) {|ru, expa, expb| expa + expb}
@@ -82,13 +82,13 @@ module Dimensional
82
82
 
83
83
  # The conversion factor relative to the base unit.
84
84
  def factor
85
- @factor ||= reference_factor * reference_units.inject(1){|f, (ru, exp)| f * (ru.factor**exp)}
85
+ @factor ||= reference_factor * reference_units.inject(Rational(1,1)){|f, (ru, exp)| f * (ru.factor**exp)}
86
86
  end
87
87
 
88
88
  # Returns the conversion factor to convert to the other unit
89
89
  def convert(other)
90
90
  raise "Units #{self} and #{other} are not commensurable" unless commensurable?(other)
91
- return 1 if self == other
91
+ return Rational(1,1) if self == other
92
92
  self.factor / other.factor
93
93
  end
94
94
 
@@ -1,3 +1,3 @@
1
1
  module Dimensional
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -161,6 +161,14 @@ class MetricTest < Test::Unit::TestCase
161
161
  assert_same @fathom, new.unit
162
162
  end
163
163
 
164
+ def test_convert_with_fractional_factor
165
+ range = Class.new(Metric)
166
+ range.dimension = Dimension::L
167
+ new = range.new(100000, @meter).convert(@kilometer)
168
+ assert_in_delta(100, new, 0.000001)
169
+ assert_same @kilometer, new.unit
170
+ end
171
+
164
172
  def test_identity_conversion
165
173
  depth = Class.new(Metric)
166
174
  depth.dimension = Dimension::L
@@ -25,7 +25,9 @@ class UnitTest < Test::Unit::TestCase
25
25
  assert_same Dimension::L, u.dimension
26
26
  assert u.base?
27
27
  assert_equal({u => 1}, u.base)
28
- assert_same 1, u.factor
28
+ assert_kind_of Rational, u.base[u]
29
+ assert_equal 1, u.factor
30
+ assert_kind_of Rational, u.factor
29
31
  end
30
32
 
31
33
  def test_enumerability
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dimensional
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Hapgood
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-06 00:00:00 -05:00
12
+ date: 2010-02-08 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15