From octave-sources-request at bevo dot che dot wisc dot edu Mon Jan 27 14:10:30 2003 Subject: mass2kg.m From: Stefan Burger To: octave-sources at bevo dot che dot wisc dot edu Date: Mon, 27 Jan 2003 07:44:27 -0600 --------------Boundary-00=_KVKDQPUACR7NFTZICFUK Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable --------------Boundary-00=_KVKDQPUACR7NFTZICFUK Content-Type: text/plain; charset="us-ascii"; name="mass2kg.m" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="mass2kg.m" ## Copyright (C) 2003 Stefan Burger ## ## This file is part of Octave. ## ## Octave is free software; you can redistribute it and/or modify it ## under the terms of the GNU General Public License as published by the ## Free Software Foundation; either version 2, or (at your option) any ## later version. ## ## Octave is distributed in the hope that it will be useful, but WITHOUT ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ## for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, write to the Free ## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. ## -*- texinfo -*- ## at deftypefn {Function File} {} mass2kg (@var{unitstr}, @var{val}, @var{= SIstr}) ## Converts non-SI or US length measures to SI (metric) measures, ## such as kg. ## Input: ## at var{unitstr}=09: unit to be converted from. ## at var{val} =09; Value to be converted. ## at var{SIstr} =09: unit to be converted to, optional, defaults to 'kg', = kilogram. ## ## Units supported: ##=09=09lb, pound, ##=09=09metric carat, met-ct, carat, ct ##=09=09grain, gr ##=09=09oz, ounce ##=09=09ozt, Troy-ounce, Troy-oz ##=09=09Troy-pound, Troy-lb ##=09=09pennyweight, dwt ##=09=09long-hundredweight, UK-hundredweight (Rem.: 112lb) ##=09=09short-hundredweight, US=ADhundredweight (Rem.: 100lb) ##=09=09short-ton, US-ton (Rem.: 2000lb) ##=09=09short-quarter, quarter ##=09=09dram, dr ##=09=09UK-ton, long-ton (Rem.: 2240lb) ##=09=09Assay-ton, AT ##=09=09slug,=20 ##=09=09clove, ##=09=09stone ##=09=09tod ## ## SI (or: metric) Units supported:=20 ##=09=09kg, kilogram ##=09=09t, metric-ton, tonne ##=09=09g, gram ##=09=09mg, milligram ##=09=09ug, microgram ##=09=09ng, nanogram ##=09=09pg, picogram ## ## References: ## See NIST Guide to SI units, Appendix B8, ## at www.nist.gov/Pubs/SP811/appenB8.html ## ## at end deftypefn ## at seealso{metric2len, square2metric, vol2metric} ## Author: Stefan Burger ## Description: converts non-SI weight or mass measures to SI unit measur= es, such as kg. function v =3D mass2kg (unitstr, val, SIstr) if (nargin =3D=3D1) val =3D 1.0; SIstr =3D "kg"; %% if no value given, default to one. %% if no SI weight unit string given, default to kg. %% reason: usage val * mass2kg("lb") endif if (nargin =3D=3D 2) SIstr =3D "m"; %% if not specified, default to meters elseif (nargin < 1 || nargin > 3 ) usage("mass2kg(unitstr [, val, SIstr])"); endif if (! (isstr(unitstr) && isstr(SIstr))) usage("mass2kg(unitstr [, val, SIstr])"); endif %% %% definitions %% all relative to kg. %% Avoirdupois weights pound =3D 4.5359237E-1; switch (unitstr) case {"lb"; "pound"}; fv =3D pound; case {"metric carat", "met-ct", "carat", "ct"} fv =3D 2E-4; %% 5 carats equal 1 gram. %% also called "metric carat" - is there another one ?? case {"grain"; "gr"} fv =3D pound / 7000; %% 7000 grains equals 1 pound. case {"oz", "ounce"} fv =3D pound / 16; case {"ozt"; "Troy-ounce", "Troy-oz"} fv =3D mass2kg("Troy-lb") / 12; case {"Troy-pound", "Troy-lb"} fv =3D 5760 * grain; case {"pennyweight"; "dwt"}=09%% shorthand for pennyweight fv =3D mass2kg("Troy-oz") / 20; %% pennyweight is 1/20 Troy ounce. case {"long-hundredweight", "UK-hundredweight"} fv =3D 112 * pound; %% UK hundredweight equals 8 stone. case {"short-hundredweight", "US=ADhundredweight"} fv =3D 100 * pound; case {"ton"} error ("mass2kg: As 'ton' is too ambiguous in worldwide usage, plea= se use short-ton (2000lb), long-ton (2240lb), UK-ton, or metric-ton / ton= ne instead."); case {"short-ton", "US-ton"} fv =3D 2000 * pound; case {"short-quarter", "quarter"} fv =3D mass2kg("short-ton") / 4; case {"dram", "dr"} fv =3D mass2kg("oz") / 16; case {"UK-ton", "long-ton"}=09%% UK unit. fv =3D 2240 * pound; %% one UK ton equals 20 UK hundredweights. case {"Assay-ton", "AT"} fv =3D 2.916667E-2; case {"slug"} fv =3D 1.459390E1; case {"clove"}=09%% UK unit. fv =3D 7 * pound; case {"stone"}=09%% UK unit. fv =3D 14 * pound; %% one UK stone equals 2 cloves, or 14 pound. case {"tod"}=09%% UK unit. fv =3D 28 * pound; %% one UK tod equals 2 stones.. case {"kg", "kilogram"} fm =3D 1; case {"t", "metric-ton"; "tonne"} fm =3D 1E3; case {"g", "gram"} fm =3D 1E-3; case {"mg", "milligram"} fm =3D 1E-6; case {"ug", "microgram"} fm =3D 1E-9; case {"ng", "nanogram"} fm =3D 1E-12; case {"pg", "picogram"} fm =3D 1E-15; otherwise error("mass2kg: unrecognized option"); endswitch %% determine the conversion factor from meters to desired %% metric unit. switch (SIstr) case {"kg", "kilogram"} fm =3D 1; case {"t"; "metric-ton"; "tonne"} fm =3D 1E-3; case {"g", "gram"} fm =3D 1E3; case {"mg", "milligram"} fm =3D 1E6; case {"ug", "microgram"} fm =3D 1E9; case {"ng", "nanogram"} fm =3D 1E12; case {"pg", "picogram"} fm =3D 1E15; otherwise error("mass2kg: unrecognized option"); endswitch %% The most simple part: Putting the result together. v =3D val * fv * fm; endfunction --------------Boundary-00=_KVKDQPUACR7NFTZICFUK--