vanunits 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/HISTORY +16 -0
  2. data/HOWITWORKS.rdoc +72 -0
  3. data/LICENSE +23 -0
  4. data/README.rdoc +68 -0
  5. data/lib/van/units.rb +116 -0
  6. data/lib/van/units/base.rb +991 -0
  7. data/lib/van/units/constants.rb +2 -0
  8. data/lib/van/units/constants/cgs.rb +122 -0
  9. data/lib/van/units/constants/math.rb +3 -0
  10. data/lib/van/units/constants/math/cgs.rb +125 -0
  11. data/lib/van/units/constants/math/mks.rb +126 -0
  12. data/lib/van/units/constants/math/natural.rb +33 -0
  13. data/lib/van/units/constants/mks.rb +122 -0
  14. data/lib/van/units/currency.rb +160 -0
  15. data/lib/van/units/data/binary/base.rb +4 -0
  16. data/lib/van/units/data/cex.rb +5 -0
  17. data/lib/van/units/data/currency-default.rb +5 -0
  18. data/lib/van/units/data/currency-standard.rb +2 -0
  19. data/lib/van/units/data/currency/base.rb +89 -0
  20. data/lib/van/units/data/iec.rb +5 -0
  21. data/lib/van/units/data/iec_binary/base.rb +6 -0
  22. data/lib/van/units/data/si.rb +8 -0
  23. data/lib/van/units/data/si/base.rb +11 -0
  24. data/lib/van/units/data/si/constants.rb +88 -0
  25. data/lib/van/units/data/si/derived.rb +33 -0
  26. data/lib/van/units/data/si/extra.rb +35 -0
  27. data/lib/van/units/data/si/misc.rb +10 -0
  28. data/lib/van/units/data/uk.rb +10 -0
  29. data/lib/van/units/data/uk/base.rb +25 -0
  30. data/lib/van/units/data/units-default.rb +12 -0
  31. data/lib/van/units/data/units-standard.rb +5 -0
  32. data/lib/van/units/data/us.rb +10 -0
  33. data/lib/van/units/data/us/base.rb +47 -0
  34. data/lib/van/units/data/xmethods.rb +5 -0
  35. data/lib/van/units/data/xmethods/cached.rb +84 -0
  36. data/lib/van/units/data/xmethods/mapping.rb +87 -0
  37. data/lib/van/units/loaders.rb +100 -0
  38. data/lib/van/units/units.rb +111 -0
  39. data/lib/van/units_currency.rb +12 -0
  40. data/meta/author +1 -0
  41. data/meta/collection +1 -0
  42. data/meta/contact +1 -0
  43. data/meta/created +1 -0
  44. data/meta/description +5 -0
  45. data/meta/homepage +1 -0
  46. data/meta/name +1 -0
  47. data/meta/summary +1 -0
  48. data/meta/title +1 -0
  49. data/meta/version +1 -0
  50. data/qed/conversion.rdoc +14 -0
  51. data/qed/equality.rdoc +150 -0
  52. data/qed/operations.rdoc +74 -0
  53. data/test/test_constants.rb +12 -0
  54. data/test/test_currency.rb +26 -0
  55. data/test/test_units.rb +205 -0
  56. metadata +123 -0
