Skip to content
Browse files

completely refactor definitions for standard units. When possible uni…

…ts are defined according to their actual definitions. Rational scalars are also used when possible (and in some cases, values are approximated by their rational equivalents). This minimizes problems with round-off errors. In general it is much easier to read and understand the definitions, but recognize that some units need to be defined immediately so that they can be used to define other units. This ends up causing the parser to repeatedly call Unit.setup, which is still somewhat inefficient.
  • Loading branch information...
1 parent 08f9375 commit ce2e3c733bf28c209b3e5f912421a26fa5a5e662 @olbrich committed Jan 2, 2012
Showing with 704 additions and 207 deletions.
  1. +704 −207 lib/ruby_units/unit_definitions/standard.rb
View
911 lib/ruby_units/unit_definitions/standard.rb
@@ -1,208 +1,705 @@
-unit_definitions = {
-
- # length units
- '<inch>' => [%w{in inch inches "}, Rational(254,10_000), :length, %w{<meter>}],
- '<foot>' => [%w{ft foot feet '}, Rational(3048,10_000), :length, %w{<meter>}],
- '<yard>' => [%w{yd yard yards}, 0.9144, :length, %w{<meter>}],
- '<mile>' => [%w{mi mile miles}, 1609.344, :length, %w{<meter>}],
- '<naut-mile>' => [%w{nmi}, 1852, :length, %w{<meter>}],
- '<league>' => [%w{league leagues}, 4828, :length, %w{<meter>}],
- '<furlong>' => [%w{furlong furlongs}, 201.2, :length, %w{<meter>}],
- '<rod>' => [%w{rd rod rods}, 5.029, :length, %w{<meter>}],
- '<mil>' => [%w{mil mils}, 0.0000254, :length, %w{<meter>}],
- '<angstrom>' => [%w{ang angstrom angstroms}, Rational(1,1e10), :length, %w{<meter>}],
- '<fathom>' => [%w{fathom fathoms}, 1.829, :length, %w{<meter>}],
- '<pica>' => [%w{pica picas}, 0.004217, :length, %w{<meter>}],
- '<point>' => [%w{pt point points}, 0.0003514, :length, %w{<meter>}],
- '<redshift>' => [%w{z red-shift}, 1.302773e26, :length, %w{<meter>}],
- '<AU>' => [%w{AU astronomical-unit}, 149597900000, :length, %w{<meter>}],
- '<light-second>' => [%w{ls light-second}, 299792500, :length, %w{<meter>}],
- '<light-minute>' => [%w{lmin light-minute}, 17987550000, :length, %w{<meter>}],
- '<light-year>' => [%w{ly light-year}, 9460528000000000, :length, %w{<meter>}],
- '<parsec>' => [%w{pc parsec parsecs}, 30856780000000000, :length, %w{<meter>}],
-
- #mass
- '<AMU>' => [%w{u AMU amu}, 6.0221415e26, :mass, %w{<kilogram>}],
- '<dalton>' => [%w{Da Dalton Daltons dalton daltons}, 6.0221415e26, :mass, %w{<kilogram>}],
- '<slug>' => [%w{slug slugs}, 14.5939029, :mass, %w{<kilogram>}],
- '<short-ton>' => [%w{tn ton}, 907.18474, :mass, %w{<kilogram>}],
- '<metric-ton>' => [%w{tonne}, 1000, :mass, %w{<kilogram>}],
- '<carat>' => [%w{ct carat carats}, 0.0002, :mass, %w{<kilogram>}],
- '<pound>' => [%w{lbs lb pound pounds #}, Rational(8171193714040401,18014398509481984), :mass, %w{<kilogram>}],
- '<ounce>' => [%w{oz ounce ounces}, Rational(8171193714040401,288230376151711744), :mass, %w{<kilogram>}],
- '<gram>' => [%w{g gram grams gramme grammes},Rational(1,1e3),:mass, %w{<kilogram>}],
-
- #area
- '<hectare>' => [%w{hectare}, 10000, :area, %w{<meter> <meter>}],
- '<acre>' => [%w(acre acres), 4046.85642, :area, %w{<meter> <meter>}],
- '<sqft>' => [%w(sqft), 1, :area, %w{<feet> <feet>}],
-
- #volume
- '<liter>' => [%w{l L liter liters litre litres}, Rational(1,1e3), :volume, %w{<meter> <meter> <meter>}],
- '<gallon>' => [%w{gal gallon gallons}, 0.0037854118, :volume, %w{<meter> <meter> <meter>}],
- '<quart>' => [%w{qt quart quarts}, 0.00094635295, :volume, %w{<meter> <meter> <meter>}],
- '<pint>' => [%w{pt pint pints}, 0.000473176475, :volume, %w{<meter> <meter> <meter>}],
- '<cup>' => [%w{cu cup cups}, 0.000236588238, :volume, %w{<meter> <meter> <meter>}],
- '<fluid-ounce>' => [%w{floz fluid-ounce}, 2.95735297e-5, :volume, %w{<meter> <meter> <meter>}],
- '<tablespoon>' => [%w{tbs tbsp tablespoon tablespoons}, 1.47867648e-5, :volume, %w{<meter> <meter> <meter>}],
- '<teaspoon>' => [%w{tsp teaspoon teaspoons}, 4.92892161e-6, :volume, %w{<meter> <meter> <meter>}],
-
- #volumetric flow
- '<cfm>' => [%w{cfm CFM CFPM}, (18435447/39062500000), :volumetric_flow, %w{<meter> <meter> <meter>}, %w{<second>}],
-
- #speed
- '<kph>' => [%w{kph}, 0.277777778, :speed, %w{<meter>}, %w{<second>}],
- '<mph>' => [%w{mph}, 0.44704, :speed, %w{<meter>}, %w{<second>}],
- '<knot>' => [%w{kt kn kts knot knots}, 0.514444444, :speed, %w{<meter>}, %w{<second>}],
- '<fps>' => [%w{fps}, 0.3048, :speed, %w{<meter>}, %w{<second>}],
-
- #acceleration
- '<gee>' => [%w{gee}, 9.80655, :acceleration, %w{<meter>}, %w{<second> <second>}],
-
-
- #temperature_difference
- '<celsius>' => [%w{degC celsius celsius centigrade}, 1, :temperature, %w{<kelvin>}],
- '<fahrenheit>' => [%w{degF fahrenheit}, Rational(1,1.8), :temperature, %w{<kelvin>}],
- '<rankine>' => [%w{degR rankine}, Rational(1,1.8), :temperature, %w{<kelvin>}],
- '<tempC>' => [%w{tempC}, 1, :temperature, %w{<tempK>}],
- '<tempF>' => [%w{tempF}, Rational(1,1.8), :temperature, %w{<tempK>}],
- '<tempR>' => [%w{tempR}, Rational(1,1.8), :temperature, %w{<tempK>}],
-
- #time
- '<minute>' => [%w{min minute minutes}, 60, :time, %w{<second>}],
- '<hour>' => [%w{h hr hrs hour hours}, 3600, :time, %w{<second>}],
- '<day>' => [%w{d day days}, 3600*24, :time, %w{<second>}],
- '<week>' => [%w{wk week weeks}, 7*3600*24, :time, %w{<second>}],
- '<fortnight>' => [%w{fortnight fortnights}, 1209600, :time, %W{<second>}],
- '<year>' => [%w{y yr year years annum}, 31556926, :time, %w{<second>}],
- '<decade>' => [%w{decade decades}, 315569260, :time, %w{<second>}],
- '<century>' => [%w{century centuries}, 3155692600, :time, %w{<second>}],
-
- #pressure
- '<pascal>' => [%w{Pa pascal Pascal}, 1, :pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
- '<bar>' => [%w{bar bars}, 100000, :pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
- '<mmHg>' => [%w{mmHg}, 133.322368,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
- '<inHg>' => [%w{inHg}, 3386.3881472,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
- '<torr>' => [%w{torr}, 133.322368,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
- '<bar>' => [%w{bar}, 100000,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
- '<atm>' => [%w{atm ATM atmosphere atmospheres}, 101325,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
- '<psi>' => [%w{psi}, 6894.76,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
- '<cmh2o>' => [%w{cmH2O}, 98.0638,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
- '<inh2o>' => [%w{inH2O}, 249.082052,:pressure, %w{<kilogram>},%w{<meter> <second> <second>}],
-
- #viscosity
- '<poise>' => [%w{P poise}, Rational(1,10), :viscosity, %w{<kilogram>},%w{<meter> <second>} ],
- '<stokes>' => [%w{St stokes}, Rational(1,1e4), :viscosity, %w{<meter> <meter>}, %w{<second>}],
-
- #substance
-
- #concentration
- '<molar>' => [%w{M molar}, 1000, :concentration, %w{<mole>}, %w{<meter> <meter> <meter>}],
- '<wtpercent>' => [%w{wt% wtpercent}, 10, :concentration, %w{<kilogram>}, %w{<meter> <meter> <meter>}],
-
- #activity
- '<katal>' => [%w{kat katal Katal}, 1, :activity, %w{<mole>}, %w{<second>}],
- '<unit>' => [%w{U enzUnit}, 16.667e-16, :activity, %w{<mole>}, %w{<second>}],
-
- #capacitance
- '<farad>' => [%w{F farad Farad}, 1, :capacitance, %w{<second> <second> <second> <second> <ampere> <ampere>}, %w{<kilogram> <meter> <meter>}],
-
- #charge
- '<coulomb>' => [%w{C coulomb Coulomb}, 1, :charge, %w{<ampere> <second>}],
-
- #current
-
- #conductance
- '<siemens>' => [%w{S Siemens siemens}, 1, :resistance, %w{<second> <second> <second> <ampere> <ampere>}, %w{<kilogram> <meter> <meter>}],
-
- #inductance
- '<henry>' => [%w{H Henry henry}, 1, :inductance, %w{<meter> <meter> <kilogram>}, %w{<second> <second> <ampere> <ampere>}],
-
- #potential
- '<volt>' => [%w{V Volt volt volts}, 1, :potential, %w{<meter> <meter> <kilogram>}, %w{<second> <second> <second> <ampere>}],
-
- #resistance
- '<ohm>' => [%w{Ohm ohm}, 1, :resistance, %w{<meter> <meter> <kilogram>},%w{<second> <second> <second> <ampere> <ampere>}],
-
- #magnetism
- '<weber>' => [%w{Wb weber webers}, 1, :magnetism, %w{<meter> <meter> <kilogram>}, %w{<second> <second> <ampere>}],
- '<tesla>' => [%w{T tesla teslas}, 1, :magnetism, %w{<kilogram>}, %w{<second> <second> <ampere>}],
- '<gauss>' => [%w{G gauss}, Rational(1,1e4), :magnetism, %w{<kilogram>}, %w{<second> <second> <ampere>}],
- '<maxwell>' => [%w{Mx maxwell maxwells}, Rational(1,1e8), :magnetism, %w{<meter> <meter> <kilogram>}, %w{<second> <second> <ampere>}],
- '<oersted>' => [%w{Oe oersted oersteds}, 250.0/Math::PI, :magnetism, %w{<ampere>}, %w{<meter>}],
-
- #energy
- '<joule>' => [%w{J joule Joule joules}, 1, :energy, %w{<kilogram> <meter> <meter>}, %w{<second> <second>}],
- '<erg>' => [%w{erg ergs}, Rational(1,1e7), :energy, %w{<meter> <meter> <kilogram>}, %w{<second> <second>}],
- '<btu>' => [%w{BTU btu BTUs}, 1055.056, :energy, %w{<meter> <meter> <kilogram>}, %w{<second> <second>}],
- '<calorie>' => [%w{cal calorie calories}, 4.18400, :energy,%w{<meter> <meter> <kilogram>}, %w{<second> <second>}],
- '<Calorie>' => [%w{Cal Calorie Calories}, 4184.00, :energy,%w{<meter> <meter> <kilogram>}, %w{<second> <second>}],
- '<therm-US>' => [%w{th therm therms Therm}, 105480400, :energy,%w{<meter> <meter> <kilogram>}, %w{<second> <second>}],
-
- #force
- '<newton>' => [%w{N Newton newton}, 1, :force, %w{<kilogram> <meter>}, %w{<second> <second>}],
- '<dyne>' => [%w{dyn dyne}, Rational(1,1e5), :force, %w{<kilogram> <meter>}, %w{<second> <second>}],
- '<pound-force>' => [%w{lbf pound-force}, 4.448222, :force, %w{<kilogram> <meter>}, %w{<second> <second>}],
-
- #frequency
- '<hertz>' => [%w{Hz hertz Hertz}, 1, :frequency, %w{<1>}, %{<second>}],
-
- #angle
- '<degree>' => [%w{deg degree degrees}, Math::PI / 180.0, :angle, %w{<radian>}],
- '<grad>' => [%w{grad gradian grads}, Math::PI / 200.0, :angle, %w{<radian>}],
-
- #rotation
- '<rotation>' => [%w{rotation}, 2.0*Math::PI, :angle, %w{<radian>}],
- '<rpm>' => [%w{rpm}, 2.0*Math::PI / 60.0, :angular_velocity, %w{<radian>}, %w{<second>}],
-
- #memory
- '<bit>' => [%w{b bit}, 0.125, :memory, %w{<byte>}],
-
- #currency
- '<cents>' => [%w{cents}, Rational(1,100), :currency, %w{<dollar>}],
-
- #luminosity
- '<lumen>' => [%w{lm lumen}, 1, :luminous_power, %w{<candela> <steradian>}],
- '<lux>' => [%w{lux}, 1, :illuminance, %w{<candela> <steradian>}, %w{<meter> <meter>}],
-
- #power
- '<watt>' => [%w{W watt watts}, 1, :power, %w{<kilogram> <meter> <meter>}, %w{<second> <second> <second>}],
- '<horsepower>' => [%w{hp horsepower}, 745.699872, :power, %w{<kilogram> <meter> <meter>}, %w{<second> <second> <second>}],
-
- #radiation
- '<gray>' => [%w{Gy gray grays}, 1, :radiation, %w{<meter> <meter>}, %w{<second> <second>}],
- '<roentgen>' => [%w{R roentgen}, 0.009330, :radiation, %w{<meter> <meter>}, %w{<second> <second>}],
- '<sievert>' => [%w{Sv sievert sieverts}, 1, :radiation, %w{<meter> <meter>}, %w{<second> <second>}],
- '<becquerel>' => [%w{Bq bequerel bequerels}, 1, :radiation, %w{<1>},%w{<second>}],
- '<curie>' => [%w{Ci curie curies}, 3.7e10, :radiation, %w{<1>},%w{<second>}],
-
- # rate
- '<cpm>' => [%w{cpm}, Rational(1,60), :rate, %w{<count>},%w{<second>}],
- '<dpm>' => [%w{dpm}, Rational(1,60), :rate, %w{<count>},%w{<second>}],
- '<bpm>' => [%w{bpm}, Rational(1,60), :rate, %w{<count>},%w{<second>}],
-
- #resolution / typography
- '<dot>' => [%w{dot dots}, 1, :resolution, %w{<each>}],
- '<pixel>' => [%w{pixel px}, 1, :resolution, %w{<each>}],
- '<ppi>' => [%w{ppi}, 1, :resolution, %w{<pixel>}, %w{<inch>}],
- '<dpi>' => [%w{dpi}, 1, :typography, %w{<dot>}, %w{<inch>}],
- '<pica>' => [%w{pica}, 0.00423333333 , :typography, %w{<meter>}],
- '<point>' => [%w{point pt}, 0.000352777778, :typography, %w{<meter>}],
-
- #other
- '<cell>' => [%w{cells cell}, 1, :counting, %w{<each>}],
- '<count>' => [%w{count}, 1, :counting, %w{<each>}],
- '<base-pair>' => [%w{bp}, 1, :counting, %w{<each>}],
- '<nucleotide>' => [%w{nt}, 1, :counting, %w{<each>}],
- '<molecule>' => [%w{molecule molecules}, 1, :counting, %w{<each>}],
- '<dozen>' => [%w{doz dz dozen},12,:prefix_only, %w{<1>}],
- '<percent>' => [%w{% percent}, Rational(1,100), :prefix_only, %w{<1>}],
- '<ppm>' => [%w{ppm},Rational(1,1e6),:prefix_only, %w{<1>}],
- '<ppt>' => [%w{ppt},Rational(1,1e9),:prefix_only, %w{<1>}],
- '<gross>' => [%w{gr gross},144, :prefix_only, %w{<dozen> <dozen>}]
-} # doc
-
-
-unit_definitions.each do |unit, definition|
- Unit.define(Unit::Definition.new(unit, definition))
+# length units
+
+Unit.define!('inch') do |inch|
+ inch.definition = Unit('254/10000 meter')
+ inch.aliases = %w{in inch inches "}
+end
+
+Unit.define!('foot') do |foot|
+ foot.definition = Unit('12 inches')
+ foot.aliases = %w{ft foot feet '}
+end
+
+Unit.define('survey-foot') do |sft|
+ sft.definition = Unit('1200/3937 meter')
+ sft.aliases = %w{sft sfoot sfeet}
+end
+
+Unit.define('yard') do |yard|
+ yard.definition = Unit('3 ft')
+ yard.aliases = %w{yd yard yards}
+end
+
+Unit.define!('mile') do |mile|
+ mile.definition = Unit('5280 ft')
+ mile.aliases = %w{mi mile miles}
+end
+
+Unit.define!('naut-mile') do |naut|
+ naut.definition = Unit('1852 m')
+ naut.aliases = %w{nmi M NM}
+end
+
+# on land
+Unit.define('league') do |league|
+ league.definition = Unit('3 miles')
+ league.aliases = %w{league leagues}
+end
+
+# at sea
+Unit.define('naut-league') do |naut_league|
+ naut_league.definition = Unit('3 nmi')
+ naut_league.aliases = %w{nleague nleagues}
+end
+
+Unit.define('furlong') do |furlong|
+ furlong.definition = Unit('1/8 mile')
+ furlong.aliases = %w{fur furlong furlongs}
+end
+
+Unit.define('rod') do |rod|
+ rod.definition = Unit('33/2 feet')
+ rod.aliases = %w{rd rod rods}
+end
+
+Unit.define('fathom') do |fathom|
+ fathom.definition = Unit('6 ft')
+ fathom.aliases = %w{fathom fathoms}
+end
+
+Unit.define('mil') do |mil|
+ mil.definition = Unit('1/1000 inch')
+ mil.aliases = %w{mil mils}
+end
+
+Unit.define('angstrom') do |ang|
+ ang.definition = Unit('1/10 nm')
+ ang.aliases = %w{ang angstrom angstroms}
+end
+
+# typesetting
+
+Unit.define!('point') do |point|
+ point.definition = Unit('1/72 ft')
+ point.aliases = %w{pt point points}
+end
+
+Unit.define('pica') do |pica|
+ pica.definition = Unit('12 pt')
+ pica.aliases = %w{P pica picas}
+end
+
+Unit.define!('dot') do |dot|
+ dot.definition = Unit('1 each')
+ dot.aliases = %w{dot dots}
+ dot.kind = :counting
+end
+
+Unit.define!('pixel') do |pixel|
+ pixel.definition = Unit('1 each')
+ pixel.aliases = %w{px pixel pixels}
+ pixel.kind = :counting
+end
+
+Unit.define('ppi') do |ppi|
+ ppi.definition = Unit('1 pixel/inch')
+end
+
+Unit.define('dpi') do |dpi|
+ dpi.definition = Unit('1 dot/inch')
+end
+
+# Mass
+
+avagadro_constant = Unit('6.02214129e23 1/mol')
+
+Unit.define!('AMU') do |amu|
+ amu.definition = Unit('12 kg/mol') / (12 * avagadro_constant)
+ amu.aliases = %w{u AMU amu}
+end
+
+Unit.define('dalton') do |dalton|
+ dalton.definition = Unit('1 amu')
+ dalton.aliases = %w{Da dalton daltons}
+end
+
+standard_gravitation = Unit('9.80665 m/s^2')
+
+Unit.define('metric-ton') do |mton|
+ mton.definition = Unit('1000 kg')
+ mton.aliases = %w{tonne}
+end
+
+# defined as a rational number to preserve accuracy and minimize round-off errors during
+# calculations
+Unit.define!('pound') do |pound|
+ pound.definition = Unit(Rational(8171193714040401,18014398509481984), 'kg')
+ pound.aliases = %w{lbs lb lbm pound-mass pound pounds #}
+end
+
+Unit.define('ounce') do |ounce|
+ ounce.definition = Unit('1/16 lbs')
+ ounce.aliases = %w{oz ounce ounces}
+end
+
+Unit.define('gram') do |gram|
+ gram.definition = Unit('1/1000 kg')
+ gram.aliases = %w{g gram grams gramme grammes}
+end
+
+Unit.define('short-ton') do |ton|
+ ton.definition = Unit('2000 lbs')
+ ton.aliases = %w{ton tn}
+end
+
+Unit.define('carat') do |carat|
+ carat.definition = Unit('1/5000 kg')
+ carat.aliases = %w{ct carat carats}
+end
+
+# time
+
+Unit.define!('minute') do |min|
+ min.definition = Unit('60 seconds')
+ min.aliases = %w{min minute minutes}
+end
+
+Unit.define!('hour') do |hour|
+ hour.definition = Unit('60 minutes')
+ hour.aliases = %w{h hr hrs hour hours}
+end
+
+Unit.define!('day') do |day|
+ day.definition = Unit('24 hours')
+ day.aliases = %w{d day days}
+end
+
+Unit.define!('week') do |week|
+ week.definition = Unit('7 days')
+ week.aliases = %w{wk week weeks}
+end
+
+Unit.define('fortnight') do |fortnight|
+ fortnight.definition = Unit('2 weeks')
+ fortnight.aliases = %w{fortnight fortnights}
+end
+
+Unit.define!('year') do |year|
+ year.definition = Unit('31556926 seconds') # works out to 365.24219907407405 days
+ year.aliases = %w{y yr year years annum}
+end
+
+Unit.define('decade') do |decade|
+ decade.definition = Unit('10 years')
+ decade.aliases = %w{decade decades}
+end
+
+Unit.define('century') do |century|
+ century.definition = Unit('100 years')
+ century.aliases = %w{century centuries}
+end
+
+# area
+
+Unit.define('hectare') do |hectare|
+ hectare.definition = Unit('10000 m^2')
+end
+
+Unit.define('acre') do |acre|
+ acre.definition = Unit('1 mi')**2 / 640
+ acre.aliases = %w{acre acres}
+end
+
+Unit.define('sqft') do |sqft|
+ sqft.definition = Unit('1 ft^2')
+end
+
+Unit.define('sqin') do |sqin|
+ sqin.definition = Unit('1 in^2')
+end
+
+# volume
+
+Unit.define('liter') do |liter|
+ liter.definition = Unit('1/1000 m^3')
+ liter.aliases = %w{l L liter liters litre litres}
+end
+
+Unit.define!('gallon') do |gallon|
+ gallon.definition = Unit('231 in^3')
+ gallon.aliases = %w{gal gallon gallons}
+end
+
+Unit.define('quart') do |quart|
+ quart.definition = Unit('1/4 gal')
+ quart.aliases = %w{qt quart quarts}
+end
+
+Unit.define('pint') do |pint|
+ pint.definition = Unit('1/8 gal')
+ pint.aliases = %w{pt pint pints}
+end
+
+Unit.define('cup') do |cup|
+ cup.definition = Unit('1/16 gal')
+ cup.aliases = %w{cu cup cups}
+end
+
+Unit.define!('fluid-ounce') do |floz|
+ floz.definition = Unit('1/128 gal')
+ floz.aliases = %w{floz fluid-ounce}
+end
+
+Unit.define!('tablespoon') do |tbsp|
+ tbsp.definition = Unit('1/2 floz')
+ tbsp.aliases = %w{tbs tbsp tablespoon tablespoons}
+end
+
+Unit.define('teaspoon') do |tsp|
+ tsp.definition = Unit('1/3 tablespoon')
+ tsp.aliases = %w{tsp teaspoon teaspoons}
+end
+
+# volumetric flow
+
+Unit.define('cfm') do |cfm|
+ cfm.definition = Unit('1 ft^3/minute')
+ cfm.aliases = %w{cfm CFM CFPM}
+end
+
+# speed
+
+Unit.define('kph') do |kph|
+ kph.definition = Unit('1 kilometer/hour')
+end
+
+Unit.define('mph') do |mph|
+ mph.definition = Unit('1 mile/hour')
+end
+
+Unit.define('fps') do |fps|
+ fps.definition = Unit('1 foot/second')
+end
+
+Unit.define('knot') do |knot|
+ knot.definition = Unit('1 nmi/hour')
+ knot.aliases = %w{kt kn kts knot knots}
+end
+
+Unit.define!('gee') do |gee|
+ # approximated as a rational number to minimize round-off errors
+ gee.definition = Unit(Rational(5520596865723767,562949953421312), 'm/s^2') # equivalent to 9.80655 m/s^2
+ gee.aliases = %w{gee standard-gravitation}
+end
+
+# temperature differences
+
+Unit.define!('newton') do |newton|
+ newton.definition = Unit('1 kg*m/s^2')
+ newton.aliases = %w{N newton newtons}
+end
+
+Unit.define('dyne') do |dyne|
+ dyne.definition = Unit('1/100000 N')
+ dyne.aliases = %w{dyn dyne}
+end
+
+Unit.define!('pound-force') do |lbf|
+ lbf.definition = Unit('1 lb') * Unit('1 gee')
+ lbf.aliases = %w{lbf pound-force}
+end
+
+Unit.define('poundal') do |poundal|
+ poundal.definition = Unit('1 lb') * Unit('1 ft/s^2')
+ poundal.aliases = %w{pdl poundal poundals}
+end
+
+temp_convert_factor = Rational(2501999792983609,4503599627370496) # approximates 1/1.8
+
+Unit.define('celsius') do |celsius|
+ celsius.definition = Unit('1 degK')
+ celsius.aliases = %w{degC celsius centigrade}
+end
+
+Unit.define!('fahrenheit') do |fahrenheit|
+ fahrenheit.definition = Unit(temp_convert_factor, 'degK')
+ fahrenheit.aliases = %w{degF fahrenheit}
+end
+
+Unit.define('rankine') do |rankine|
+ rankine.definition = Unit('1 degF')
+ rankine.aliases = %w{degR rankine}
+end
+
+Unit.define('tempC') do |tempC|
+ tempC.definition = Unit('1 tempK')
+end
+
+Unit.define!('tempF') do |tempF|
+ tempF.definition = Unit(temp_convert_factor, 'tempK')
+end
+
+Unit.define('tempR') do |tempR|
+ tempR.definition = Unit('1 tempF')
+end
+
+# astronomy
+
+speed_of_light = Unit('299792458 m/s')
+
+Unit.define('light-second') do |ls|
+ ls.definition = Unit('1 s') * speed_of_light
+ ls.aliases = %w{ls lsec light-second}
+end
+
+Unit.define('light-minute') do |lmin|
+ lmin.definition = Unit('1 min') * speed_of_light
+ lmin.aliases = %w{lmin light-minute}
+end
+
+Unit.define!('light-year') do |ly|
+ ly.definition = Unit('1 y') * speed_of_light
+ ly.aliases = %w{ly light-year}
+end
+
+Unit.define('parsec') do |parsec|
+ parsec.definition = Unit('3.26163626 ly')
+ parsec.aliases = %w{pc parsec parsecs}
+end
+
+# once was '149597900000 m' but there appears to be a more accurate estimate according to wikipedia
+# see http://en.wikipedia.org/wiki/Astronomical_unit
+Unit.define('AU') do |au|
+ au.definition = Unit('149597870700 m')
+ au.aliases = %w{AU astronomical-unit}
+end
+
+Unit.define('redshift') do |red|
+ red.definition = Unit('1.302773e26 m')
+ red.aliases = %w{z red-shift}
+end
+
+# mass
+
+Unit.define('slug') do |slug|
+ slug.definition = Unit('1 lbf*s^2/ft')
+ slug.aliases = %w{slug slugs}
+end
+
+# pressure
+
+Unit.define!('pascal') do |pascal|
+ pascal.definition = Unit('1 kg/m*s^2')
+ pascal.aliases = %w{Pa pascal pascals}
+end
+
+Unit.define('bar') do |bar|
+ bar.definition = Unit('100 kPa')
+ bar.aliases = %w{bar bars}
+end
+
+Unit.define!('atm') do |atm|
+ atm.definition = Unit('101325 Pa')
+ atm.aliases = %w{atm ATM atmosphere atmospheres}
+end
+
+Unit.define('mmHg') do |mmhg|
+ density_of_mercury = Unit('7653360911758079/562949953421312 g/cm^3') # 13.5951 g/cm^3 at 0 tempC
+ mmhg.definition = Unit('1 mm') * Unit('1 gee') * density_of_mercury
+end
+
+Unit.define('inHg') do |inhg|
+ density_of_mercury = Unit('7653360911758079/562949953421312 g/cm^3') # 13.5951 g/cm^3 at 0 tempC
+ inhg.definition = Unit('1 in') * Unit('1 gee') * density_of_mercury
+end
+
+Unit.define('torr') do |torr|
+ torr.definition = Unit('1/760 atm')
+ torr.aliases = %w{Torr torr}
+end
+
+Unit.define('psi') do |psi|
+ psi.definition = Unit('1 lbf/in^2')
+end
+
+Unit.define('cmh2o') do |cmh2o|
+ density_of_water = Unit('1 g/cm^3') # at 4 tempC
+ cmh2o.definition = Unit('1 cm') * Unit('1 gee') * density_of_water
+ cmh2o.aliases = %w{cmH2O cmh2o cmAq}
+end
+
+Unit.define('inh2o') do |inh2o|
+ density_of_water = Unit('1 g/cm^3') # at 4 tempC
+ inh2o.definition = Unit('1 in') * Unit('1 gee') * density_of_water
+ inh2o.aliases = %w{inH2O inh2o inAq}
+end
+
+#viscosity
+
+Unit.define('poise') do |poise|
+ poise.definition = Unit('dPa*s')
+ poise.aliases = %w{P poise}
+end
+
+Unit.define('stokes') do |stokes|
+ stokes.definition = Unit('1 cm^2/s')
+ stokes.aliases = %w{St stokes}
+end
+
+# #energy
+
+Unit.define!('joule') do |joule|
+ joule.definition = Unit('1 N*m')
+ joule.aliases = %w{J joule joules}
+end
+
+Unit.define('erg') do |erg|
+ erg.definition = Unit('1 g*cm^2/s^2')
+ erg.aliases = %w{erg ergs}
+end
+
+#power
+
+Unit.define!('watt') do |watt|
+ watt.definition = Unit('1 N*m/s')
+ watt.aliases = %w{W Watt watt watts}
+end
+
+Unit.define('horsepower') do |hp|
+ hp.definition = Unit('33000 ft*lbf/min')
+ hp.aliases = %w{hp horsepower}
+end
+
+# energy
+Unit.define!('btu') do |btu|
+ btu.definition = Unit('2320092679909671/2199023255552 J') # 1055.056 J --- ISO standard
+ btu.aliases = %w{Btu btu Btus btus}
+end
+
+Unit.define('therm') do |therm|
+ therm.definition = Unit('100 kBtu')
+ therm.aliases = %w{thm therm therms Therm}
+end
+
+# "small" calorie
+Unit.define!('calorie') do |calorie|
+ calorie.definition = Unit('4.184 J')
+ calorie.aliases = %w{cal calorie calories}
+end
+
+# "big" calorie
+Unit.define('Calorie') do |calorie|
+ calorie.definition = Unit('1 kcal')
+ calorie.aliases = %w{Cal Calorie Calories}
+end
+
+Unit.define('molar') do |molar|
+ molar.definition = Unit('1 mole/l')
+ molar.aliases = %w{M molar}
+end
+
+# potential
+Unit.define!('volt') do |volt|
+ volt.definition = Unit('1 W/A')
+ volt.aliases = %w{V volt volts}
+end
+
+# capacitance
+Unit.define('farad') do |farad|
+ farad.definition = Unit('1 A*s/V')
+ farad.aliases = %w{F farad farads}
+end
+
+# charge
+Unit.define('coulomb') do |coulomb|
+ coulomb.definition = Unit('1 A*s')
+ coulomb.aliases = %w{C coulomb coulombs}
+end
+
+# conductance
+Unit.define('siemens') do |siemens|
+ siemens.definition = Unit('1 A/V')
+ siemens.aliases = %w{S siemens}
+end
+
+# inductance
+Unit.define('henry') do |henry|
+ henry.definition = Unit('1 J/A^2')
+ henry.aliases = %w{H henry henries}
+end
+
+# resistance
+Unit.define('ohm') do |ohm|
+ ohm.definition = Unit('1 V/A')
+ ohm.aliases = %w{Ohm ohm ohms}
+end
+
+# magnetism
+
+Unit.define('weber') do |weber|
+ weber.definition = Unit('1 V*s')
+ weber.aliases = %w{Wb weber webers}
+end
+
+Unit.define!('tesla') do |tesla|
+ tesla.definition = Unit('1 V*s/m^2')
+ tesla.aliases = %w{T tesla teslas}
+end
+
+Unit.define!('gauss') do |gauss|
+ gauss.definition = Unit('100 microT')
+ gauss.aliases = %w{G gauss}
+end
+
+Unit.define('maxwell') do |maxwell|
+ maxwell.definition = Unit('1 gauss*cm^2')
+ maxwell.aliases = %w{Mx maxwell maxwells}
+end
+
+Unit.define('oersted') do |oersted|
+ oersted.definition = Unit(250.0/Math::PI, 'A/m')
+ oersted.aliases = %w{Oe oersted oersteds}
+end
+
+#activity
+Unit.define!('katal') do |katal|
+ katal.definition = Unit('1 mole/sec')
+ katal.aliases = %w{kat katal}
+end
+
+Unit.define('unit') do |unit|
+ unit.definition = Unit('1/60 microkatal')
+ unit.aliases = %w{U enzUnit}
+end
+
+#frequency
+
+Unit.define('hertz') do |hz|
+ hz.definition = Unit('1 1/s')
+ hz.aliases = %w{Hz hertz}
+end
+
+#angle
+Unit.define('degree') do |deg|
+ deg.definition = Unit(Math::PI / 180.0, 'radian')
+ deg.aliases = %w{deg degree degrees}
+end
+
+Unit.define('grad') do |grad|
+ grad.definition = Unit(Math::PI / 200.0, 'radian')
+ grad.aliases = %w{grad gradian grads}
+end
+
+#rotation
+Unit.define!('rotation') do |rotation|
+ rotation.definition = Unit(2.0*Math::PI, 'radian')
+end
+
+Unit.define('rpm') do |rpm|
+ rpm.definition = Unit('1 rotation/min')
+end
+
+#memory
+Unit.define('bit') do |bit|
+ bit.definition = Unit('1/8 byte')
+ bit.aliases = %w{b bit}
+end
+
+#currency
+Unit.define('cents') do |cents|
+ cents.definition = Unit('1/100 dollar')
+end
+
+#luminosity
+Unit.define!('lumen') do |lumen|
+ lumen.definition = Unit('1 cd*steradian')
+ lumen.aliases = %w{lm lumen}
+end
+
+Unit.define('lux') do |lux|
+ lux.definition = Unit('1 lumen/m^2')
+end
+
+#radiation
+Unit.define('gray') do |gray|
+ gray.definition = Unit('1 J/kg')
+ gray.aliases = %w{Gy gray grays}
+end
+
+Unit.define('roentgen') do |roentgen|
+ roentgen.definition = Unit('2.58e-4 C/kg')
+ roentgen.aliases = %w{R roentgen}
+end
+
+Unit.define('sievert') do |sievert|
+ sievert.definition = Unit('1 J/kg')
+ sievert.aliases = %w{Sv sievert sieverts}
+end
+
+Unit.define!('becquerel') do |becquerel|
+ becquerel.definition = Unit('1 1/s')
+ becquerel.aliases = %w{Bq becquerel becquerels}
+end
+
+Unit.define('curie') do |curie|
+ curie.definition = Unit('37 GBq')
+ curie.aliases = %w{Ci curie curies}
+end
+
+Unit.define!('count') do |count|
+ count.definition = Unit('1 each')
+ count.kind = :counting
+end
+
+# rate
+Unit.define('cpm') do |cpm|
+ cpm.definition = Unit('1 count/min')
+end
+
+Unit.define('dpm') do |dpm|
+ dpm.definition = Unit('1 count/min')
+end
+
+Unit.define('bpm') do |bpm|
+ bpm.definition = Unit('1 count/min')
+end
+
+# misc
+Unit.define!('dozen') do |dozen|
+ dozen.definition = Unit('12 each')
+ dozen.aliases = %w{doz dz dozen}
+ dozen.kind = :counting
+end
+
+Unit.define('gross') do |gross|
+ gross.definition = Unit('12 dozen')
+ gross.aliases = %w{gr gross}
+ gross.kind = :counting
+end
+
+Unit.define('cell') do |cell|
+ cell.definition = Unit('1 each')
+ cell.aliases = %w{cells cell}
+ cell.kind = :counting
+end
+
+Unit.define('base-pair') do |bp|
+ bp.definition = Unit('1 each')
+ bp.aliases = %w{bp base-pair}
+ bp.kind = :counting
+end
+
+Unit.define('nucleotide') do |nt|
+ nt.definition = Unit('1 each')
+ nt.aliases = %w{nt}
+ nt.kind = :counting
+end
+
+Unit.define('molecule') do |molecule|
+ molecule.definition = Unit('1 each')
+ molecule.aliases = %w{molecule molecules}
+ molecule.kind = :counting
+end
+
+Unit.define('percent') do |percent|
+ percent.definition = Unit('1/100')
+ percent.aliases = %w{% percent}
+end
+
+Unit.define('ppm') do |ppm|
+ ppm.definition = Unit(1) / 1_000_000
+end
+
+Unit.define('ppb') do |ppb|
+ ppb.definition = Unit(1) / 1_000_000_000
end

0 comments on commit ce2e3c7

Please sign in to comment.
Something went wrong with that request. Please try again.