phys-units 0.9.1 → 0.9.2
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 +23 -3
- data/lib/phys/units/load_units.rb +5 -105
- data/lib/phys/units/parse.rb +12 -4
- data/lib/phys/units/parse.y +13 -5
- data/lib/phys/units/quantity.rb +56 -35
- data/lib/phys/units/unit.rb +22 -18
- data/lib/phys/units/unit_class.rb +6 -5
- data/lib/phys/units/version.rb +1 -1
- data/spec/helper.rb +22 -0
- data/spec/units_dat_spec.rb +6983 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 562362594d0457c4492bc6a2467e2ee271c81dab
|
4
|
+
data.tar.gz: 86d22f70ec1f1962631fde49b63fcf72e408650b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1124141150ac160dcd3568ba3507e0cf86d1521ba80e49a7c6307b7130710943fa1e7452b3f0df54fe2bc885f67cd690f3f433ecdd7f28605897d3736cd37060
|
7
|
+
data.tar.gz: d92bfe06366359ff027c0eb599d2bd8c78c7795a8c4743789c017bdb23b6d9725e0c2a1d46ca30815f0f854f817c012e6ed546db680b4bc7fa75ed97c136ca4f
|
data/README.md
CHANGED
@@ -5,7 +5,17 @@ Former name is [Quanty](http://narray.rubyforge.org/quanty/quanty-en.html),
|
|
5
5
|
the first Ruby units library released in 2001.
|
6
6
|
|
7
7
|
## Phys::Quantity
|
8
|
-
is
|
8
|
+
is the primary class of Phys-Units library, to be manipulated by users.
|
9
|
+
It contains:
|
10
|
+
|
11
|
+
* *Value* :
|
12
|
+
+value+ must be a class instance having arithmetic methods,
|
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.
|
18
|
+
|
9
19
|
See Documentation at [Rubygems Site](https://rubygems.org/gems/phys-units)
|
10
20
|
|
11
21
|
## Installation
|
@@ -36,7 +46,7 @@ Phys-Units library is discriminated from many other units libraies for Ruby,
|
|
36
46
|
by the following features:
|
37
47
|
|
38
48
|
* Compatible with GNU Units except nonlinear units.
|
39
|
-
* Provides
|
49
|
+
* Provides 2331 units, 85 prefixes, including UTF-8 unit names.
|
40
50
|
* All units are defined in a unit data file from GNU Units
|
41
51
|
and not defined as a Ruby codes, except temperature definitions.
|
42
52
|
* No addition or modification to Ruby standard classes by default,
|
@@ -47,8 +57,18 @@ by the following features:
|
|
47
57
|
in the decimal form such as `1.0e10'.
|
48
58
|
* PI number has a dimension.
|
49
59
|
|
60
|
+
## Platforms tested
|
61
|
+
|
62
|
+
* ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux]
|
63
|
+
|
50
64
|
## Copying License
|
51
|
-
|
65
|
+
|
66
|
+
This program is free software.
|
67
|
+
You can distribute/modify this program
|
68
|
+
under the same terms as GPL3.
|
69
|
+
See "COPYING" file.
|
70
|
+
NO WARRANTY.
|
52
71
|
|
53
72
|
## Author
|
73
|
+
|
54
74
|
Masahiro TANAKA
|
@@ -927,8 +927,8 @@ wc water # water column
|
|
927
927
|
mach 331.46 m/s # speed of sound in dry air at STP
|
928
928
|
standardtemp 273.15 K # standard temperature
|
929
929
|
stdtemp standardtemp
|
930
|
-
normaltemp tempF(70) # for gas density, from NIST
|
931
|
-
normtemp normaltemp # Handbook 44
|
930
|
+
#normaltemp tempF(70) # for gas density, from NIST
|
931
|
+
#normtemp normaltemp # Handbook 44
|
932
932
|
|
933
933
|
# Weight of mercury and water at different temperatures using the standard
|
934
934
|
# force of gravity.
|
@@ -3446,62 +3446,6 @@ megalerg megaerg # 'L' added to make it pronounceable [18].
|
|
3446
3446
|
|
3447
3447
|
unitedstatesdollar US$
|
3448
3448
|
$ dollar
|
3449
|
-
mark germanymark
|
3450
|
-
bolivar venezuelanbolivarfuerte
|
3451
|
-
bolivarfuerte bolivar # The currency was revalued by
|
3452
|
-
oldbolivar 1|1000 bolivar # a factor of 1000.
|
3453
|
-
peseta spainpeseta
|
3454
|
-
rand southafricarand
|
3455
|
-
escudo portugalescudo
|
3456
|
-
guilder netherlandsguilder
|
3457
|
-
hollandguilder netherlandsguilder
|
3458
|
-
peso mexicopeso
|
3459
|
-
yen japanyen
|
3460
|
-
lira italylira
|
3461
|
-
rupee indiarupee
|
3462
|
-
drachma greecedrachma
|
3463
|
-
franc francefranc
|
3464
|
-
markka finlandmarkka
|
3465
|
-
britainpound unitedkingdompound
|
3466
|
-
greatbritainpound unitedkingdompound
|
3467
|
-
poundsterling britainpound
|
3468
|
-
yuan chinayuan
|
3469
|
-
|
3470
|
-
# Some European currencies have permanent fixed exchange rates with
|
3471
|
-
# the Euro. These rates were taken from the EC's web site:
|
3472
|
-
# http://ec.europa.eu/economy_finance/euro/adoption/conversion/index_en.htm
|
3473
|
-
|
3474
|
-
austriaschilling 1|13.7603 euro
|
3475
|
-
belgiumfranc 1|40.3399 euro
|
3476
|
-
estoniakroon 1|15.6466 euro # Equal to 1|8 germanymark
|
3477
|
-
finlandmarkka 1|5.94573 euro
|
3478
|
-
francefranc 1|6.55957 euro
|
3479
|
-
germanymark 1|1.95583 euro
|
3480
|
-
greecedrachma 1|340.75 euro
|
3481
|
-
irelandpunt 1|0.787564 euro
|
3482
|
-
italylira 1|1936.27 euro
|
3483
|
-
luxembourgfranc 1|40.3399 euro
|
3484
|
-
netherlandsguilder 1|2.20371 euro
|
3485
|
-
portugalescudo 1|200.482 euro
|
3486
|
-
spainpeseta 1|166.386 euro
|
3487
|
-
cypruspound 1|0.585274 euro
|
3488
|
-
maltalira 1|0.429300 euro
|
3489
|
-
sloveniatolar 1|239.640 euro
|
3490
|
-
slovakiakoruna 1|30.1260 euro
|
3491
|
-
|
3492
|
-
UKP GBP # Not an ISO code, but looks like one, and
|
3493
|
-
# sometimes used on usenet.
|
3494
|
-
VEB 1|1000 VEF # old venezuelan bolivar
|
3495
|
-
|
3496
|
-
!include currency.units
|
3497
|
-
|
3498
|
-
# Money on the gold standard, used in the late 19th century and early
|
3499
|
-
# 20th century.
|
3500
|
-
|
3501
|
-
olddollargold 23.22 grains goldprice # Used until 1934
|
3502
|
-
newdollargold 96|7 grains goldprice # After Jan 31, 1934
|
3503
|
-
dollargold newdollargold
|
3504
|
-
poundgold 113 grains goldprice
|
3505
3449
|
|
3506
3450
|
# Nominal masses of US coins. Note that dimes, quarters and half dollars
|
3507
3451
|
# have weight proportional to value. Before 1965 it was $40 / kg.
|
@@ -3513,45 +3457,6 @@ USquarterweight US$ 0.25 / (20 US$ / lb) # Since 1965
|
|
3513
3457
|
UShalfdollarweight US$ 0.50 / (20 US$ / lb) # Since 1971
|
3514
3458
|
USdollarmass 8.1 grams
|
3515
3459
|
|
3516
|
-
# British currency
|
3517
|
-
|
3518
|
-
quid britainpound # Slang names
|
3519
|
-
fiver 5 quid
|
3520
|
-
tenner 10 quid
|
3521
|
-
monkey 500 quid
|
3522
|
-
brgrand 1000 quid
|
3523
|
-
bob shilling
|
3524
|
-
|
3525
|
-
shilling 1|20 britainpound # Before decimalisation, there
|
3526
|
-
oldpence 1|12 shilling # were 20 shillings to a pound,
|
3527
|
-
farthing 1|4 oldpence # each of twelve old pence
|
3528
|
-
guinea 21 shilling # Still used in horse racing
|
3529
|
-
crown 5 shilling
|
3530
|
-
florin 2 shilling
|
3531
|
-
groat 4 oldpence
|
3532
|
-
tanner 6 oldpence
|
3533
|
-
brpenny 0.01 britainpound
|
3534
|
-
pence brpenny
|
3535
|
-
tuppence 2 pence
|
3536
|
-
tuppenny tuppence
|
3537
|
-
ha'penny halfbrpenny
|
3538
|
-
hapenny ha'penny
|
3539
|
-
oldpenny oldpence
|
3540
|
-
oldtuppence 2 oldpence
|
3541
|
-
oldtuppenny oldtuppence
|
3542
|
-
threepence 3 oldpence # threepence never refers to new money
|
3543
|
-
threepenny threepence
|
3544
|
-
oldthreepence threepence
|
3545
|
-
oldthreepenny threepence
|
3546
|
-
oldhalfpenny halfoldpenny
|
3547
|
-
oldha'penny oldhalfpenny
|
3548
|
-
oldhapenny oldha'penny
|
3549
|
-
brpony 25 britainpound
|
3550
|
-
|
3551
|
-
# Canadian currency
|
3552
|
-
|
3553
|
-
loony 1 canadadollar # This coin depicts a loon
|
3554
|
-
toony 2 canadadollar
|
3555
3460
|
|
3556
3461
|
#
|
3557
3462
|
# Units used for measuring volume of wood
|
@@ -5205,7 +5110,7 @@ hogshead brhogshead
|
|
5205
5110
|
# U+2150- 1|7 For some reason these characters are getting
|
5206
5111
|
# U+2151- 1|9 flagged as invalid UTF8.
|
5207
5112
|
# U+2152- 1|10
|
5208
|
-
|
5113
|
+
#ℯ exp(1) # U+212F, base of natural log
|
5209
5114
|
|
5210
5115
|
µ- micro # micro sign U+00B5
|
5211
5116
|
μ- micro # small mu U+03BC
|
@@ -5223,13 +5128,6 @@ röntgen roentgen
|
|
5223
5128
|
K K # Kelvin symbol, U+212A
|
5224
5129
|
ℓ liter # unofficial abbreviation used in some places
|
5225
5130
|
¢ cent
|
5226
|
-
£ britainpound
|
5227
|
-
¥ japanyen
|
5228
|
-
€ euro
|
5229
|
-
₩ southkoreawon
|
5230
|
-
₪ israelnewshekel
|
5231
|
-
₤ lira
|
5232
|
-
₨ rupee
|
5233
5131
|
|
5234
5132
|
Ω ohm # Ohm symbol U+2126
|
5235
5133
|
Ω ohm # Greek capital omega U+03A9
|
@@ -5403,4 +5301,6 @@ module Phys
|
|
5403
5301
|
zero_degc = Unit["stdtemp"].conversion_factor
|
5404
5302
|
OffsetUnit.define( "tempC", Unit["K"], zero_degc )
|
5405
5303
|
OffsetUnit.define( "tempF", Unit["K"]*Rational(5,9), zero_degc-32*Rational(5,9) )
|
5304
|
+
|
5305
|
+
#puts "#{Phys::Unit::LIST.size} units, #{Phys::Unit::PREFIX.size} prefixes"
|
5406
5306
|
end
|
data/lib/phys/units/parse.rb
CHANGED
@@ -481,7 +481,6 @@ module_eval(<<'...end parse.y/module_eval...', 'parse.y', 63)
|
|
481
481
|
|
482
482
|
def parse( str )
|
483
483
|
return Unit.new(str) if str.empty?
|
484
|
-
#p str
|
485
484
|
@q = []
|
486
485
|
|
487
486
|
c = Unit.unit_chars
|
@@ -490,15 +489,24 @@ module_eval(<<'...end parse.y/module_eval...', 'parse.y', 63)
|
|
490
489
|
case str
|
491
490
|
when /\A[\s]+/o
|
492
491
|
when /\A(\d+)(?:(?:\.(\d*))?(?:[eE]([+-]?\d+))?)/o
|
493
|
-
|
492
|
+
@q.push [:NUMBER, build_num($1,$2,$3)]
|
494
493
|
when /\A(sin|cos|tan|log|ln|log2)\b/o
|
495
494
|
@q.push [:UFUNC, $&]
|
496
495
|
when /\A\//o
|
497
496
|
@q.push [:DIV, $&]
|
498
497
|
when /\Aper\b/o
|
499
498
|
@q.push [:DIV, $&]
|
500
|
-
|
501
|
-
|
499
|
+
when /\A([^#{c}0-9,.-]([^#{c}$-]*[^#{c}0-9,.])?)(\d)?/o
|
500
|
+
a = $&
|
501
|
+
x = $1
|
502
|
+
n = $3
|
503
|
+
if n && n.to_i >= 2
|
504
|
+
@q.push [:WORD, x]
|
505
|
+
@q.push [:POW, '']
|
506
|
+
@q.push [:NUMBER, n.to_i]
|
507
|
+
else
|
508
|
+
@q.push [:WORD, a]
|
509
|
+
end
|
502
510
|
when /\A[%'"]'?/o
|
503
511
|
@q.push [:WORD, $&]
|
504
512
|
when /\A\^|\A\*\*/o
|
data/lib/phys/units/parse.y
CHANGED
@@ -41,7 +41,7 @@ rule
|
|
41
41
|
|
42
42
|
list: numexpr
|
43
43
|
| pexpr
|
44
|
-
| WORD
|
44
|
+
| WORD { result = Unit.word(val[0]) }
|
45
45
|
| list list = MULTIPLY { result = val[0] * val[1] }
|
46
46
|
| list POW list { result = val[0]** val[2] }
|
47
47
|
| list POW '-' list = POW { result = val[0]**(-val[3]) }
|
@@ -81,7 +81,6 @@ class Unit
|
|
81
81
|
|
82
82
|
def parse( str )
|
83
83
|
return Unit.new(str) if str.empty?
|
84
|
-
#p str
|
85
84
|
@q = []
|
86
85
|
|
87
86
|
c = Unit.unit_chars
|
@@ -90,15 +89,24 @@ class Unit
|
|
90
89
|
case str
|
91
90
|
when /\A[\s]+/o
|
92
91
|
when /\A(\d+)(?:(?:\.(\d*))?(?:[eE]([+-]?\d+))?)/o
|
93
|
-
|
92
|
+
@q.push [:NUMBER, build_num($1,$2,$3)]
|
94
93
|
when /\A(sin|cos|tan|log|ln|log2)\b/o
|
95
94
|
@q.push [:UFUNC, $&]
|
96
95
|
when /\A\//o
|
97
96
|
@q.push [:DIV, $&]
|
98
97
|
when /\Aper\b/o
|
99
98
|
@q.push [:DIV, $&]
|
100
|
-
|
101
|
-
|
99
|
+
when /\A([^#{c}0-9,.-]([^#{c}$-]*[^#{c}0-9,.])?)(\d)?/o
|
100
|
+
a = $&
|
101
|
+
x = $1
|
102
|
+
n = $3
|
103
|
+
if n && n.to_i >= 2
|
104
|
+
@q.push [:WORD, x]
|
105
|
+
@q.push [:POW, '']
|
106
|
+
@q.push [:NUMBER, n.to_i]
|
107
|
+
else
|
108
|
+
@q.push [:WORD, a]
|
109
|
+
end
|
102
110
|
when /\A[%'"]'?/o
|
103
111
|
@q.push [:WORD, $&]
|
104
112
|
when /\A\^|\A\*\*/o
|
data/lib/phys/units/quantity.rb
CHANGED
@@ -9,14 +9,21 @@
|
|
9
9
|
|
10
10
|
module Phys
|
11
11
|
|
12
|
+
# alias to Phys::Quantity.new
|
12
13
|
def Quantity(*a)
|
13
14
|
Quantity.new(*a)
|
14
15
|
end
|
15
16
|
|
16
17
|
# Phys::Quantity is a class to represent physical quantities
|
17
18
|
# with unit of measure.
|
18
|
-
# It contains
|
19
|
-
#
|
19
|
+
# It contains:
|
20
|
+
# * *Value* of the quantity.
|
21
|
+
# +value+ must be a class instance having arithmetic methods,
|
22
|
+
# but it is not necessary to be a Numeric.
|
23
|
+
# This is a duck typing way.
|
24
|
+
# * *Unit* of the quantity.
|
25
|
+
# +unit+ is a instance of Phys::Unit class
|
26
|
+
# obtained by parsing a +expr+ string.
|
20
27
|
#== Usage
|
21
28
|
# require 'phys/units'
|
22
29
|
# Q=Phys::Quantity
|
@@ -33,7 +40,7 @@ module Phys
|
|
33
40
|
# Alias to Phys::Quantity.new.
|
34
41
|
# @param [Object] value
|
35
42
|
# Value of quantity.
|
36
|
-
# @param [String] expr unit expression.
|
43
|
+
# @param [String] expr a string of unit expression.
|
37
44
|
# If +expr+ is not supplied, it becomes dimeinsionless.
|
38
45
|
# @return [Phys::Quantity]
|
39
46
|
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
@@ -45,9 +52,7 @@ module Phys
|
|
45
52
|
# Initialize a new quantity.
|
46
53
|
# @param [Object] value
|
47
54
|
# Value of quantity.
|
48
|
-
#
|
49
|
-
# in a duck typing way.
|
50
|
-
# @param [String] expr unit expression.
|
55
|
+
# @param [String] expr a string of unit expression.
|
51
56
|
# If +expr+ is not supplied, it becomes dimeinsionless.
|
52
57
|
# @param [Phys::Unit] unit (optional)
|
53
58
|
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
@@ -86,7 +91,7 @@ module Phys
|
|
86
91
|
end
|
87
92
|
alias convert want
|
88
93
|
|
89
|
-
# Addition
|
94
|
+
# Addition.
|
90
95
|
# Before the operation, it converts +other+ to the unit of +self+.
|
91
96
|
# * If the +other+ param is Phys::Quantity,
|
92
97
|
# +other+ is converted to the unit of +self+.
|
@@ -101,7 +106,7 @@ module Phys
|
|
101
106
|
self.class.new( val, @expr, @unit )
|
102
107
|
end
|
103
108
|
|
104
|
-
# Subtraction
|
109
|
+
# Subtraction.
|
105
110
|
# Before the operation, it converts +other+ to the unit of +self+.
|
106
111
|
# * If the +other+ param is Phys::Quantity,
|
107
112
|
# +other+ is converted to the unit of +self+.
|
@@ -116,32 +121,32 @@ module Phys
|
|
116
121
|
self.class.new( val, @expr, @unit )
|
117
122
|
end
|
118
123
|
|
119
|
-
# @return [Phys::Quantity]
|
124
|
+
# @return [Phys::Quantity] Abs. in the unit of +self+.
|
120
125
|
def abs
|
121
126
|
self.class.new( @val.abs, @expr, @unit )
|
122
127
|
end
|
123
128
|
|
124
|
-
# @return [Phys::Quantity]
|
129
|
+
# @return [Phys::Quantity] Abs2. in the squared unit of +self+.
|
125
130
|
def abs2
|
126
131
|
self**2
|
127
132
|
end
|
128
133
|
|
129
|
-
# @return [Phys::Quantity]
|
134
|
+
# @return [Phys::Quantity] Ceil. in the unit of +self+.
|
130
135
|
def ceil
|
131
136
|
self.class.new( @val.ceil, @expr, @unit )
|
132
137
|
end
|
133
138
|
|
134
|
-
# @return [Phys::Quantity]
|
139
|
+
# @return [Phys::Quantity] Round. in the unit of +self+.
|
135
140
|
def round
|
136
141
|
self.class.new( @val.round, @expr, @unit )
|
137
142
|
end
|
138
143
|
|
139
|
-
# @return [Phys::Quantity]
|
144
|
+
# @return [Phys::Quantity] Floor. in the unit of +self+.
|
140
145
|
def floor
|
141
146
|
self.class.new( @val.floor, @expr, @unit )
|
142
147
|
end
|
143
148
|
|
144
|
-
# @return [Phys::Quantity]
|
149
|
+
# @return [Phys::Quantity] Truncate. in the unit of +self+.
|
145
150
|
def truncate
|
146
151
|
self.class.new( @val.truncate, @expr, @unit )
|
147
152
|
end
|
@@ -153,58 +158,81 @@ module Phys
|
|
153
158
|
end
|
154
159
|
|
155
160
|
# Unary Minus.
|
156
|
-
# @return [Phys::Quantity]
|
161
|
+
# @return [Phys::Quantity] in the unit of +self+.
|
157
162
|
def -@
|
158
163
|
self.class.new( -@val, @expr, @unit )
|
159
164
|
end
|
160
165
|
|
161
166
|
# Comparison of quantities.
|
162
167
|
# Before the comparison, it converts +other+ to the unit of +self+.
|
168
|
+
# @param [Phys::Quantity] other
|
163
169
|
# @return [Integer]
|
170
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
164
171
|
def <=> (other)
|
165
172
|
@val <=> @unit.convert(other)
|
166
173
|
end
|
167
174
|
|
168
|
-
# Comparison
|
175
|
+
# Comparison. Returns +true+ if +self+ has the same value as +other+.
|
169
176
|
# Before the comparison, it converts +other+ to the unit of +self+.
|
177
|
+
# @param [Phys::Quantity] other
|
170
178
|
# @return [Boolean]
|
179
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
171
180
|
def == (other)
|
172
181
|
@val == @unit.convert(other)
|
173
182
|
end
|
174
183
|
|
175
|
-
# Comparison
|
184
|
+
# Comparison. Returns +true+ if +self+ is greather-than or equal-to +other+.
|
176
185
|
# Before the comparison, it converts +other+ to the unit of +self+.
|
186
|
+
# @param [Phys::Quantity] other
|
177
187
|
# @return [Boolean]
|
188
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
178
189
|
def >= (other)
|
179
190
|
@val >= @unit.convert(other)
|
180
191
|
end
|
181
192
|
|
182
|
-
# Comparison
|
193
|
+
# Comparison. Returns +true+ if +self+ is less-than or equal-to +other+.
|
183
194
|
# Before the comparison, it converts +other+ to the unit of +self+.
|
195
|
+
# @param [Phys::Quantity] other
|
184
196
|
# @return [Boolean]
|
197
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
185
198
|
def <= (other)
|
186
199
|
@val <= @unit.convert(other)
|
187
200
|
end
|
188
201
|
|
189
|
-
# Comparison
|
202
|
+
# Comparison. Returns +true+ if +self+ is less than +other+.
|
190
203
|
# Before the comparison, it converts +other+ to the unit of +self+.
|
204
|
+
# @param [Phys::Quantity] other
|
191
205
|
# @return [Boolean]
|
206
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
192
207
|
def < (other)
|
193
208
|
@val < @unit.convert(other)
|
194
209
|
end
|
195
210
|
|
196
|
-
# Comparison
|
211
|
+
# Comparison. Returns +true+ if +self+ is greater than +other+.
|
197
212
|
# Before the comparison, it converts +other+ to the unit of +self+.
|
213
|
+
# @param [Phys::Quantity] other
|
198
214
|
# @return [Boolean]
|
215
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
199
216
|
def > (other)
|
200
217
|
@val > @unit.convert(other)
|
201
218
|
end
|
202
219
|
|
220
|
+
# Closeness. Returns +true+ if +self+ and +other+ is close to each other
|
221
|
+
# within +epsilon+ relative to the absolute values.
|
222
|
+
# Before the comparison, it converts +other+ to the unit of +self+.
|
223
|
+
# @param [Phys::Quantity] other
|
224
|
+
# @param [Numeric] epsilon
|
225
|
+
# @return [Boolean]
|
226
|
+
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
227
|
+
def close_to(other,epsilon=Float::EPSILON)
|
228
|
+
other_value = @unit.convert(other)
|
229
|
+
(@val-other_value).abs/(@val.abs+other_value.abs) <= epsilon
|
230
|
+
end
|
231
|
+
|
203
232
|
# Power of a quantity.
|
204
233
|
# @param [Numeric] n
|
205
234
|
# @return [Phys::Quantity] a quantity in the +n+ -powered unit of +self+.
|
206
235
|
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
207
|
-
#
|
208
236
|
def **(n)
|
209
237
|
if @expr.nil?
|
210
238
|
expr = nil
|
@@ -240,7 +268,7 @@ module Phys
|
|
240
268
|
end
|
241
269
|
end
|
242
270
|
|
243
|
-
# Multiplication
|
271
|
+
# Multiplication.
|
244
272
|
# If the +other+ param is *not* Phys::Quantity,
|
245
273
|
# +other+ is regarded as a dimensionless value.
|
246
274
|
# @param [Object] other
|
@@ -258,7 +286,7 @@ module Phys
|
|
258
286
|
end
|
259
287
|
end
|
260
288
|
|
261
|
-
# Division
|
289
|
+
# Division.
|
262
290
|
# If the +other+ param is *not* Phys::Quantity,
|
263
291
|
# +other+ is regarded as a dimensionless value.
|
264
292
|
# @param [Object] other
|
@@ -276,7 +304,7 @@ module Phys
|
|
276
304
|
end
|
277
305
|
end
|
278
306
|
|
279
|
-
# Division
|
307
|
+
# Division more correctly.
|
280
308
|
# If the +other+ param is *not* Phys::Quantity,
|
281
309
|
# +other+ is regarded as a dimensionless value.
|
282
310
|
# @param [Object] other
|
@@ -295,7 +323,7 @@ module Phys
|
|
295
323
|
end
|
296
324
|
alias fdiv quo
|
297
325
|
|
298
|
-
# Division
|
326
|
+
# Division without Modulo.
|
299
327
|
# * If the +other+ param is Phys::Quantity,
|
300
328
|
# +other+ is converted to the unit of +self+,
|
301
329
|
# and returns +div+ of values.
|
@@ -314,7 +342,7 @@ module Phys
|
|
314
342
|
end
|
315
343
|
end
|
316
344
|
|
317
|
-
# Remainder
|
345
|
+
# Remainder.
|
318
346
|
# * If the +other+ param is Phys::Quantity,
|
319
347
|
# +other+ is converted to the unit of +self+,
|
320
348
|
# and returns +remainder+ of values.
|
@@ -333,7 +361,7 @@ module Phys
|
|
333
361
|
end
|
334
362
|
end
|
335
363
|
|
336
|
-
# Modulo
|
364
|
+
# Modulo.
|
337
365
|
# * If the +other+ param is Phys::Quantity,
|
338
366
|
# +other+ is converted to the unit of +self+,
|
339
367
|
# and returns +modulo+ of values.
|
@@ -353,7 +381,7 @@ module Phys
|
|
353
381
|
end
|
354
382
|
alias modulo %
|
355
383
|
|
356
|
-
# Division
|
384
|
+
# Division with Modulo.
|
357
385
|
# * If the +other+ param is Phys::Quantity,
|
358
386
|
# +other+ is converted to the unit of +self+,
|
359
387
|
# and returns +divmod+ of values.
|
@@ -383,7 +411,6 @@ module Phys
|
|
383
411
|
# Returns the quantity converted to a base unit.
|
384
412
|
# @return [Phys::Quantity] a quantity in the base unit.
|
385
413
|
# @raise [Phys::UnitConversionError] if unit conversion is failed.
|
386
|
-
#
|
387
414
|
def to_base_unit
|
388
415
|
unit = @unit.base_unit
|
389
416
|
val = unit.convert(self)
|
@@ -396,7 +423,6 @@ module Phys
|
|
396
423
|
# Conversion to Numeric.
|
397
424
|
# @return [Numeric]
|
398
425
|
# @raise [Phys::UnitConversionError] if the unit is non-dminensionless.
|
399
|
-
#
|
400
426
|
def to_numeric
|
401
427
|
@unit.convert_to_numeric(@val)
|
402
428
|
end
|
@@ -405,7 +431,6 @@ module Phys
|
|
405
431
|
# Conversion to Float.
|
406
432
|
# @return [Float]
|
407
433
|
# @raise [Phys::UnitConversionError] if the unit is non-dminensionless.
|
408
|
-
#
|
409
434
|
def to_f
|
410
435
|
to_numeric.to_f
|
411
436
|
end
|
@@ -414,7 +439,6 @@ module Phys
|
|
414
439
|
# Conversion to Integer.
|
415
440
|
# @return [Integer]
|
416
441
|
# @raise [Phys::UnitConversionError] if the unit is non-dminensionless.
|
417
|
-
#
|
418
442
|
def to_i
|
419
443
|
to_numeric.to_i
|
420
444
|
end
|
@@ -424,7 +448,6 @@ module Phys
|
|
424
448
|
# Conversion to Rational.
|
425
449
|
# @return [Rational]
|
426
450
|
# @raise [Phys::UnitConversionError] if the unit is non-dminensionless.
|
427
|
-
#
|
428
451
|
def to_r
|
429
452
|
to_numeric.to_r
|
430
453
|
end
|
@@ -432,7 +455,6 @@ module Phys
|
|
432
455
|
|
433
456
|
# Conversion to String.
|
434
457
|
# @return [String]
|
435
|
-
#
|
436
458
|
def to_s
|
437
459
|
if @expr
|
438
460
|
expr = ",'" +@expr+"'"
|
@@ -444,7 +466,6 @@ module Phys
|
|
444
466
|
|
445
467
|
# Inspect String.
|
446
468
|
# @return [String]
|
447
|
-
#
|
448
469
|
def inspect
|
449
470
|
if @expr
|
450
471
|
expr = "," +@expr.inspect
|
data/lib/phys/units/unit.rb
CHANGED
@@ -212,7 +212,7 @@ module Phys
|
|
212
212
|
end
|
213
213
|
|
214
214
|
def base_unit
|
215
|
-
Unit.new(1,
|
215
|
+
Unit.new(1,dimensionless_deleted)
|
216
216
|
end
|
217
217
|
|
218
218
|
# Unit operation
|
@@ -294,40 +294,40 @@ module Phys
|
|
294
294
|
end
|
295
295
|
|
296
296
|
def *(x)
|
297
|
-
|
297
|
+
x = Unit.cast(x)
|
298
298
|
if scalar?
|
299
|
-
return
|
300
|
-
elsif
|
299
|
+
return x
|
300
|
+
elsif x.scalar?
|
301
301
|
return self
|
302
302
|
end
|
303
|
-
check_operable2(
|
304
|
-
dims = dimension_binop(
|
305
|
-
factor = self.factor *
|
303
|
+
check_operable2(x)
|
304
|
+
dims = dimension_binop(x){|a,b| a+b}
|
305
|
+
factor = self.factor * x.factor
|
306
306
|
Unit.new(factor,dims)
|
307
307
|
end
|
308
308
|
|
309
309
|
def /(x)
|
310
|
-
|
310
|
+
x = Unit.cast(x)
|
311
311
|
if scalar?
|
312
|
-
return
|
313
|
-
elsif
|
312
|
+
return x.inv
|
313
|
+
elsif x.scalar?
|
314
314
|
return self
|
315
315
|
end
|
316
|
-
check_operable2(
|
317
|
-
dims = dimension_binop(
|
318
|
-
factor = self.factor /
|
316
|
+
check_operable2(x)
|
317
|
+
dims = dimension_binop(x){|a,b| a-b}
|
318
|
+
factor = self.factor / x.factor
|
319
319
|
Unit.new(factor,dims)
|
320
320
|
end
|
321
321
|
|
322
322
|
def rdiv(x)
|
323
|
-
|
323
|
+
x = Unit.cast(x)
|
324
324
|
if scalar?
|
325
|
-
return
|
326
|
-
elsif
|
325
|
+
return x.inv
|
326
|
+
elsif x.scalar?
|
327
327
|
return self
|
328
328
|
end
|
329
|
-
check_operable2(
|
330
|
-
dims = dimension_binop(
|
329
|
+
check_operable2(x)
|
330
|
+
dims = dimension_binop(x){|a,b| a-b}
|
331
331
|
factor = Rational(self.factor,x.factor)
|
332
332
|
Unit.new(factor,dims)
|
333
333
|
end
|
@@ -342,6 +342,10 @@ module Phys
|
|
342
342
|
Unit.new(Rational(1,self.factor), dims)
|
343
343
|
end
|
344
344
|
|
345
|
+
def self.inv(x)
|
346
|
+
Unit.cast(x).inv
|
347
|
+
end
|
348
|
+
|
345
349
|
def **(x)
|
346
350
|
check_operable
|
347
351
|
m = Utils.as_numeric(x)
|