ICAROUS
All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Modules Pages
Static Public Member Functions | Static Public Attributes | Static Private Member Functions | List of all members
larcfm::Units Class Reference

#include <Units.h>

Static Public Member Functions

static double to (const double symbol, const double value)
 
static double to (const std::string &symbol, double value)
 
static double from (const double symbol, const double value)
 
static double from (const std::string &units, double value)
 
static double fromInternal (const std::string &defaultUnits, const std::string &units, double value)
 
static double getFactor (const std::string &unit)
 
static bool isUnit (const std::string &unit)
 
static bool isCompatible (const std::string &unit1, const std::string &unit2)
 
static std::string str (const std::string &symbol, double value)
 
static std::string strX (const std::string &symbol, double value)
 
static std::string str (const std::string &symbol, double value, int precision)
 
static std::string clean (const std::string &unit)
 
static double parse (const std::string &defaultUnitsFrom, const std::string &s, double default_val)
 
static double parse (const std::string &s)
 
static double parse (const std::string &str, double default_value)
 
static double parse (const std::string &, const std::string &str)
 
static std::string parseUnits (const std::string &s)
 
static const std::string canonical (const std::string &unit)
 

Static Public Attributes

static const double unspecified = Units::getFactor("unspecified")
 
static const double unitless = Units::getFactor("unitless")
 
static const double internal = Units::getFactor("internal")
 
static const double m = Units::getFactor("m")
 
static const double km = Units::getFactor("km")
 
static const double nmi = Units::getFactor("NM")
 
static const double NM = Units::getFactor("NM")
 
static const double foot
 
static const double ft = Units::getFactor("ft")
 
static const double mm = Units::getFactor("mm")
 
static const double meter2
 
static const double s = Units::getFactor("s")
 
static const double sec
 
static const double min = Units::getFactor("min")
 
static const double hour = Units::getFactor("hour")
 
static const double day = Units::getFactor("day")
 
static const double ms = Units::getFactor("ms")
 
static const double rad = Units::getFactor("rad")
 
static const double deg = Units::getFactor("deg")
 
static const double degree
 
static const double kg = Units::getFactor("kg")
 
static const double pound_mass = Units::getFactor("pound_mass")
 
static const double mps = Units::getFactor("mps")
 
static const double meter_per_second = Units::getFactor("meter_per_second")
 
static const double kph = Units::getFactor("kph")
 
static const double kilometer_per_hour = Units::getFactor("kilometer_per_hour")
 
static const double knot = Units::getFactor("knot")
 
static const double kn = Units::getFactor("kn")
 
static const double kts = Units::getFactor("kts")
 
static const double fpm = Units::getFactor("fpm")
 
static const double foot_per_second = Units::getFactor("foot_per_second")
 
static const double nautical_mile_per_second = Units::getFactor("nautical_mile_per_second")
 
static const double mph = Units::getFactor("mph")
 
static const double degreeC = Units::getFactor("degreeC")
 
static const double degreeF = Units::getFactor("degreeF")
 
static const double K = Units::getFactor("K")
 
static const double degreeK = Units::getFactor("K")
 
static const double degreeR = Units::getFactor("degreeR")
 
static const double gn = _FormalATM_gn()
 
static const double G = Units::getFactor("G")
 
static const double meter_per_second2 = Units::getFactor("meter_per_second2")
 
static const double newton = Units::getFactor("newton")
 
static const double pascal = Units::getFactor("pascal")
 
static const double Pa
 
static const double P0 = _FormalATM_P0()
 
static const double atm = Units::getFactor("atm")
 

Static Private Member Functions

static std::string cleanOnly (const std::string &unit)
 

Detailed Description

This class defines the conversion factors for various units. Base units are defined for the seven base dimensions: length, mass, time, electric current, thermodynamic temperature, amount of substance, and luminous intensity. Together these seven units form a coherent set of units. If all quantities in a program are defined in terms of these seven coherent units, then no unit conversions are required within the program. Unit conversions are only needed when specifying and outputting quantities. This class is designed to facilitate that type of use.

For example, note there are no unit conversions in the equations:

double one_mile = Units.from("mi", 1.0);
double one_km = Units.from("km", 1.0);
double time = Units.from("hour", 1.0);

double distance = one_mile + one_km;
double speed = distance / time;