@@ -0,0 +1,12 @@
1
+ require 'van/units/currency'
2
+
3
+ module Van
4
+ module Units
5
+
6
+ # Load conversion units.
7
+ class Converter
8
+ require("currency-standard")
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1 @@
1
+ Peter Vanbroekhoven
@@ -0,0 +1 @@
1
+ rubyworks
@@ -0,0 +1 @@
1
+ Trans <transfire at gmail.com>
@@ -0,0 +1 @@
1
+ 2005-11-26
@@ -0,0 +1,5 @@
1
+ VanUnits is a units system providing both SI units and web-updated
2
+ currency units, via a very easy to use method-based interface.
3
+ It also include a large set of real world contants, provided in
4
+ "m kg s" and "cm kg s" measures.
5
+
@@ -0,0 +1 @@
1
+ http://rubyworks.github.com/vanunits
@@ -0,0 +1 @@
1
+ vanunits
@@ -0,0 +1 @@
1
+ Vanbroekhoven's Units System
@@ -0,0 +1 @@
1
+ VanUnits
@@ -0,0 +1 @@
1
+ 1.5.0
@@ -0,0 +1,14 @@
1
+ = Unit Conversions
2
+
3
+ require 'stick/units/si'
4
+
5
+ Check conversions to same type.
6
+
7
+ 1.g.to(1.g).assert == 1.g
8
+ 1.kg.to(1.kg).assert == 1.kg
9
+
10
+ Check conversions of simple multiples.
11
+
12
+ 1.kg.to(1.g).assert == 1000.g
13
+ 1.km.to(1.m).assert == 1000.m
14
+
@@ -0,0 +1,150 @@
1
+ = Unit Equality
2
+
3
+ Equality is a broader issue for unit systems, then it is for
4
+ the usual type system, since two aspect of a thing are being
5
+ compared, the measures value and it's unit.
6
+
7
+ require 'stick/units/si'
8
+
9
+ == Case Equality
10
+
11
+ Check case equality of prime units.
12
+
13
+ 1.g.assert === 1.g
14
+ 1.kg.assert === 1.kg
15
+ 1.N.assert === 1.N
16
+
17
+ 1.g.assert === 10.g
18
+ 1.kg.assert === 10.kg
19
+ 1.N.assert === 10.N
20
+
21
+
22
+ == Value Equality
23
+
24
+ Check value equality of prime units.
25
+
26
+ 1.g.assert == 1.g
27
+ 1.kg.assert == 1.kg
28
+ 1.km.assert == 1.km
29
+
30
+ 1.g.refute == 10.g
31
+ 1.kg.refute == 10.kg
32
+ 1.km.refute == 10.km
33
+
34
+ Check value equality of commensurate prime units.
35
+
36
+ 1.kg.assert == 1000.g
37
+ 1.km.assert == 1000.m
38
+
39
+ 1000.g.assert == 1.kg
40
+ 1000.m.assert == 1.km
41
+
42
+ Check value equality of commensurate prefixed units.
43
+
44
+ 1000000.g.assert == 1.Mg
45
+ 0.001.g.assert == 1.mg
46
+
47
+ Check value equality of compound units.
48
+
49
+ 1.g.m.assert == 1.g.m
50
+ 1.kg.m.assert == 1.kg.m
51
+
52
+ 1.m.g.assert == 1.g.m
53
+ 1.m.kg.assert == 1.kg.m
54
+
55
+ Check value equality of commensurate compound units.
56
+
57
+ 1.kg.m.assert == 1000.g.m
58
+ 1.km.s.assert == 1000.m.s
59
+
60
+ 1.m.kg.assert == 1000.g.m
61
+ 1.s.km.assert == 1000.m.s
62
+
63
+ 1.s.kg.refute == 1000.g.m
64
+
65
+
66
+ == Unit Equality
67
+
68
+ Check unit equality of like units.
69
+
70
+ 1.g.assert.equals? 1.g
71
+ 1.kg.assert.equals? 1.kg
72
+ 1.km.assert.equals? 1.km
73
+
74
+ Check unit inequality of commensutate units.
75
+
76
+ 1.kg.refute.equals? 1000.g
77
+ 1.km.refute.equals? 1000.m
78
+
79
+ 1000.g.refute.equals? 1.kg
80
+ 1000.m.refute.equals? 1.km
81
+
82
+ Check unit equality of commensurate prefixed units.
83
+
84
+ 1000000.g.refute.equals? 1.Mg
85
+ 0.001.g.refute.equals? 1.mg
86
+
87
+ Check unit equality of compound units.
88
+
89
+ 1.g.m.assert.equals? 1.g.m
90
+ 1.kg.m.assert.equals? 1.kg.m
91
+
92
+ 1.m.g.assert.equals? 1.g.m
93
+ 1.m.kg.assert.equals? 1.kg.m
94
+
95
+
96
+ == Identity Equality
97
+
98
+ TODO: Check identity equality of like units.
99
+ This may need fixing.
100
+
101
+ 1.g.assert.eql? 1.g
102
+ 1.kg.assert.eql? 1.kg
103
+ 1.km.assert.eql? 1.km
104
+
105
+
106
+ == Commensurate Equality
107
+
108
+ Two measures are commensurate when their units ar the
109
+ same or can be converted to be the same, irregardless
110
+ of the powers of those units.
111
+
112
+ 1.kg.assert.commensurate? 1.kg
113
+ 1.kg.assert.commensurate? 1.g
114
+
115
+ 1.kg.assert.commensurate? 1.kg**2
116
+ 1.kg.assert.commensurate? 1.g**2
117
+
118
+
119
+ == Proportional Equality
120
+
121
+ Two measures are proportional when their units are
122
+ exactly the same or can be converted to be exactly
123
+ the same --including their power.
124
+
125
+ 1.kg.assert.proportional? 1.kg
126
+ 1.kg.assert.proportional? 1.g
127
+
128
+ 1.kg.refute.proportional? 1.kg**2
129
+
130
+ Check proportional equality of commensurate prefixed units.
131
+
132
+ 1000000.g.assert.proportional? 1.Mg
133
+ 0.001.g.assert.proportional? 1.mg
134
+
135
+ == Systemtic Equality
136
+
137
+ Two measures are systemic when all their units
138
+ are from the same unit system.
139
+
140
+ 1.kg.assert.systemic? 1.kg
141
+ 1.kg.assert.systemic? 1.g
142
+
143
+ Check proportional equality of commensurate prefixed units.
144
+
145
+ 1000000.g.assert.systemic? 1.Mg
146
+ 0.001.g.assert.systemic? 1.mg
147
+
148
+ ---
149
+ * Helper[helper.rb]
150
+
@@ -0,0 +1,74 @@
1
+ = Unit Operations
2
+
3
+ Unit operations include all the typical numerical operations.
4
+
5
+ require 'stick/units/si'
6
+
7
+ == Unit Power
8
+
9
+ Unit power is used to create a measure of a squared, cubic
10
+ or higher diemension units. It works just like the normal
11
+ power method (#**) except that it leaves the measure numerical
12
+ value unchanged.
13
+
14
+ (1.m^2).assert == 1.m * 1.m
15
+ (2.m^2).assert == 2.m * 1.m
16
+
17
+
18
+ == Power
19
+
20
+ (1.m ** 2).assert == 1.m * 1.m
21
+ (2.m ** 2).assert == 2.m * 2.m
22
+
23
+
24
+ == Multiplication
25
+
26
+ Multiplication with scalars.
27
+
28
+ (1.m * 1).assert == 1.m
29
+ (1.m * 2).assert == 2.m
30
+ (3.m * 2).assert == 6.m
31
+
32
+ Multiplication of like units.
33
+
34
+ (1.m * 1.m).assert == 1.m^2
35
+ (2.m * 3.m).assert == 6.m^2
36
+
37
+ (2.m * 3.m * 4.m).assert == 24.m^3
38
+
39
+ (1.N * 1.kN).assert == 1001.N
40
+
41
+ Multiplication of unlike units.
42
+
43
+ (1.m * 2.g).assert == 1.m * 1.g * 2
44
+
45
+
46
+ == Division
47
+
48
+ Division with like units.
49
+
50
+ ((2.m^2) / 2.m).assert == 1.m
51
+
52
+ Division with scalars.
53
+
54
+ (1.m / 1.m).assert == 1
55
+ (2.m / 1.m).assert == 2
56
+
57
+
58
+ == Addition
59
+
60
+ (1.m + 1.m).assert == 2.m
61
+
62
+
63
+ == Subtraction
64
+
65
+ (2.m - 1.m).assert == 1.m
66
+
67
+
68
+ == Inversion
69
+
70
+ 2.m.invert.assert == 0.5 * (1.m^-1)
71
+
72
+ ---
73
+
74
+
@@ -0,0 +1,12 @@
1
+ require 'test/unit'
2
+
3
+ class TestConstants < Test::Unit::TestCase
4
+
5
+ def test_require_constants
6
+ assert_nothing_raised do
7
+ require 'van/units/constants'
8
+ end
9
+ end
10
+
11
+ end
12
+
@@ -0,0 +1,26 @@
1
+ $:.unshift(File.dirname(__FILE__) + '../lib')
2
+
3
+ require 'test/unit'
4
+ require 'van/units_currency'
5
+
6
+ class TestUnitsCurrency < Test::Unit::TestCase
7
+
8
+ include Van::Units
9
+
10
+ def test_typerror
11
+ assert_raises(TypeError) do
12
+ assert_equal(1.usd, 1.try.to(:usd))
13
+ end
14
+ end
15
+
16
+ def test_cex
17
+ e = 1.twd().unit
18
+ r = 1.usd(:cex).to(twd(:cex))
19
+ assert_equal( e, r.unit )
20
+ end
21
+
22
+ def test_usd_to_tws
23
+ assert_equal( 1.tws, 1.usd.to(tws) )
24
+ end
25
+
26
+ end
@@ -0,0 +1,205 @@
1
+ $:.unshift(File.dirname(__FILE__) + '../lib')
2
+
3
+ require 'test/unit'
4
+ require 'van/units'
5
+
6
+ class TestUnits < Test::Unit::TestCase
7
+
8
+ include Van::Units
9
+
10
+ def assert_in_unit_delta(v1, v2, d)
11
+ assert( v2 * (1 - d) <= v1 && v1 <= v2 * (1 + d), "<#{v1}> expected but was\n<#{v2}>" )
12
+ end
13
+
14
+ def test_bit_and_bytes
15
+ assert_equal( 65.bit/s, 1.bit/s + 8.bytes/s )
16
+ assert_equal( 0.125.byte/s, ((1.bit/s).to(byte/s)) )
17
+ # PETER : Why 0?
18
+ # assert_equal( 0, ((1.bit/s).to(byte/s)) )
19
+ end
20
+
21
+ def miles_to_feet
22
+ assert_equal( 5000.feet, 1.mile.to(feet) )
23
+ end
24
+
25
+ def test_acre_to_yards_squared
26
+ assert_equal( 4840.sq_yd, 1.acre.to(yd**2) )
27
+ # PETER : This obviously fails because yards and square yards are not the same.
28
+ # assert_equal( 4840.yd, 1.acre.to(yd**2) )
29
+ end
30
+
31
+ def test_acre_to_sq_yd
32
+ assert_equal( 4840.sq_yd, 1.acre.to(sq_yd) )
33
+ end
34
+
35
+ def test_gallon_to_liter
36
+ assert_in_unit_delta( 3.785411784.L, 1.gallon.to(L), 1e-15 )
37
+ # PETER : A gallon expressed in liters is not a liter.
38
+ # assert_equal( 1.L, 1.gallon.to(L) )
39
+ end
40
+
41
+ def test_lb_to_kg
42
+ assert_equal( 0.45359237.kg, 1.lb.to(kg) )
43
+ end
44
+
45
+ def test_m_s_to_m_s
46
+ assert_equal( 1.m.s, 1.m.s.to(m.s) )
47
+ end
48
+
49
+ def test_sq_mi_to_km_squared
50
+ assert_in_unit_delta( 2.589988110336.to_value(km**2), 1.sq_mi.to(km**2), 1e-15 )
51
+ # PETER : See comment in #test_bandwidth.
52
+ # assert_equal( 1.to(km**2), 1.sq_mi.to(km**2) )
53
+ end
54
+
55
+ def test_mile_to_km
56
+ assert_in_unit_delta( 1.609344.km, 1.mile.to(km), 1e-15 )
57
+ # PETER : These are floats, they don't compare.
58
+ # assert_equal( 1.609344.km, 1.mile.to(km) )
59
+ end
60
+
61
+ def test_with_unit_convertor_uk
62
+ with_unit_converter(:uk) {
63
+ assert_equal( 112.lb, 1.cwt.to(lb) )
64
+ }
65
+ assert_equal( 112.lb(:uk), 1.cwt(:uk).to(lb(:uk)) )
66
+ # PETER : That would be a very small hundredweight
67
+ # assert_equal( 1.lb(:uk), 1.cwt(:uk).to(lb(:uk)) )
68
+ end
69
+
70
+ def test_with_unit_convertor_us
71
+ with_unit_converter(:us) {
72
+ assert_equal( 100.lb, 1.cwt.to(lb) )
73
+ }
74
+ assert_equal( 100.lb(:us), 1.cwt(:us).to(lb(:us)) )
75
+ # PETER : That would be a very small hundredweight
76
+ # assert_equal( 1.lb(:us), 1.cwt(:us).to(lb(:us)) )
77
+ end
78
+
79
+ def test_current_lb
80
+ assert_equal( lb, Converter.current.lb )
81
+ # PETER : compares values and units again.
82
+ # assert_equal( 1.lb, Converter.current.lb )
83
+ end
84
+
85
+ def test_cwt
86
+ assert_equal( 1.12.cwt(:us), 1.cwt(:uk).to(cwt(:us)) )
87
+ assert_equal( 1.cwt(:uk), 1.12.cwt(:us).to(cwt(:uk)) )
88
+ end
89
+
90
+ def test_unit_convertor_cwt
91
+ with_unit_converter(:uk) {
92
+ assert_equal( 1.cwt, 1.cwt.to(cwt(:us)) )
93
+ assert_equal( 1.cwt(:us), 1.cwt(:us).to(cwt) )
94
+ # PETER : This is not correct. The conversion does
95
+ # not change the magnitude of the thing, i.e.,
96
+ # something that weighs a hundredweight UK style does
97
+ # not weigh a hundredweight US style even when its
98
+ # weight us expressed in US style hundredweights
99
+ # (if that makes sense.)
100
+ # Also, this converter has no effect because the
101
+ # converter is specified everywhere. So it is really
102
+ # the same as the above.
103
+ # assert_equal( 1.cwt(:us), 1.cwt(:uk).to(cwt(:us)) )
104
+ # assert_equal( 1.cwt(:uk), 1.cwt(:us).to(cwt(:uk)) )
105
+ }
106
+ end
107
+
108
+ #def test_registered_convertors
109
+ # assert_equal( [:cex, :default, :uk, :us], Converter.registered_converters.sort )
110
+ #end
111
+
112
+ def test_spaceship
113
+ assert_nil( 1.m <=> 1.L )
114
+ # PETER : meter and liter are like apples and oranges.
115
+ # returns nil instead.
116
+ # assert_equal( 0, (1.m <=> 1.L) )
117
+ assert_equal( 1, (1.m <=> 1.cm) )
118
+ end
119
+
120
+ def test_bandwidth
121
+ assert_equal( 1024.to_value(kB / s), ((1.MB / s).to(kB / s)) )
122
+ with_unit_converter(:binary_iec_base) {
123
+ assert_equal( 1000.to_value(kB / s), ((1.MB / s).to(kB / s)) )
124
+ }
125
+ # PETER : The #to method does conversion from one unit
126
+ # to another compatible unit. Although it could make
127
+ # sense to convert a unitless number to a unit
128
+ # that is essentially unitless too (like kB/MB), there
129
+ # are better ways to do the same, and the below still
130
+ # wouldn't work because 1 is unitless and 1.kB/s is not.
131
+ # I've added a to_value method for this.
132
+ # assert_equal( 1.to(kB / s), ((1.MB / s).to(kB / s)) )
133
+ # with_unit_converter(:binary_iec_base) {
134
+ # assert_equal( 1.to(kB / s), ((1.MB / s).to(kB / s)) )
135
+ # }
136
+ end
137
+
138
+ def test_to_unit
139
+ assert_equal( m/s, "m / s".to_unit )
140
+ # PETER : m/s and 1 m/s are not the same thing.
141
+ # assert_equal( 1.to(m / s), "m / s".to_unit )
142
+ end
143
+
144
+ def test_to_value
145
+ assert_equal( 1.m/s, "1 m / s".to_value )
146
+ # PETER : See remark in #test_bandwidth.
147
+ # assert_equal( 1.to(m / s), "1 m / s".to_value )
148
+ end
149
+
150
+ def test_simplify
151
+ # PETER : Don't see how this could be zero. Don't know
152
+ # exactly what you want to test though...
153
+ # assert_equal( 0, "1 m / cm L".to_value.simplify )
154
+ end
155
+
156
+ def test_float
157
+ assert_equal( 100.0, "1 m / cm".to_value.to_f )
158
+ end
159
+
160
+ def test_m_to_str_cm
161
+ assert_equal( 100.cm, 1.m.to("cm") )
162
+ end
163
+
164
+ def test_m_plus_str_cm
165
+ assert_equal( 105.cm, 1.m + "5cm" )
166
+ end
167
+
168
+ def test_plus
169
+ assert_equal( 1.05.m, 1.m + 5.cm )
170
+ end
171
+
172
+ def test
173
+ assert_equal( 105.m, 5.cm + 1.m )
174
+ end
175
+
176
+ # PETER : This compares a value (quantity + unit) to a unit. This could
177
+ # be allowed by saying that the unit of meter and 1 meter is the
178
+ # same, but personally I'd like to keep the distinction.
179
+ # def test_
180
+ # assert_equal( 101.cm, cm * m )
181
+ # end
182
+
183
+ # def test_
184
+ # assert_equal( 101.cm, cm * "m" )
185
+ # end
186
+
187
+ def test_value
188
+ assert_equal(-5.mm, "-5mm".to_value )
189
+ # PETER : -5mm is not the same as -5
190
+ # assert_equal(-5, "-5mm".to_value )
191
+ end
192
+
193
+ def test_value_abs
194
+ assert_equal(5.mm, "-5mm".to_value.abs )
195
+ # PETER : 5mm is not the same as 5
196
+ # assert_equal(5, "-5mm".to_value.abs )
197
+ end
198
+
199
+ def test_infinite?
200
+ # PETER : I'm not sure what the point of this is, but in
201
+ # any case, a Value does not have an infinite? method
202
+ # assert_equal( false, ("5.0mm".to_value / 1).infinite? )
203
+ end
204
+
205
+ end