phys-units 0.9.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +21 -18
- data/lib/phys/units.rb +1 -0
- data/lib/phys/units/errors.rb +10 -0
- data/lib/phys/units/quantity.rb +280 -75
- data/lib/phys/units/unit.rb +20 -19
- data/lib/phys/units/unit_class.rb +0 -5
- data/lib/phys/units/utils.rb +1 -1
- data/lib/phys/units/version.rb +1 -1
- data/setup.rb +1585 -0
- data/spec/quantity_spec.rb +39 -0
- data/spec/unit_spec.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8fd589f3ac5dded9010b0d83ba9f31abccc40a4f
|
4
|
+
data.tar.gz: 17427a7a2bcb4631adea755b4fed53e9f0adccdd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5018dbf704ba64a03afcfa92718389636f9ac058ee633eebe81ef5e27353178bb7eb9e9c88b127508258d12584071ab32a37df3827e66f26e46fe5909266cff
|
7
|
+
data.tar.gz: 02d92bee85d27cfc47555a3b14bad2b0c2df683786700e73747d0b1a2c65737b9241f081bb03d2ff569d76af54ad1561ad711342ac3c80db7eb5b840e0dd86ac
|
data/README.md
CHANGED
@@ -3,32 +3,28 @@
|
|
3
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
|
-
This library provides the following Classes:
|
7
6
|
|
8
|
-
|
9
|
-
|
7
|
+
## Phys::Quantity
|
8
|
+
is a primary class of Phys-Units library, to be manipulated by users.
|
9
|
+
See Documentation at [Rubygems Site](https://rubygems.org/gems/phys-units)
|
10
10
|
|
11
11
|
## Installation
|
12
12
|
|
13
|
-
|
13
|
+
Install from gem as:
|
14
14
|
|
15
|
-
gem
|
16
|
-
|
17
|
-
And then execute:
|
18
|
-
|
19
|
-
$ bundle
|
15
|
+
$ gem install phys-units
|
20
16
|
|
21
|
-
Or install
|
17
|
+
Or install from source tree:
|
22
18
|
|
23
|
-
$
|
19
|
+
$ ruby setup.rb
|
24
20
|
|
25
21
|
## Usage
|
26
22
|
|
27
23
|
require 'phys/units'
|
28
24
|
Q = Phys::Quantity
|
29
|
-
Q[1.23,'km'] + Q[4.56,'m'] #=> Phys::
|
30
|
-
Q[123,'mile'] / Q[2,'hr'] #=> Phys::
|
31
|
-
Q[61,'miles/hr'].want('m/s') #=> Phys::
|
25
|
+
Q[1.23,'km'] + Q[4.56,'m'] #=> Phys::Quantity[1.23456,'km']
|
26
|
+
Q[123,'mile'] / Q[2,'hr'] #=> Phys::Quantity[61,'mile/hr']
|
27
|
+
Q[61,'miles/hr'].want('m/s') #=> Phys::Quantity[27.26944,'m/s']
|
32
28
|
Q[1.0,'are'] == Q[10,'m']**2 #=> true
|
33
29
|
Q[70,'tempF'] + Q[10,'tempC'] #=> Phys::Quantity[88,'tempF']
|
34
30
|
Q[20,'tempC'].want('tempF') #=> Phys::Quantity[68,'tempF']
|
@@ -36,16 +32,23 @@ Or install it yourself as:
|
|
36
32
|
|
37
33
|
## Features
|
38
34
|
|
39
|
-
Phys-Units library is discriminated from
|
35
|
+
Phys-Units library is discriminated from many other units libraies for Ruby,
|
40
36
|
by the following features:
|
41
|
-
|
37
|
+
|
38
|
+
* Compatible with GNU Units except nonlinear units.
|
42
39
|
* Provides 2415 units, 85 prefixes, including UTF-8 unit names.
|
43
|
-
* All
|
40
|
+
* All units are defined in a unit data file from GNU Units
|
44
41
|
and not defined as a Ruby codes, except temperature definitions.
|
45
42
|
* No addition or modification to Ruby standard classes by default,
|
46
43
|
avoiding conflict with other libraries.
|
47
|
-
* Calculation of values is
|
44
|
+
* Calculation of values is through Ruby Numeric arithmetic methods.
|
48
45
|
None of the Phys-Units lib's buisiness.
|
49
46
|
* Conversion factors are held in Rational even defined
|
50
47
|
in the decimal form such as `1.0e10'.
|
51
48
|
* PI number has a dimension.
|
49
|
+
|
50
|
+
## Copying License
|
51
|
+
GPL3
|
52
|
+
|
53
|
+
## Author
|
54
|
+
Masahiro TANAKA
|
data/lib/phys/units.rb
CHANGED
data/lib/phys/units/quantity.rb
CHANGED
@@ -13,12 +13,16 @@ module Phys
|
|
13
13
|
Quantity.new(*a)
|
14
14
|
end
|
15
15
|
|
16
|
+
# Phys::Quantity is a class to represent physical quantities
|
17
|
+
# with unit of measure.
|
18
|
+
# It contains a value in Numeric or similar class,
|
19
|
+
# and unit in Phys::Unit class.
|
16
20
|
#== Usage
|
17
21
|
# require 'phys/units'
|
18
22
|
# Q=Phys::Quantity
|
19
|
-
# Q[1.23,'km'] + Q[4.56,'m'] #=> Phys::
|
20
|
-
# Q[123,'mile'] / Q[2,'hr'] #=> Phys::
|
21
|
-
# Q[61,'miles/hr'].want('m/s') #=> Phys::
|
23
|
+
# Q[1.23,'km'] + Q[4.56,'m'] #=> Phys::Quantity[1.23456,'km']
|
24
|
+
# Q[123,'mile'] / Q[2,'hr'] #=> Phys::Quantity[61,'mile/hr']
|
25
|
+
# Q[61,'miles/hr'].want('m/s') #=> Phys::Quantity[27.26944,'m/s']
|
22
26
|
# Q[1.0,'are'] == Q[10,'m']**2 #=> true
|
23
27
|
# Q[70,'tempF'] + Q[10,'tempC'] #=> Phys::Quantity[88,'tempF']
|
24
28
|
# Q[20,'tempC'].want('tempF') #=> Phys::Quantity[68,'tempF']
|
@@ -26,16 +30,28 @@ module Phys
|
|
26
30
|
class Quantity
|
27
31
|
|
28
32
|
class << self
|
29
|
-
#
|
30
|
-
|
31
|
-
|
33
|
+
# Alias to Phys::Quantity.new.
|
34
|
+
# @param [Object] value
|
35
|
+
# Value of quantity.
|
36
|
+
# @param [String] expr unit expression.
|
37
|
+
# If +expr+ is not supplied, it becomes dimeinsionless.
|
38
|
+
# @return [Phys::Quantity]
|
39
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
40
|
+
def [](value,expr=nil)
|
41
|
+
self.new(value,expr)
|
32
42
|
end
|
33
43
|
end
|
34
44
|
|
35
45
|
# Initialize a new quantity.
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
46
|
+
# @param [Object] value
|
47
|
+
# Value of quantity.
|
48
|
+
# +value+ must be a class instance having arithmetic methods
|
49
|
+
# in a duck typing way.
|
50
|
+
# @param [String] expr unit expression.
|
51
|
+
# If +expr+ is not supplied, it becomes dimeinsionless.
|
52
|
+
# @param [Phys::Unit] unit (optional)
|
53
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
54
|
+
#
|
39
55
|
def initialize(value,expr=nil,unit=nil)
|
40
56
|
@val = value
|
41
57
|
expr = expr.to_s if Symbol===expr
|
@@ -48,14 +64,21 @@ module Phys
|
|
48
64
|
end
|
49
65
|
end
|
50
66
|
|
67
|
+
# @return [Object] value of the quantity
|
51
68
|
attr_reader :val
|
69
|
+
alias value val
|
70
|
+
|
71
|
+
# @return [String] unit expression
|
52
72
|
attr_reader :expr
|
53
|
-
attr_reader :unit
|
54
73
|
|
55
|
-
#
|
56
|
-
|
74
|
+
# @return [Phys::Unit] unit
|
75
|
+
attr_reader :unit
|
57
76
|
|
58
|
-
# Conversion to a quantity in another
|
77
|
+
# Conversion to a quantity in another unit.
|
78
|
+
# @param [String] expr unit expression.
|
79
|
+
# @return [Phys::Quantity] quantity in the unit of +expr+.
|
80
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
81
|
+
#
|
59
82
|
def want(expr)
|
60
83
|
unit = Unit.parse(expr)
|
61
84
|
val = unit.convert(self)
|
@@ -64,63 +87,124 @@ module Phys
|
|
64
87
|
alias convert want
|
65
88
|
|
66
89
|
# Addition of two quantities.
|
67
|
-
#
|
68
|
-
#
|
69
|
-
#
|
70
|
-
#
|
90
|
+
# Before the operation, it converts +other+ to the unit of +self+.
|
91
|
+
# * If the +other+ param is Phys::Quantity,
|
92
|
+
# +other+ is converted to the unit of +self+.
|
93
|
+
# * If the +other+ param is *not* Phys::Quantity,
|
94
|
+
# both params must be dimensionless.
|
95
|
+
# @param [Object] other
|
96
|
+
# @return [Phys::Quantity] a quantity in the unit of +self+.
|
97
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
98
|
+
#
|
71
99
|
def +(other)
|
72
100
|
val = @val + @unit.convert_scale(other)
|
73
101
|
self.class.new( val, @expr, @unit )
|
74
102
|
end
|
75
103
|
|
76
104
|
# Subtraction of two quantities.
|
77
|
-
#
|
78
|
-
#
|
79
|
-
#
|
80
|
-
#
|
105
|
+
# Before the operation, it converts +other+ to the unit of +self+.
|
106
|
+
# * If the +other+ param is Phys::Quantity,
|
107
|
+
# +other+ is converted to the unit of +self+.
|
108
|
+
# * If the +other+ param is *not* Phys::Quantity,
|
109
|
+
# both params must be dimensionless.
|
110
|
+
# @param [Object] other
|
111
|
+
# @return [Phys::Quantity] a quantity in the unit of +self+.
|
112
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
113
|
+
#
|
81
114
|
def -(other)
|
82
115
|
val = @val - @unit.convert_scale(other)
|
83
116
|
self.class.new( val, @expr, @unit )
|
84
117
|
end
|
85
118
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
end
|
119
|
+
# @return [Phys::Quantity] abs quantity in the unit of +self+.
|
120
|
+
def abs
|
121
|
+
self.class.new( @val.abs, @expr, @unit )
|
90
122
|
end
|
91
123
|
|
92
|
-
#
|
93
|
-
def
|
124
|
+
# @return [Phys::Quantity] abs2 quantity in the squared unit of +self+.
|
125
|
+
def abs2
|
126
|
+
self**2
|
127
|
+
end
|
94
128
|
|
95
|
-
#
|
96
|
-
def
|
129
|
+
# @return [Phys::Quantity] ceil quantity in the unit of +self+.
|
130
|
+
def ceil
|
131
|
+
self.class.new( @val.ceil, @expr, @unit )
|
132
|
+
end
|
97
133
|
|
98
|
-
#
|
99
|
-
|
100
|
-
|
134
|
+
# @return [Phys::Quantity] round quantity in the unit of +self+.
|
135
|
+
def round
|
136
|
+
self.class.new( @val.round, @expr, @unit )
|
137
|
+
end
|
101
138
|
|
102
|
-
#
|
103
|
-
|
104
|
-
|
139
|
+
# @return [Phys::Quantity] floor quantity in the unit of +self+.
|
140
|
+
def floor
|
141
|
+
self.class.new( @val.floor, @expr, @unit )
|
142
|
+
end
|
105
143
|
|
106
|
-
#
|
107
|
-
|
108
|
-
|
144
|
+
# @return [Phys::Quantity] truncate quantity in the unit of +self+.
|
145
|
+
def truncate
|
146
|
+
self.class.new( @val.truncate, @expr, @unit )
|
147
|
+
end
|
109
148
|
|
110
|
-
#
|
111
|
-
#
|
112
|
-
def
|
149
|
+
# Unary Plus.
|
150
|
+
# @return [Phys::Quantity] +self+.
|
151
|
+
def +@
|
152
|
+
self.class.new( @val, @expr, @unit )
|
153
|
+
end
|
113
154
|
|
114
|
-
#
|
115
|
-
#
|
116
|
-
def
|
155
|
+
# Unary Minus.
|
156
|
+
# @return [Phys::Quantity] a quantity in the unit of +self+.
|
157
|
+
def -@
|
158
|
+
self.class.new( -@val, @expr, @unit )
|
159
|
+
end
|
160
|
+
|
161
|
+
# Comparison of quantities.
|
162
|
+
# Before the comparison, it converts +other+ to the unit of +self+.
|
163
|
+
# @return [Integer]
|
164
|
+
def <=> (other)
|
165
|
+
@val <=> @unit.convert(other)
|
166
|
+
end
|
167
|
+
|
168
|
+
# Comparison of quantities.
|
169
|
+
# Before the comparison, it converts +other+ to the unit of +self+.
|
170
|
+
# @return [Boolean]
|
171
|
+
def == (other)
|
172
|
+
@val == @unit.convert(other)
|
173
|
+
end
|
174
|
+
|
175
|
+
# Comparison of quantities.
|
176
|
+
# Before the comparison, it converts +other+ to the unit of +self+.
|
177
|
+
# @return [Boolean]
|
178
|
+
def >= (other)
|
179
|
+
@val >= @unit.convert(other)
|
180
|
+
end
|
117
181
|
|
118
|
-
# Comparison of quantities.
|
119
|
-
#
|
120
|
-
|
182
|
+
# Comparison of quantities.
|
183
|
+
# Before the comparison, it converts +other+ to the unit of +self+.
|
184
|
+
# @return [Boolean]
|
185
|
+
def <= (other)
|
186
|
+
@val <= @unit.convert(other)
|
187
|
+
end
|
188
|
+
|
189
|
+
# Comparison of quantities.
|
190
|
+
# Before the comparison, it converts +other+ to the unit of +self+.
|
191
|
+
# @return [Boolean]
|
192
|
+
def < (other)
|
193
|
+
@val < @unit.convert(other)
|
194
|
+
end
|
195
|
+
|
196
|
+
# Comparison of quantities.
|
197
|
+
# Before the comparison, it converts +other+ to the unit of +self+.
|
198
|
+
# @return [Boolean]
|
199
|
+
def > (other)
|
200
|
+
@val > @unit.convert(other)
|
201
|
+
end
|
121
202
|
|
122
203
|
# Power of a quantity.
|
123
|
-
#
|
204
|
+
# @param [Numeric] n
|
205
|
+
# @return [Phys::Quantity] a quantity in the +n+ -powered unit of +self+.
|
206
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
207
|
+
#
|
124
208
|
def **(n)
|
125
209
|
if @expr.nil?
|
126
210
|
expr = nil
|
@@ -132,7 +216,10 @@ module Phys
|
|
132
216
|
self.class.new( @val**n, expr, @unit**n )
|
133
217
|
end
|
134
218
|
|
135
|
-
|
219
|
+
# (internally used method)
|
220
|
+
# @private
|
221
|
+
# @return [String]
|
222
|
+
def enclose_expr #:nodoc: used internally
|
136
223
|
return nil if @expr.nil?
|
137
224
|
if /\/|\||per/o =~ @expr
|
138
225
|
'('+@expr+')'
|
@@ -140,8 +227,11 @@ module Phys
|
|
140
227
|
@expr
|
141
228
|
end
|
142
229
|
end
|
143
|
-
|
144
|
-
|
230
|
+
|
231
|
+
# (internally used method)
|
232
|
+
# @private
|
233
|
+
# @return [String]
|
234
|
+
def enclose_expr_div #:nodoc: used internally
|
145
235
|
return nil if @expr.nil?
|
146
236
|
if /\w[^\w]+\w/o =~ @expr
|
147
237
|
'/('+@expr+')'
|
@@ -151,7 +241,13 @@ module Phys
|
|
151
241
|
end
|
152
242
|
|
153
243
|
# Multiplication of two quantities.
|
154
|
-
#
|
244
|
+
# If the +other+ param is *not* Phys::Quantity,
|
245
|
+
# +other+ is regarded as a dimensionless value.
|
246
|
+
# @param [Object] other
|
247
|
+
# @return [Phys::Quantity] a quantity
|
248
|
+
# both the values and units are multiplied respectively.
|
249
|
+
# @raise [Phys::UnitOperationError] if unit is not operable.
|
250
|
+
#
|
155
251
|
def *(other)
|
156
252
|
if Quantity===other
|
157
253
|
a = [self.enclose_expr, other.enclose_expr]
|
@@ -163,42 +259,131 @@ module Phys
|
|
163
259
|
end
|
164
260
|
|
165
261
|
# Division of two quantities.
|
166
|
-
#
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
262
|
+
# If the +other+ param is *not* Phys::Quantity,
|
263
|
+
# +other+ is regarded as a dimensionless value.
|
264
|
+
# @param [Object] other
|
265
|
+
# @return [Phys::Quantity] a quantity
|
266
|
+
# both the values and units are divided respectively.
|
267
|
+
# @raise [Phys::UnitOperationError] if unit is not operable.
|
268
|
+
#
|
269
|
+
def /(other)
|
270
|
+
if Quantity===other
|
271
|
+
a = [self.enclose_expr, other.enclose_expr_div]
|
272
|
+
a.delete(nil)
|
273
|
+
self.class.new( @val/other.val, a.join, @unit/other.unit )
|
274
|
+
else
|
275
|
+
self.class.new( @val/other, @expr, @unit )
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
# Division of two quantities.
|
280
|
+
# If the +other+ param is *not* Phys::Quantity,
|
281
|
+
# +other+ is regarded as a dimensionless value.
|
282
|
+
# @param [Object] other
|
283
|
+
# @return [Phys::Quantity] a quantity
|
284
|
+
# both the values and units are divided respectively.
|
285
|
+
# @raise [Phys::UnitOperationError] if unit is not operable.
|
286
|
+
#
|
287
|
+
def quo(other)
|
288
|
+
if Quantity===other
|
289
|
+
a = [self.enclose_expr, other.enclose_expr_div]
|
290
|
+
a.delete(nil)
|
291
|
+
self.class.new( @val.quo(other.val), a.join, @unit/other.unit )
|
292
|
+
else
|
293
|
+
self.class.new( @val.quo(other), @expr, @unit )
|
176
294
|
end
|
177
295
|
end
|
178
296
|
alias fdiv quo
|
179
297
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
298
|
+
# Division of two quantities without Remainder.
|
299
|
+
# * If the +other+ param is Phys::Quantity,
|
300
|
+
# +other+ is converted to the unit of +self+,
|
301
|
+
# and returns +div+ of values.
|
302
|
+
# * If the +other+ param is *not* Phys::Quantity,
|
303
|
+
# +other+ is regarded as dimensionless,
|
304
|
+
# and returns +div+ of Phys::Quantity.
|
305
|
+
# @param [Object] other
|
306
|
+
# @return [Object] div
|
307
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
308
|
+
#
|
309
|
+
def div(other)
|
310
|
+
if Quantity===other
|
311
|
+
@val.div( @unit.convert(other) )
|
312
|
+
else
|
313
|
+
self.class.new( @val.div(other), @expr, @unit )
|
184
314
|
end
|
185
315
|
end
|
186
|
-
alias modulo %
|
187
316
|
|
188
|
-
|
189
|
-
|
317
|
+
# Remainder of two quantities.
|
318
|
+
# * If the +other+ param is Phys::Quantity,
|
319
|
+
# +other+ is converted to the unit of +self+,
|
320
|
+
# and returns +remainder+ of values.
|
321
|
+
# * If the +other+ param is *not* Phys::Quantity,
|
322
|
+
# +other+ is regarded as dimensionless,
|
323
|
+
# and returns +remainder+ of Phys::Quantity.
|
324
|
+
# @param [Object] other
|
325
|
+
# @return [Object] remainder
|
326
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
327
|
+
#
|
328
|
+
def remainder(other) #:nodoc: used internally
|
329
|
+
if Quantity===other
|
330
|
+
@val.remainder( @unit.convert(other) )
|
331
|
+
else
|
332
|
+
self.class.new( @val.remainder(other), @expr, @unit )
|
333
|
+
end
|
190
334
|
end
|
191
335
|
|
192
|
-
|
193
|
-
|
336
|
+
# Modulo of two quantities.
|
337
|
+
# * If the +other+ param is Phys::Quantity,
|
338
|
+
# +other+ is converted to the unit of +self+,
|
339
|
+
# and returns +modulo+ of values.
|
340
|
+
# * If the +other+ param is *not* Phys::Quantity,
|
341
|
+
# +other+ is regarded as dimensionless,
|
342
|
+
# and returns +modulo+ of Phys::Quantity.
|
343
|
+
# @param [Object] other
|
344
|
+
# @return [Object] modulo
|
345
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
346
|
+
#
|
347
|
+
def %(other)
|
348
|
+
if Quantity===other
|
349
|
+
@val % @unit.convert(other)
|
350
|
+
else
|
351
|
+
self.class.new( @val % other, @expr, @unit )
|
352
|
+
end
|
194
353
|
end
|
354
|
+
alias modulo %
|
195
355
|
|
196
|
-
|
197
|
-
|
356
|
+
# Division and Modulo of two quantities.
|
357
|
+
# * If the +other+ param is Phys::Quantity,
|
358
|
+
# +other+ is converted to the unit of +self+,
|
359
|
+
# and returns +divmod+ of values.
|
360
|
+
# * If the +other+ param is *not* Phys::Quantity,
|
361
|
+
# +other+ is regarded as dimensionless,
|
362
|
+
# and returns +divmod+ of Phys::Quantity.
|
363
|
+
# @param [Object] other
|
364
|
+
# @return [Array] result of +divmod+, an array of [quotient, modulo].
|
365
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
366
|
+
#
|
367
|
+
def divmod(other)
|
368
|
+
if Quantity===other
|
369
|
+
@val.divmod( @unit.convert(other) )
|
370
|
+
else
|
371
|
+
d,m = @val.divmod(other)
|
372
|
+
[ self.class.new( d, @expr, @unit ),
|
373
|
+
self.class.new( m, @expr, @unit ) ]
|
374
|
+
end
|
375
|
+
end
|
376
|
+
|
377
|
+
# @return [Array]
|
378
|
+
def coerce(other)
|
379
|
+
[ self.class.new(other), self ]
|
198
380
|
end
|
199
381
|
|
200
382
|
# Conversion to base unit.
|
201
383
|
# Returns the quantity converted to a base unit.
|
384
|
+
# @return [Phys::Quantity] a quantity in the base unit.
|
385
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
386
|
+
#
|
202
387
|
def to_base_unit
|
203
388
|
unit = @unit.base_unit
|
204
389
|
val = unit.convert(self)
|
@@ -209,28 +394,45 @@ module Phys
|
|
209
394
|
alias to_SI to_base_unit
|
210
395
|
|
211
396
|
# Conversion to Numeric.
|
212
|
-
#
|
213
|
-
#
|
397
|
+
# @return [Numeric]
|
398
|
+
# @raise [Phys::UnitConversionError] if the unit is non-dminensionless.
|
399
|
+
#
|
214
400
|
def to_numeric
|
215
401
|
@unit.convert_to_numeric(@val)
|
216
402
|
end
|
403
|
+
alias to_num to_numeric
|
217
404
|
|
405
|
+
# Conversion to Float.
|
406
|
+
# @return [Float]
|
407
|
+
# @raise [Phys::UnitConversionError] if the unit is non-dminensionless.
|
408
|
+
#
|
218
409
|
def to_f
|
219
410
|
to_numeric.to_f
|
220
411
|
end
|
221
412
|
alias to_float to_f
|
222
413
|
|
414
|
+
# Conversion to Integer.
|
415
|
+
# @return [Integer]
|
416
|
+
# @raise [Phys::UnitConversionError] if the unit is non-dminensionless.
|
417
|
+
#
|
223
418
|
def to_i
|
224
419
|
to_numeric.to_i
|
225
420
|
end
|
226
421
|
alias to_int to_i
|
227
422
|
alias to_integer to_i
|
228
423
|
|
424
|
+
# Conversion to Rational.
|
425
|
+
# @return [Rational]
|
426
|
+
# @raise [Phys::UnitConversionError] if the unit is non-dminensionless.
|
427
|
+
#
|
229
428
|
def to_r
|
230
429
|
to_numeric.to_r
|
231
430
|
end
|
232
431
|
alias to_rational to_r
|
233
432
|
|
433
|
+
# Conversion to String.
|
434
|
+
# @return [String]
|
435
|
+
#
|
234
436
|
def to_s
|
235
437
|
if @expr
|
236
438
|
expr = ",'" +@expr+"'"
|
@@ -240,6 +442,9 @@ module Phys
|
|
240
442
|
self.class.to_s+"["+Unit::Utils.num_inspect(@val)+expr+"]"
|
241
443
|
end
|
242
444
|
|
445
|
+
# Inspect String.
|
446
|
+
# @return [String]
|
447
|
+
#
|
243
448
|
def inspect
|
244
449
|
if @expr
|
245
450
|
expr = "," +@expr.inspect
|