System.out.println("The distance should be 2.6 in kilometers and is " + Units.to("km", distance)); 
System.out.println("The distance should be 1.6 in miles and is " + Units.to("mi", distance)); 
System.out.println("The distance should be 8560 in feet and is " + Units.to("ft", distance)); 
System.out.println("The speed should be 1.6 in miles/hour and is " + Units.to("mph", speed)); 
System.out.println("The speed should be 0.72 in m/s and is " + Units.to("m/s", speed)); 
double one_mile = Units.from("mile", 1.0);
System.out.println("One mile in liters is " + Units.to("liter", one_mile));

Obviously this makes no sense, but the Units class will not flag this as an error; be careful.

One can determine if two units are compatible by using the isCompatible method. Units are considered compatible if they have the same dimensionality. For instance, the unit "meter" is compatible with the unit "foot" since they are both units of distance.

if (!isCompatible("m", "L")) {
    System.out.println("'m' and 'L' are not compatible units!");
}
if (isCompatible("ft", "m")) {
    System.out.println("'ft' and 'm' are compatible units!");
}

The user can also create their own composite units with the addUnit(String, double, String) method. For example a fathom unit could be defined as:

Units.addUnit("fathom", 6.0, "foot");

This method means, define the unit called "fathom", where one fathom is equal to 6.0 feet. This method also states that the new "fathom" unit is compatible with the unit of "foot."

To create a new composite unit that has no compatible units, use the addUnit(String, double) method. For example a linear density unit could be defined as:

Units.addUnit("kg/m", Units.kg / Units.meter);

Special Units:

Usage Notes:

Special Constants:

Implementation Notes:

References:

Member Function Documentation

◆ canonical()

const std::string Units::canonical ( const std::string &  unit)
static

Return the canonical name for this unit. For instance, "meter" will return "m" and "m" will return "m".

◆ clean()

std::string Units::clean ( const std::string &  unit)
static

Clean up the unit string that may contain brackets or extra space. For instance, " [ feet]" becomes "feet". If the string in brackets is not a recognized, then "unspecified" is returned.

Parameters
unita "dirty" string
Returns
a cleaned-up unit string

◆ cleanOnly()

std::string Units::cleanOnly ( const std::string &  unit)
staticprivate

Clean up the unit string that may contain brackets or extra space. For instance, " [ feet]" becomes "feet". However, if the unit is "[ fred]" then "fred" will be returned (unlike the clean() method).

◆ from() [1/2]

double Units::from ( const double  symbol,
const double  value 
)
static

Convert the value from the given units into internal units

◆ from() [2/2]

double Units::from ( const std::string &  units,
double  value 
)
static

Convert the value from the given units into internal units

◆ getFactor()

double Units::getFactor ( const std::string &  unit)
static

Get the unit conversion factor for the given string unit

◆ isCompatible()

bool Units::isCompatible ( const std::string &  unit1,
const std::string &  unit2 
)
static

Determines if these two units are compatible with one another. That is, if they have the same dimensionality (for instance, both are units of distance) then true is returned. Note, the unit "unspecified" is compatible with all units, including "unitless" which is not compatible with any other units.

◆ isUnit()

bool Units::isUnit ( const std::string &  unit)
static

Determine if the given string is a valid unit

Parameters
unitstring representation of a unit
Returns
true, if this string is known as a unit in the database of units

◆ parse()

static double larcfm::Units::parse ( const std::string &  str,
double  default_value 
)
static

Parse a string, including an optional units identifier, as a double value. If the string does not contain a (valid) unit, then the value is interpreted as an "unspecified" unit. This version does not parse numbers in exponential notation, e.g., "10e-4". (Java does)

Parameters
strstring to parse
default_valueif the string is not recognized as a valid value, the result to be returned
Returns
value

◆ parseUnits()

std::string Units::parseUnits ( const std::string &  s)
static

Parse a string, representing a value and a unit. If the string does not contain a unit, then the unit "unspecified" is returned. This version does not parse numbers in exponential notation, e.g., "10e-4". (Java does)

Parameters
sstring to parse
Returns
unit string;

◆ str() [1/2]

std::string Units::str ( const std::string &  symbol,
double  value 
)
static

Make a string representation of the value in internal units to the given units

◆ str() [2/2]

std::string Units::str ( const std::string &  symbol,
double  value,
int  precision 
)
static

Make a string representation of the value in internal units to the given units

◆ strX()

std::string Units::strX ( const std::string &  symbol,
double  value 
)
static

Make a string representation of the value in internal units to the given units.

