phys-units 0.9.2 → 0.9.3

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: 562362594d0457c4492bc6a2467e2ee271c81dab
4
- data.tar.gz: 86d22f70ec1f1962631fde49b63fcf72e408650b
3
+ metadata.gz: c123870463b0713958805751bbcef484dc35d1dc
4
+ data.tar.gz: 8a7bd323be840936ea2b3535b8a5bf6e312f5b4d
5
5
  SHA512:
6
- metadata.gz: 1124141150ac160dcd3568ba3507e0cf86d1521ba80e49a7c6307b7130710943fa1e7452b3f0df54fe2bc885f67cd690f3f433ecdd7f28605897d3736cd37060
7
- data.tar.gz: d92bfe06366359ff027c0eb599d2bd8c78c7795a8c4743789c017bdb23b6d9725e0c2a1d46ca30815f0f854f817c012e6ed546db680b4bc7fa75ed97c136ca4f
6
+ metadata.gz: efbbc5b61cb7a9eebf65f0e2420073365750321f502b24aeecc45afa9cc42f01b6a895f126383e4a3cf49e3cf8178a230602c3fed460a6d7ebcda678e756083a
7
+ data.tar.gz: f1657606f20fadf5ff43744261b334322538ede36f93d89921f9afd7cbc68af5de040a67b2ccb0d56fda09e5a08afce03c9250fdcd27bc76052bbe4a46002bcf
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Phys-Units
2
2
 
3
- GNU Units-compatible library for Ruby.
3
+ GNU Units-compatible library for Ruby.
4
4
  Former name is [Quanty](http://narray.rubyforge.org/quanty/quanty-en.html),
5
5
  the first Ruby units library released in 2001.
6
6
 
@@ -8,15 +8,16 @@ the first Ruby units library released in 2001.
8
8
  is the primary class of Phys-Units library, to be manipulated by users.
9
9
  It contains:
10
10
 
11
- * *Value* :
12
- +value+ must be a class instance having arithmetic methods,
11
+ * *Value*
12
+ must be a class instance having arithmetic methods,
13
13
  but it is not necessary to be a Numeric.
14
- This is a duck typing way.
15
- * *Unit* :
16
- +unit+ is a instance of Phys::Unit class
17
- obtained by parsing a +expr+ string.
14
+ This is a duck typing way.
15
+ * *Unit*
16
+ is an instance of Phys::Unit class
17
+ obtained by parsing *expr* string.
18
18
 
19
- See Documentation at [Rubygems Site](https://rubygems.org/gems/phys-units)
19
+ See [Phys::Quantity Documentation](http://rubydoc.info/gems/phys-units/frames/Phys/Quantity)
20
+ for more details.
20
21
 
21
22
  ## Installation
22
23
 
@@ -47,15 +48,16 @@ by the following features:
47
48
 
48
49
  * Compatible with GNU Units except nonlinear units.
49
50
  * Provides 2331 units, 85 prefixes, including UTF-8 unit names.
50
- * All units are defined in a unit data file from GNU Units
51
- and not defined as a Ruby codes, except temperature definitions.
52
- * No addition or modification to Ruby standard classes by default,
51
+ * Defines Units by reading GNU Units text data,
52
+ unlike other libraries which define Units in Ruby code.
53
+ * No addition or modification to Ruby standard classes,
53
54
  avoiding conflict with other libraries.
54
55
  * Calculation of values is through Ruby Numeric arithmetic methods.
55
- None of the Phys-Units lib's buisiness.
56
- * Conversion factors are held in Rational even defined
57
- in the decimal form such as `1.0e10'.
56
+ Phys-Units does not care it.
57
+ * Conversion factors are internally held in Rational form even
58
+ defined as the decimal form such as `1.0e10'.
58
59
  * PI number has a dimension.
60
+ * Japanese Units are available by require 'phys/units/jp'.
59
61
 
60
62
  ## Platforms tested
61
63
 
@@ -1,10 +1,5 @@
1
1
  module Phys
2
+ # Error in Unit handling
2
3
  class UnitError < StandardError
3
4
  end
4
- class UnitParseError < UnitError
5
- end
6
- class UnitConversionError < UnitError
7
- end
8
- class UnitOperationError < UnitError
9
- end
10
5
  end
@@ -0,0 +1,169 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "phys/units"
3
+
4
+ Phys::Unit.import_units <<EOL
5
+ !utf8
6
+ # Japanese Number units
7
+ 一- 1
8
+ 二- 2
9
+ 三- 3
10
+ 四- 4
11
+ 五- 5
12
+ 六- 6
13
+ 七- 7
14
+ 八- 8
15
+ 九- 9
16
+ 十- 10
17
+ 百- 10^2
18
+ 千- 10^3
19
+ 万- 10^4
20
+ 億- 10^8
21
+ 兆- 10^12
22
+ 京- 10^16
23
+ 垓- 10^20
24
+ 秭- 10^24
25
+ 秭- 10^24
26
+ 穰- 10^28
27
+ 溝- 10^32
28
+ 澗- 10^36
29
+ 正- 10^40
30
+ 載- 10^44
31
+ 極- 10^48
32
+ 恒河沙- 10^52
33
+ 阿僧祇- 10^56
34
+ 那由他- 10^60
35
+ 不可思議- 10^64
36
+ 無量大数- 10^68
37
+ #
38
+ 一 1
39
+ 二 2
40
+ 三 3
41
+ 四 4
42
+ 五 5
43
+ 六 6
44
+ 七 7
45
+ 八 8
46
+ 九 9
47
+ 十 10
48
+ 百 10^2
49
+ 千 10^3
50
+ 万 10^4
51
+ 億 10^8
52
+ 兆 10^12
53
+ 京 10^16
54
+ 垓 10^20
55
+ 秭 10^24
56
+ 秭 10^24
57
+ 穰 10^28
58
+ 溝 10^32
59
+ 澗 10^36
60
+ 正 10^40
61
+ 載 10^44
62
+ 極 10^48
63
+ 恒河沙 10^52
64
+ 阿僧祇 10^56
65
+ 那由他 10^60
66
+ 不可思議 10^64
67
+ 無量大数 10^68
68
+ #
69
+ 割- 1|10
70
+ 分- 1|100
71
+ 厘- 1|1000
72
+ 釐- 1|1000
73
+ 毛- 1|10^4
74
+ 毫- 1|10^4
75
+ 糸- 1|10^4
76
+ 絲- 1|10^4
77
+ 忽- 1|10^5
78
+ 微- 1|10^6
79
+ 繊- 1|10^7
80
+ 沙- 1|10^8
81
+ 塵- 1|10^9
82
+ 埃- 1|10^10
83
+ 渺- 1|10^11
84
+ 漠- 1|10^12
85
+ 模糊- 1|10^13
86
+ 逡巡- 1|10^14
87
+ 須臾- 1|10^15
88
+ 瞬息- 1|10^16
89
+ 弾指- 1|10^17
90
+ 刹那- 1|10^18
91
+ 六徳- 1|10^19
92
+ 虚空- 1|10^20
93
+ 清浄- 1|10^21
94
+ 阿頼耶- 1|10^22
95
+ 阿摩羅- 1|10^23
96
+ 涅槃寂静- 1|10^24
97
+ #
98
+ 割 1|10
99
+ 分 1|100
100
+ 厘 1|1000
101
+ 釐 1|1000
102
+ 毛 1|10^4
103
+ 毫 1|10^4
104
+ 糸 1|10^4
105
+ 絲 1|10^4
106
+ 忽 1|10^5
107
+ 微 1|10^6
108
+ 繊 1|10^7
109
+ 沙 1|10^8
110
+ 塵 1|10^9
111
+ 埃 1|10^10
112
+ 渺 1|10^11
113
+ 漠 1|10^12
114
+ 模糊 1|10^13
115
+ 逡巡 1|10^14
116
+ 須臾 1|10^15
117
+ 瞬息 1|10^16
118
+ 弾指 1|10^17
119
+ 刹那 1|10^18
120
+ 六徳 1|10^19
121
+ 虚空 1|10^20
122
+ 清浄 1|10^21
123
+ 阿頼耶 1|10^22
124
+ 阿摩羅 1|10^23
125
+ 涅槃寂静 1|10^24
126
+ #
127
+ # Traditional Japanese units (shakkanhou)
128
+ #
129
+ # Japanese Length Measures
130
+ 尺 shaku # = 10/33 m ≒ 0.3030303 m
131
+ 寸 1|10 尺
132
+ 里 36 町 # ≒ 3927.272727 m
133
+ 町 60 間 # ≒ 109.090909 m
134
+ 間 6 尺 # ≒ 1.818182 m
135
+ 丈 10 尺 # ≒ 3.030303 m
136
+ 曲尺 尺
137
+ 鯨尺 kujirajaku # 10|8 shaku
138
+ #
139
+ # Japanese Area Measures
140
+ 坪 tsubo # = 400/121 m^2 ≒ 3.305785 m^2
141
+ 畝 30 坪 # ≒ 99.173554 m^2
142
+ 反 10 畝 # ≒ 991.7355 m^2
143
+ 段 反
144
+ #歩 坪
145
+ #町 10 反 # ≒ 9917.355 m^2
146
+ #合 1|10 坪 # ≒ 0.3305785 m^2
147
+ #勺 1|100 坪
148
+ #
149
+ # Japanese architecture is based on a "standard" size of tatami mat.
150
+ 江戸間 edoma # (5.8*2.9) shaku^2
151
+ 京間 kyouma # (6.3*3.15) shaku^2
152
+ 中京間 chuukyouma # (6*3) shaku^2
153
+ 畳 tatami
154
+ #
155
+ # Japanese Volume Measures
156
+ 升 shou # 10合 = 2401/1331 l ≒ 1.803906837 l
157
+ 斗 10 升 # ≒ 18.039068 l
158
+ 石 10 斗 # ≒ 180.390684 l
159
+ 合 1|10 升 # 10 勺 ≒ 0.180390684 l
160
+ 勺 1|10 合
161
+ #
162
+ # Japanese Weight Measures
163
+ 貫 1000 匁 # = 100両 = 3.75 kg
164
+ 斤 160 匁 # = 600 g
165
+ 両 10 匁 # = 37.5 g
166
+ 匁 momme # = 3.75 g
167
+ #
168
+ !endutf8
169
+ EOL
@@ -4526,7 +4526,7 @@ pfund 500 g
4526
4526
  #
4527
4527
  # If you would like to help expand this section and understand
4528
4528
  # cyrillic transliteration, let me know. These measures are meant to
4529
- # reflect common usage, e.g. in translated literature.
4529
+ # reflect common usage, e.g. in translated literature.
4530
4530
  #
4531
4531
 
4532
4532
  dessiatine 2400 sazhen^2 # Land measure
@@ -5294,10 +5294,9 @@ EOL
5294
5294
 
5295
5295
  module Phys
5296
5296
  # define pi
5297
- Unit.define("pi","!dimensionless",Math::PI)
5297
+ BaseUnit.define("pi","!dimensionless",Math::PI)
5298
5298
 
5299
5299
  # define tempC and tempK
5300
- #Phys::OffsetUnit.import_temperature
5301
5300
  zero_degc = Unit["stdtemp"].conversion_factor
5302
5301
  OffsetUnit.define( "tempC", Unit["K"], zero_degc )
5303
5302
  OffsetUnit.define( "tempF", Unit["K"]*Rational(5,9), zero_degc-32*Rational(5,9) )
@@ -1,5 +1,11 @@
1
1
  require "phys/units"
2
2
 
3
+ # ActiveSupport-like mix-in to Numeric.
4
+ # This is activated by:
5
+ # require 'phys/units/mixin'
6
+ #
7
+ # However, this kind of global change will cause unexpected problems.
8
+
3
9
  class Numeric
4
10
  alias method_missing_orig method_missing
5
11
  def method_missing(method, *args, &block)
@@ -450,13 +450,13 @@ end
450
450
  ###### racc/parser.rb end
451
451
 
452
452
 
453
- # -*- coding: utf-8 -*-
454
453
  # parse.y, parse.rb
455
454
  #
456
455
  # by Masahiro Tanaka <masa16.tanaka@gmail.com>
457
456
  #
458
457
  module Phys
459
458
  class Unit
459
+ # @visibility private
460
460
  class Parse < Racc::Parser
461
461
 
462
462
  module_eval(<<'...end parse.y/module_eval...', 'parse.y', 63)
@@ -480,10 +480,10 @@ module_eval(<<'...end parse.y/module_eval...', 'parse.y', 63)
480
480
  end
481
481
 
482
482
  def parse( str )
483
- return Unit.new(str) if str.empty?
483
+ return Unit.new(1) if str.empty?
484
484
  @q = []
485
485
 
486
- c = Unit.unit_chars
486
+ c = Unit.unit_exclude_chars
487
487
 
488
488
  while str.size > 0 do
489
489
  case str
@@ -677,7 +677,7 @@ Racc_debug_parser = false
677
677
 
678
678
  module_eval(<<'.,.,', 'parse.y', 23)
679
679
  def _reduce_2(val, _values, result)
680
- result = Unit.inv(val[1])
680
+ result = Unit.inverse(val[1])
681
681
  result
682
682
  end
683
683
  .,.,
@@ -21,7 +21,7 @@ class Parse
21
21
  rule
22
22
 
23
23
  target: expr
24
- | DIV list { result = Unit.inv(val[1]) }
24
+ | DIV list { result = Unit.inverse(val[1]) }
25
25
  ;
26
26
 
27
27
  expr: list
@@ -30,10 +30,10 @@ rule
30
30
  | expr '-' expr { result = val[0] - val[2] }
31
31
  | expr '*' expr { result = val[0] * val[2] }
32
32
  | expr DIV expr { result = val[0] / val[2] }
33
- ;
33
+ ;
34
34
 
35
35
  numexpr: NUMBER
36
- | numexpr '|' numexpr { result = Unit.rdiv(val[0],val[2]) }
36
+ | numexpr '|' numexpr { result = Unit.rdiv(val[0],val[2]) }
37
37
  ;
38
38
 
39
39
  pexpr: '(' expr ')' { result = val[1] }
@@ -52,13 +52,13 @@ end
52
52
 
53
53
  ---- header ----
54
54
 
55
- # -*- coding: utf-8 -*-
56
55
  # parse.y, parse.rb
57
56
  #
58
57
  # by Masahiro Tanaka <masa16.tanaka@gmail.com>
59
58
  #
60
59
  module Phys
61
60
  class Unit
61
+ # @visibility private
62
62
  ---- inner ----
63
63
 
64
64
  def build_num(ov,ud,pw)
@@ -80,10 +80,10 @@ class Unit
80
80
  end
81
81
 
82
82
  def parse( str )
83
- return Unit.new(str) if str.empty?
83
+ return Unit.new(1) if str.empty?
84
84
  @q = []
85
85
 
86
- c = Unit.unit_chars
86
+ c = Unit.unit_exclude_chars
87
87
 
88
88
  while str.size > 0 do
89
89
  case str
@@ -10,9 +10,9 @@
10
10
  module Phys
11
11
 
12
12
  # alias to Phys::Quantity.new
13
- def Quantity(*a)
14
- Quantity.new(*a)
15
- end
13
+ #def Quantity(*a)
14
+ # Quantity.new(*a)
15
+ #end
16
16
 
17
17
  # Phys::Quantity is a class to represent physical quantities
18
18
  # with unit of measure.
@@ -20,7 +20,7 @@ module Phys
20
20
  # * *Value* of the quantity.
21
21
  # +value+ must be a class instance having arithmetic methods,
22
22
  # but it is not necessary to be a Numeric.
23
- # This is a duck typing way.
23
+ # This is a duck typing way.
24
24
  # * *Unit* of the quantity.
25
25
  # +unit+ is a instance of Phys::Unit class
26
26
  # obtained by parsing a +expr+ string.
@@ -43,7 +43,7 @@ module Phys
43
43
  # @param [String] expr a string of unit expression.
44
44
  # If +expr+ is not supplied, it becomes dimeinsionless.
45
45
  # @return [Phys::Quantity]
46
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
46
+ # @raise [Phys::UnitError] if unit conversion is failed.
47
47
  def [](value,expr=nil)
48
48
  self.new(value,expr)
49
49
  end
@@ -55,10 +55,10 @@ module Phys
55
55
  # @param [String] expr a string of unit expression.
56
56
  # If +expr+ is not supplied, it becomes dimeinsionless.
57
57
  # @param [Phys::Unit] unit (optional)
58
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
58
+ # @raise [Phys::UnitError] if unit conversion is failed.
59
59
  #
60
60
  def initialize(value,expr=nil,unit=nil)
61
- @val = value
61
+ @value = value
62
62
  expr = expr.to_s if Symbol===expr
63
63
  @expr = (expr=='') ? nil : expr
64
64
  @unit = unit
@@ -69,20 +69,24 @@ module Phys
69
69
  end
70
70
  end
71
71
 
72
- # @return [Object] value of the quantity
73
- attr_reader :val
74
- alias value val
72
+ # Value of the quantity.
73
+ # Instance of classes with same arithmetic methods as Numric.
74
+ # @return [Object]
75
+ attr_reader :value
76
+ alias val value
75
77
 
76
- # @return [String] unit expression
78
+ # Unit expression. Given as the second parameter of the Quantity constructor.
79
+ # @return [String]
77
80
  attr_reader :expr
78
81
 
79
- # @return [Phys::Unit] unit
82
+ # Unit of the quantity. Instance of Phys::Unit class.
83
+ # @return [Phys::Unit]
80
84
  attr_reader :unit
81
85
 
82
86
  # Conversion to a quantity in another unit.
83
87
  # @param [String] expr unit expression.
84
88
  # @return [Phys::Quantity] quantity in the unit of +expr+.
85
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
89
+ # @raise [Phys::UnitError] if unit conversion is failed.
86
90
  #
87
91
  def want(expr)
88
92
  unit = Unit.parse(expr)
@@ -99,10 +103,10 @@ module Phys
99
103
  # both params must be dimensionless.
100
104
  # @param [Object] other
101
105
  # @return [Phys::Quantity] a quantity in the unit of +self+.
102
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
106
+ # @raise [Phys::UnitError] if unit conversion is failed.
103
107
  #
104
108
  def +(other)
105
- val = @val + @unit.convert_scale(other)
109
+ val = @value + @unit.convert_scale(other)
106
110
  self.class.new( val, @expr, @unit )
107
111
  end
108
112
 
@@ -114,125 +118,145 @@ module Phys
114
118
  # both params must be dimensionless.
115
119
  # @param [Object] other
116
120
  # @return [Phys::Quantity] a quantity in the unit of +self+.
117
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
121
+ # @raise [Phys::UnitError] if unit conversion is failed.
118
122
  #
119
123
  def -(other)
120
- val = @val - @unit.convert_scale(other)
124
+ val = @value - @unit.convert_scale(other)
121
125
  self.class.new( val, @expr, @unit )
122
126
  end
123
127
 
124
- # @return [Phys::Quantity] Abs. in the unit of +self+.
128
+ # Absolute. Returns a quantity in the same unit of +self+.
129
+ # @return [Phys::Quantity]
125
130
  def abs
126
- self.class.new( @val.abs, @expr, @unit )
131
+ self.class.new( @value.abs, @expr, @unit )
127
132
  end
128
133
 
129
- # @return [Phys::Quantity] Abs2. in the squared unit of +self+.
134
+ # Square. Returns a quantity in squared unit of +self+.
135
+ # @return [Phys::Quantity]
130
136
  def abs2
131
137
  self**2
132
138
  end
133
139
 
134
- # @return [Phys::Quantity] Ceil. in the unit of +self+.
140
+ # Ceil. Returns a quantity with the smallest Integer value greater
141
+ # than or equal to +self+ value, in the same unit of +self+.
142
+ # @return [Phys::Quantity]
135
143
  def ceil
136
- self.class.new( @val.ceil, @expr, @unit )
144
+ self.class.new( @value.ceil, @expr, @unit )
137
145
  end
138
146
 
139
- # @return [Phys::Quantity] Round. in the unit of +self+.
140
- def round
141
- self.class.new( @val.round, @expr, @unit )
147
+ # Round. Rounds +self+ value to a given precision in decimal digits
148
+ # (default 0 digits).
149
+ # Returns a quantity with the rounded value in the same unit of +self+.
150
+ # @return [Phys::Quantity]
151
+ def round(ndigits=nil)
152
+ val = ndigits ? @value.round(ndigits) : @value.round
153
+ self.class.new( val, @expr, @unit )
142
154
  end
143
155
 
144
- # @return [Phys::Quantity] Floor. in the unit of +self+.
156
+ # Floor. Returns a quantity with the largest integer value
157
+ # less than or equal to +self+ value, in the same unit of +self+.
158
+ # @return [Phys::Quantity]
145
159
  def floor
146
- self.class.new( @val.floor, @expr, @unit )
160
+ self.class.new( @value.floor, @expr, @unit )
147
161
  end
148
162
 
149
- # @return [Phys::Quantity] Truncate. in the unit of +self+.
163
+ # Truncate. Returns a quantity with the value truncated to an integer,
164
+ # in the same unit of +self+.
165
+ # @return [Phys::Quantity]
150
166
  def truncate
151
- self.class.new( @val.truncate, @expr, @unit )
167
+ self.class.new( @value.truncate, @expr, @unit )
152
168
  end
153
169
 
154
- # Unary Plus.
155
- # @return [Phys::Quantity] +self+.
170
+ # Unary Plus. Returns +self+.
171
+ # @return [Phys::Quantity]
156
172
  def +@
157
- self.class.new( @val, @expr, @unit )
173
+ self.class.new( @value, @expr, @unit )
158
174
  end
159
175
 
160
- # Unary Minus.
161
- # @return [Phys::Quantity] in the unit of +self+.
176
+ # Unary Minus. Returns a quantity with negative value
177
+ # in the same unit of +self+.
178
+ # @return [Phys::Quantity]
162
179
  def -@
163
- self.class.new( -@val, @expr, @unit )
180
+ self.class.new( -@value, @expr, @unit )
164
181
  end
165
182
 
166
183
  # Comparison of quantities.
167
184
  # Before the comparison, it converts +other+ to the unit of +self+.
168
185
  # @param [Phys::Quantity] other
169
186
  # @return [Integer]
170
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
187
+ # @raise [Phys::UnitError] if unit conversion is failed.
171
188
  def <=> (other)
172
- @val <=> @unit.convert(other)
189
+ @value <=> @unit.convert(other)
173
190
  end
174
191
 
175
- # Comparison. Returns +true+ if +self+ has the same value as +other+.
192
+ # Equality. Returns +true+ if +self+ has the same value as +other+.
176
193
  # Before the comparison, it converts +other+ to the unit of +self+.
177
194
  # @param [Phys::Quantity] other
178
195
  # @return [Boolean]
179
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
180
- def == (other)
181
- @val == @unit.convert(other)
196
+ def == (other)
197
+ if Quantity===other
198
+ @unit.conformable?(other.unit) &&
199
+ @value == @unit.convert(other)
200
+ elsif @unit.dimensionless?
201
+ @unit.convert_value_to_base_unit(@value) == other
202
+ else
203
+ false
204
+ end
182
205
  end
183
206
 
184
207
  # Comparison. Returns +true+ if +self+ is greather-than or equal-to +other+.
185
208
  # Before the comparison, it converts +other+ to the unit of +self+.
186
209
  # @param [Phys::Quantity] other
187
210
  # @return [Boolean]
188
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
189
- def >= (other)
190
- @val >= @unit.convert(other)
211
+ # @raise [Phys::UnitError] if unit conversion is failed.
212
+ def >= (other)
213
+ @value >= @unit.convert(other)
191
214
  end
192
215
 
193
216
  # Comparison. Returns +true+ if +self+ is less-than or equal-to +other+.
194
217
  # Before the comparison, it converts +other+ to the unit of +self+.
195
218
  # @param [Phys::Quantity] other
196
219
  # @return [Boolean]
197
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
198
- def <= (other)
199
- @val <= @unit.convert(other)
220
+ # @raise [Phys::UnitError] if unit conversion is failed.
221
+ def <= (other)
222
+ @value <= @unit.convert(other)
200
223
  end
201
224
 
202
225
  # Comparison. Returns +true+ if +self+ is less than +other+.
203
226
  # Before the comparison, it converts +other+ to the unit of +self+.
204
227
  # @param [Phys::Quantity] other
205
228
  # @return [Boolean]
206
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
207
- def < (other)
208
- @val < @unit.convert(other)
229
+ # @raise [Phys::UnitError] if unit conversion is failed.
230
+ def < (other)
231
+ @value < @unit.convert(other)
209
232
  end
210
233
 
211
234
  # Comparison. Returns +true+ if +self+ is greater than +other+.
212
235
  # Before the comparison, it converts +other+ to the unit of +self+.
213
236
  # @param [Phys::Quantity] other
214
237
  # @return [Boolean]
215
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
216
- def > (other)
217
- @val > @unit.convert(other)
238
+ # @raise [Phys::UnitError] if unit conversion is failed.
239
+ def > (other)
240
+ @value > @unit.convert(other)
218
241
  end
219
242
 
220
- # Closeness. Returns +true+ if +self+ and +other+ is close to each other
221
- # within +epsilon+ relative to the absolute values.
243
+ # Closeness. Returns +true+ if difference between +self+ and +other+ is
244
+ # smaller than +epsilon+ times sum of their absolute values.
222
245
  # Before the comparison, it converts +other+ to the unit of +self+.
223
246
  # @param [Phys::Quantity] other
224
247
  # @param [Numeric] epsilon
225
248
  # @return [Boolean]
226
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
249
+ # @raise [Phys::UnitError] if unit conversion is failed.
227
250
  def close_to(other,epsilon=Float::EPSILON)
228
251
  other_value = @unit.convert(other)
229
- (@val-other_value).abs/(@val.abs+other_value.abs) <= epsilon
252
+ abs_sum = @value.abs+other_value.abs
253
+ abs_sum==0 || (@value-other_value).abs/abs_sum <= epsilon
230
254
  end
231
255
 
232
- # Power of a quantity.
256
+ # Exponentiation.
233
257
  # @param [Numeric] n
234
258
  # @return [Phys::Quantity] a quantity in the +n+ -powered unit of +self+.
235
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
259
+ # @raise [Phys::UnitError] if unit conversion is failed.
236
260
  def **(n)
237
261
  if @expr.nil?
238
262
  expr = nil
@@ -241,11 +265,11 @@ module Phys
241
265
  else
242
266
  expr = '('+@expr+')^'+n.to_s+''
243
267
  end
244
- self.class.new( @val**n, expr, @unit**n )
268
+ self.class.new( @value**n, expr, @unit**n )
245
269
  end
246
270
 
247
271
  # (internally used method)
248
- # @private
272
+ # @visibility private
249
273
  # @return [String]
250
274
  def enclose_expr #:nodoc: used internally
251
275
  return nil if @expr.nil?
@@ -255,9 +279,9 @@ module Phys
255
279
  @expr
256
280
  end
257
281
  end
258
-
282
+
259
283
  # (internally used method)
260
- # @private
284
+ # @visibility private
261
285
  # @return [String]
262
286
  def enclose_expr_div #:nodoc: used internally
263
287
  return nil if @expr.nil?
@@ -271,54 +295,54 @@ module Phys
271
295
  # Multiplication.
272
296
  # If the +other+ param is *not* Phys::Quantity,
273
297
  # +other+ is regarded as a dimensionless value.
298
+ # The values and units are multiplied respectively.
274
299
  # @param [Object] other
275
300
  # @return [Phys::Quantity] a quantity
276
- # both the values and units are multiplied respectively.
277
- # @raise [Phys::UnitOperationError] if unit is not operable.
301
+ # @raise [Phys::UnitError] if unit is not operable.
278
302
  #
279
303
  def *(other)
280
304
  if Quantity===other
281
305
  a = [self.enclose_expr, other.enclose_expr]
282
306
  a.delete(nil)
283
- self.class.new( @val*other.val, a.join(' '), @unit*other.unit )
307
+ self.class.new( @value*other.value, a.join(' '), @unit*other.unit )
284
308
  else
285
- self.class.new( @val*other, @expr, @unit )
309
+ self.class.new( @value*other, @expr, @unit )
286
310
  end
287
311
  end
288
312
 
289
313
  # Division.
290
314
  # If the +other+ param is *not* Phys::Quantity,
291
315
  # +other+ is regarded as a dimensionless value.
316
+ # The values and units are divided respectively.
292
317
  # @param [Object] other
293
318
  # @return [Phys::Quantity] a quantity
294
- # both the values and units are divided respectively.
295
- # @raise [Phys::UnitOperationError] if unit is not operable.
319
+ # @raise [Phys::UnitError] if unit is not operable.
296
320
  #
297
321
  def /(other)
298
322
  if Quantity===other
299
323
  a = [self.enclose_expr, other.enclose_expr_div]
300
324
  a.delete(nil)
301
- self.class.new( @val/other.val, a.join, @unit/other.unit )
325
+ self.class.new( @value/other.value, a.join, @unit/other.unit )
302
326
  else
303
- self.class.new( @val/other, @expr, @unit )
327
+ self.class.new( @value/other, @expr, @unit )
304
328
  end
305
329
  end
306
330
 
307
331
  # Division more correctly.
308
332
  # If the +other+ param is *not* Phys::Quantity,
309
333
  # +other+ is regarded as a dimensionless value.
334
+ # The values and units are divided respectively.
310
335
  # @param [Object] other
311
336
  # @return [Phys::Quantity] a quantity
312
- # both the values and units are divided respectively.
313
- # @raise [Phys::UnitOperationError] if unit is not operable.
337
+ # @raise [Phys::UnitError] if unit is not operable.
314
338
  #
315
339
  def quo(other)
316
340
  if Quantity===other
317
341
  a = [self.enclose_expr, other.enclose_expr_div]
318
342
  a.delete(nil)
319
- self.class.new( @val.quo(other.val), a.join, @unit/other.unit )
343
+ self.class.new( @value.quo(other.value), a.join, @unit/other.unit )
320
344
  else
321
- self.class.new( @val.quo(other), @expr, @unit )
345
+ self.class.new( @value.quo(other), @expr, @unit )
322
346
  end
323
347
  end
324
348
  alias fdiv quo
@@ -332,13 +356,13 @@ module Phys
332
356
  # and returns +div+ of Phys::Quantity.
333
357
  # @param [Object] other
334
358
  # @return [Object] div
335
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
359
+ # @raise [Phys::UnitError] if unit conversion is failed.
336
360
  #
337
361
  def div(other)
338
362
  if Quantity===other
339
- @val.div( @unit.convert(other) )
363
+ @value.div( @unit.convert(other) )
340
364
  else
341
- self.class.new( @val.div(other), @expr, @unit )
365
+ self.class.new( @value.div(other), @expr, @unit )
342
366
  end
343
367
  end
344
368
 
@@ -351,13 +375,13 @@ module Phys
351
375
  # and returns +remainder+ of Phys::Quantity.
352
376
  # @param [Object] other
353
377
  # @return [Object] remainder
354
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
378
+ # @raise [Phys::UnitError] if unit conversion is failed.
355
379
  #
356
380
  def remainder(other) #:nodoc: used internally
357
381
  if Quantity===other
358
- @val.remainder( @unit.convert(other) )
382
+ @value.remainder( @unit.convert(other) )
359
383
  else
360
- self.class.new( @val.remainder(other), @expr, @unit )
384
+ self.class.new( @value.remainder(other), @expr, @unit )
361
385
  end
362
386
  end
363
387
 
@@ -370,13 +394,13 @@ module Phys
370
394
  # and returns +modulo+ of Phys::Quantity.
371
395
  # @param [Object] other
372
396
  # @return [Object] modulo
373
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
397
+ # @raise [Phys::UnitError] if unit conversion is failed.
374
398
  #
375
399
  def %(other)
376
400
  if Quantity===other
377
- @val % @unit.convert(other)
401
+ @value % @unit.convert(other)
378
402
  else
379
- self.class.new( @val % other, @expr, @unit )
403
+ self.class.new( @value % other, @expr, @unit )
380
404
  end
381
405
  end
382
406
  alias modulo %
@@ -390,13 +414,13 @@ module Phys
390
414
  # and returns +divmod+ of Phys::Quantity.
391
415
  # @param [Object] other
392
416
  # @return [Array] result of +divmod+, an array of [quotient, modulo].
393
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
417
+ # @raise [Phys::UnitError] if unit conversion is failed.
394
418
  #
395
419
  def divmod(other)
396
420
  if Quantity===other
397
- @val.divmod( @unit.convert(other) )
421
+ @value.divmod( @unit.convert(other) )
398
422
  else
399
- d,m = @val.divmod(other)
423
+ d,m = @value.divmod(other)
400
424
  [ self.class.new( d, @expr, @unit ),
401
425
  self.class.new( m, @expr, @unit ) ]
402
426
  end
@@ -410,7 +434,7 @@ module Phys
410
434
  # Conversion to base unit.
411
435
  # Returns the quantity converted to a base unit.
412
436
  # @return [Phys::Quantity] a quantity in the base unit.
413
- # @raise [Phys::UnitConversionError] if unit conversion is failed.
437
+ # @raise [Phys::UnitError] if unit conversion is failed.
414
438
  def to_base_unit
415
439
  unit = @unit.base_unit
416
440
  val = unit.convert(self)
@@ -422,15 +446,15 @@ module Phys
422
446
 
423
447
  # Conversion to Numeric.
424
448
  # @return [Numeric]
425
- # @raise [Phys::UnitConversionError] if the unit is non-dminensionless.
449
+ # @raise [Phys::UnitError] if the unit is *not* dimensionless.
426
450
  def to_numeric
427
- @unit.convert_to_numeric(@val)
451
+ @unit.to_numeric * @value
428
452
  end
429
453
  alias to_num to_numeric
430
454
 
431
455
  # Conversion to Float.
432
456
  # @return [Float]
433
- # @raise [Phys::UnitConversionError] if the unit is non-dminensionless.
457
+ # @raise [Phys::UnitError] if the unit is *not* dimensionless.
434
458
  def to_f
435
459
  to_numeric.to_f
436
460
  end
@@ -438,7 +462,7 @@ module Phys
438
462
 
439
463
  # Conversion to Integer.
440
464
  # @return [Integer]
441
- # @raise [Phys::UnitConversionError] if the unit is non-dminensionless.
465
+ # @raise [Phys::UnitError] if the unit is *not* dimensionless.
442
466
  def to_i
443
467
  to_numeric.to_i
444
468
  end
@@ -447,7 +471,7 @@ module Phys
447
471
 
448
472
  # Conversion to Rational.
449
473
  # @return [Rational]
450
- # @raise [Phys::UnitConversionError] if the unit is non-dminensionless.
474
+ # @raise [Phys::UnitError] if the unit is *not* dimensionless.
451
475
  def to_r
452
476
  to_numeric.to_r
453
477
  end
@@ -461,7 +485,7 @@ module Phys
461
485
  else
462
486
  expr = ""
463
487
  end
464
- self.class.to_s+"["+Unit::Utils.num_inspect(@val)+expr+"]"
488
+ self.class.to_s+"["+Unit::Utils.num_inspect(@value)+expr+"]"
465
489
  end
466
490
 
467
491
  # Inspect String.
@@ -472,7 +496,7 @@ module Phys
472
496
  else
473
497
  expr = ""
474
498
  end
475
- "#<"+self.class.to_s+" "+Unit::Utils.num_inspect(@val)+expr+", "+@unit.inspect+">"
499
+ self.class.to_s+"["+Unit::Utils.num_inspect(@value)+expr+"] "+@unit.inspect
476
500
  end
477
501
  end
478
502
  end