◆ to() [1/2]

double Units::to ( const double  symbol,
const double  value 
)
static

Convert the value in internal units to the given units

◆ to() [2/2]

double Units::to ( const std::string &  symbol,
double  value 
)
static

Convert the value in internal units to the given units

Member Data Documentation

◆ atm

const double Units::atm = Units::getFactor("atm")
static

atmosphere, a unit of pressure, defined as one P0

◆ day

const double Units::day = Units::getFactor("day")
static

days

◆ deg

const double Units::deg = Units::getFactor("deg")
static

degrees

◆ degree

const double larcfm::Units::degree
static

degrees

◆ foot

const double larcfm::Units::foot
static

foot

◆ foot_per_second

const double Units::foot_per_second = Units::getFactor("foot_per_second")
static

feet per second

◆ fpm

const double Units::fpm = Units::getFactor("fpm")
static

feet per minute

◆ ft

const double Units::ft = Units::getFactor("ft")
static

foot

◆ gn

const double Units::gn = _FormalATM_gn()
static

gn is the adopted physical constant of gravity. It is given (out of place) here, due to the fact that the definition of the fundamental US customary units of mass (slug and pound-mass) depend on this quantity.

This quantity, Units.gn, is defined in IEEE/ASTM SI 10-1997 as (exactly) 9.80665 m/s^2 [page 25]. The 1962 U.S. Standard Atmosphere [page 4] calls this quantity "standard sea-level gravity" and describes it as the acceleration due to the combined effects of gravity and the Earth's rotation (i.e. the centrifugal relief) at geodetic latitude 45 degrees. This constant was adopted long before 1962.

This definition is also in NIST Special Publication 330, the International System of Units (SI), 1991 edition, p17.

◆ hour

const double Units::hour = Units::getFactor("hour")
static

hours

◆ internal

const double Units::internal = Units::getFactor("internal")
static

Use the internal representation for this quantity

◆ kg

const double Units::kg = Units::getFactor("kg")
static

kilograms

◆ km

const double Units::km = Units::getFactor("km")
static

kilometer

◆ knot

const double Units::knot = Units::getFactor("knot")
static

knots, (nautical miles per hour)

◆ kph

const double Units::kph = Units::getFactor("kph")
static

kilometers per hour

◆ m

const double Units::m = Units::getFactor("m")
static

meter

◆ meter2

const double larcfm::Units::meter2
static

meter squared, a unit of area

◆ meter_per_second2

const double Units::meter_per_second2 = Units::getFactor("meter_per_second2")
static

meters per second^2

◆ min

const double Units::min = Units::getFactor("min")
static

minutes

◆ mm

const double Units::mm = Units::getFactor("mm")
static

millimeter

◆ mph

const double Units::mph = Units::getFactor("mph")
static

mile per hour

◆ mps

const double Units::mps = Units::getFactor("mps")
static

Meters per second

◆ ms

const double Units::ms = Units::getFactor("ms")
static

milliseconds

◆ nautical_mile_per_second

const double Units::nautical_mile_per_second = Units::getFactor("nautical_mile_per_second")
static

nautical mile per second

◆ newton

const double Units::newton = Units::getFactor("newton")
static

unit of force

◆ NM

const double Units::NM = Units::getFactor("NM")
static

nautical mile

◆ nmi

const double Units::nmi = Units::getFactor("NM")
static

nautical mile

◆ P0

const double Units::P0 = _FormalATM_P0()
static

P0, the adopted standard atmosphere. This quanity equals 101325 Pa. This definition comes from NIST Special Publication 330, the International System of Units (SI), 1991 edition, p15.

◆ Pa

const double larcfm::Units::Pa
static

pascal, unit of pressure, defined as a newton per meter squared

◆ pascal

const double Units::pascal = Units::getFactor("pascal")
static

pascal, a unit of pressure

◆ pound_mass

const double Units::pound_mass = Units::getFactor("pound_mass")
static

pounds of mass

◆ rad

const double Units::rad = Units::getFactor("rad")
static

radians

◆ s

const double Units::s = Units::getFactor("s")
static

seconds

◆ sec

const double larcfm::Units::sec
static

seconds

◆ unitless

const double Units::unitless = Units::getFactor("unitless")
static

Quantity without units

◆ unspecified

const double Units::unspecified = Units::getFactor("unspecified")
static

Units were not specified


The documentation for this class was generated from the following files: