Fixed variable names

pull/2/head
Mark 2023-06-11 16:52:23 -07:00
parent e59cfac837
commit 28c1a84b80
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
4 changed files with 297 additions and 297 deletions

View File

@ -1,5 +1,4 @@
- Prettyprint mi/h, mi/g - Prettyprint mi/h, mi/g
- Base unit/baseunit naming
- Minimize parenthesis - Minimize parenthesis
- Sane autoconversion (mi + km) - Sane autoconversion (mi + km)
@ -59,6 +58,7 @@
- Numbered expressions, history recall - Numbered expressions, history recall
- Better power printing - Better power printing
- Color configuration? - Color configuration?
- Show base units on error
## Bonus ## Bonus
- Plural unit names - Plural unit names

View File

@ -2,7 +2,7 @@ use std::hash::Hash;
use crate::quantity::Scalar; use crate::quantity::Scalar;
use crate::quantity::Quantity; use crate::quantity::Quantity;
use super::UnitBase; use super::WholeUnit;
use super::Prefix; use super::Prefix;
use super::Unit; use super::Unit;
use super::unit_db; use super::unit_db;
@ -13,7 +13,7 @@ use super::unit_db;
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
pub struct FreeUnit { pub struct FreeUnit {
pub (in super) base: UnitBase, pub (in super) whole: WholeUnit,
pub (in super) prefix: Prefix pub (in super) prefix: Prefix
} }
@ -28,7 +28,7 @@ macro_rules! unpack_string {
impl ToString for FreeUnit { impl ToString for FreeUnit {
fn to_string(&self) -> String { fn to_string(&self) -> String {
let s = unit_db!(self.base, unpack_string); let s = unit_db!(self.whole, unpack_string);
let p = self.prefix.to_string(); let p = self.prefix.to_string();
format!("{p}{s}") format!("{p}{s}")
@ -55,9 +55,9 @@ macro_rules! unpack_base_factor {
scalar: Scalar::new_float_from_string($value).unwrap(), scalar: Scalar::new_float_from_string($value).unwrap(),
unit: Unit::from_array(&[ unit: Unit::from_array(&[
$( $(
(FreeUnit::from_base($u), Scalar::new_rational($p).unwrap()), (FreeUnit::from_whole($u), Scalar::new_rational($p).unwrap()),
)* )*
(FreeUnit::from_base($unit), Scalar::new_rational(-1f64).unwrap()) (FreeUnit::from_whole($unit), Scalar::new_rational(-1f64).unwrap())
]) ])
}) })
}; };
@ -73,9 +73,9 @@ macro_rules! unpack_base_factor {
scalar: Scalar::new_rational_from_string($value).unwrap(), scalar: Scalar::new_rational_from_string($value).unwrap(),
unit: Unit::from_array(&[ unit: Unit::from_array(&[
$( $(
(FreeUnit::from_base($u), Scalar::new_rational($p).unwrap()), (FreeUnit::from_whole($u), Scalar::new_rational($p).unwrap()),
)* )*
(FreeUnit::from_base($unit), Scalar::new_rational(-1f64).unwrap()) (FreeUnit::from_whole($unit), Scalar::new_rational(-1f64).unwrap())
]) ])
}) })
}; };
@ -91,9 +91,9 @@ macro_rules! unpack_base_factor {
scalar: Scalar::new_rational_from_frac($t, $b).unwrap(), scalar: Scalar::new_rational_from_frac($t, $b).unwrap(),
unit: Unit::from_array(&[ unit: Unit::from_array(&[
$( $(
(FreeUnit::from_base($u), Scalar::new_rational($p).unwrap()), (FreeUnit::from_whole($u), Scalar::new_rational($p).unwrap()),
)* )*
(FreeUnit::from_base($unit), Scalar::new_rational(-1f64).unwrap()) (FreeUnit::from_whole($unit), Scalar::new_rational(-1f64).unwrap())
]) ])
}) })
}; };
@ -101,11 +101,11 @@ macro_rules! unpack_base_factor {
impl FreeUnit { impl FreeUnit {
pub fn from_base(base: UnitBase) -> FreeUnit { pub fn from_whole(whole: WholeUnit) -> FreeUnit {
return FreeUnit { base, prefix: Prefix::None } return FreeUnit { whole, prefix: Prefix::None }
} }
pub fn from_base_prefix(base: UnitBase, prefix: Prefix) -> FreeUnit { FreeUnit {base, prefix} } pub fn from_whole_prefix(whole: WholeUnit, prefix: Prefix) -> FreeUnit { FreeUnit {whole, prefix} }
pub fn set_prefix(&mut self, prefix: Prefix) { self.prefix = prefix; } pub fn set_prefix(&mut self, prefix: Prefix) { self.prefix = prefix; }
pub fn get_prefix(&self) -> Prefix { self.prefix } pub fn get_prefix(&self) -> Prefix { self.prefix }
@ -113,12 +113,12 @@ impl FreeUnit {
/// gives a quantity in base units. /// gives a quantity in base units.
pub fn to_base_factor(&self) -> Quantity { pub fn to_base_factor(&self) -> Quantity {
let q = unit_db!(self.base, unpack_base_factor); let q = unit_db!(self.whole, unpack_base_factor);
let mut q = q.unwrap_or(Quantity::new_rational_from_string("1").unwrap()); let mut q = q.unwrap_or(Quantity::new_rational_from_string("1").unwrap());
let mut p = self.prefix.to_ratio(); let mut p = self.prefix.to_ratio();
p.insert_unit(FreeUnit::from_base(self.base), Scalar::new_rational(1f64).unwrap()); p.insert_unit(FreeUnit::from_whole(self.whole), Scalar::new_rational(1f64).unwrap());
p.insert_unit(FreeUnit::from_base_prefix(self.base, self.prefix), Scalar::new_rational(-1f64).unwrap()); p.insert_unit(FreeUnit::from_whole_prefix(self.whole, self.prefix), Scalar::new_rational(-1f64).unwrap());
q.mul_assign_no_convert(p); q.mul_assign_no_convert(p);
return q; return q;
@ -126,11 +126,11 @@ impl FreeUnit {
// Get this unit in terms of base units // Get this unit in terms of base units
pub fn get_base(&self) -> Quantity { pub fn get_base(&self) -> Quantity {
let q = unit_db!(self.base, unpack_base_factor); let q = unit_db!(self.whole, unpack_base_factor);
let mut q = q.unwrap_or(Quantity::new_rational_from_string("1").unwrap()); let mut q = q.unwrap_or(Quantity::new_rational_from_string("1").unwrap());
// Don't divide by self // Don't divide by self
q.insert_unit(FreeUnit::from_base(self.base), Scalar::new_rational(1f64).unwrap()); q.insert_unit(FreeUnit::from_whole_prefix(self.whole, self.prefix), Scalar::new_rational(1f64).unwrap());
return q; return q;
} }

View File

@ -13,7 +13,7 @@ pub use freeunit::FreeUnit;
#[derive(Debug)] #[derive(Debug)]
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
#[derive(Eq, PartialEq)] #[derive(Eq, PartialEq)]
pub enum UnitBase { pub enum WholeUnit {
// Base Units // Base Units
Second, Second,
Meter, Meter,
@ -94,174 +94,174 @@ macro_rules! fromstring_db {
($X:ident) => { ($X:ident) => {
$X!( $X!(
// Base units // Base units
(UnitBase::Meter, "meter"), (WholeUnit::Meter, "meter"),
(UnitBase::Meter, "meters"), (WholeUnit::Meter, "meters"),
(UnitBase::Ampere, "ampere"), (WholeUnit::Ampere, "ampere"),
(UnitBase::Ampere, "amperes"), (WholeUnit::Ampere, "amperes"),
(UnitBase::Gram, "gram"), (WholeUnit::Gram, "gram"),
(UnitBase::Gram, "grams"), (WholeUnit::Gram, "grams"),
(UnitBase::Gram, "gramme"), (WholeUnit::Gram, "gramme"),
(UnitBase::Gram, "grammes"), (WholeUnit::Gram, "grammes"),
(UnitBase::Kelvin, "kelvin"), (WholeUnit::Kelvin, "kelvin"),
(UnitBase::Mole, "mole"), (WholeUnit::Mole, "mole"),
(UnitBase::Candela, "candela"), (WholeUnit::Candela, "candela"),
(UnitBase::Meter, "m", (WholeUnit::Meter, "m",
("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q") ("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q")
), ),
(UnitBase::Second, "s", (WholeUnit::Second, "s",
("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q") ("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q")
), ),
(UnitBase::Gram, "g", (WholeUnit::Gram, "g",
("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q") ("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q")
), ),
(UnitBase::Ampere, "A", (WholeUnit::Ampere, "A",
("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q") ("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q")
), ),
(UnitBase::Kelvin, "K", (WholeUnit::Kelvin, "K",
("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q") ("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q")
), ),
(UnitBase::Mole, "mol", (WholeUnit::Mole, "mol",
("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q") ("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q")
), ),
(UnitBase::Candela, "cd", (WholeUnit::Candela, "cd",
("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q") ("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q")
), ),
// Length // Length
(UnitBase::Angstrom, "angstrom"), (WholeUnit::Angstrom, "angstrom"),
(UnitBase::Angstrom, "Å"), (WholeUnit::Angstrom, "Å"),
(UnitBase::Thou, "thou"), (WholeUnit::Thou, "thou"),
(UnitBase::Point, "pt"), (WholeUnit::Point, "pt"),
(UnitBase::Point, "point"), (WholeUnit::Point, "point"),
(UnitBase::Inch, "in"), (WholeUnit::Inch, "in"),
(UnitBase::Inch, "inch"), (WholeUnit::Inch, "inch"),
(UnitBase::Foot, "ft"), (WholeUnit::Foot, "ft"),
(UnitBase::Foot, "foot"), (WholeUnit::Foot, "foot"),
(UnitBase::Foot, "feet"), (WholeUnit::Foot, "feet"),
(UnitBase::Yard, "yard"), (WholeUnit::Yard, "yard"),
(UnitBase::Yard, "yd"), (WholeUnit::Yard, "yd"),
(UnitBase::Yard, "yards"), (WholeUnit::Yard, "yards"),
(UnitBase::Mile, "mi"), (WholeUnit::Mile, "mi"),
(UnitBase::Mile, "mile"), (WholeUnit::Mile, "mile"),
(UnitBase::Mile, "miles"), (WholeUnit::Mile, "miles"),
(UnitBase::AstronomicalUnit, "au"), (WholeUnit::AstronomicalUnit, "au"),
(UnitBase::AstronomicalUnit, "AU"), (WholeUnit::AstronomicalUnit, "AU"),
(UnitBase::AstronomicalUnit, "astronomicalUnit"), (WholeUnit::AstronomicalUnit, "astronomicalUnit"),
(UnitBase::AstronomicalUnit, "astronomicalUnits"), (WholeUnit::AstronomicalUnit, "astronomicalUnits"),
(UnitBase::Lightyear, "ly"), (WholeUnit::Lightyear, "ly"),
(UnitBase::Lightyear, "lightyear"), (WholeUnit::Lightyear, "lightyear"),
(UnitBase::Lightyear, "lightyears"), (WholeUnit::Lightyear, "lightyears"),
(UnitBase::Parsec, "pc"), (WholeUnit::Parsec, "pc"),
(UnitBase::Parsec, "parsec"), (WholeUnit::Parsec, "parsec"),
(UnitBase::Parsec, "parsecs"), (WholeUnit::Parsec, "parsecs"),
// Time // Time
(UnitBase::Second, "sec"), (WholeUnit::Second, "sec"),
(UnitBase::Second, "second"), (WholeUnit::Second, "second"),
(UnitBase::Second, "seconds"), (WholeUnit::Second, "seconds"),
(UnitBase::Minute, "min"), (WholeUnit::Minute, "min"),
(UnitBase::Minute, "minute"), (WholeUnit::Minute, "minute"),
(UnitBase::Minute, "minutes"), (WholeUnit::Minute, "minutes"),
(UnitBase::Hour, "h"), (WholeUnit::Hour, "h"),
(UnitBase::Hour, "hour"), (WholeUnit::Hour, "hour"),
(UnitBase::Hour, "hours"), (WholeUnit::Hour, "hours"),
(UnitBase::Day, "d"), (WholeUnit::Day, "d"),
(UnitBase::Day, "day"), (WholeUnit::Day, "day"),
(UnitBase::Day, "days"), (WholeUnit::Day, "days"),
(UnitBase::Week, "w"), (WholeUnit::Week, "w"),
(UnitBase::Week, "week"), (WholeUnit::Week, "week"),
(UnitBase::Week, "weeks"), (WholeUnit::Week, "weeks"),
(UnitBase::Month, "month"), (WholeUnit::Month, "month"),
(UnitBase::Month, "months"), (WholeUnit::Month, "months"),
(UnitBase::Fortnight, "fortnight"), (WholeUnit::Fortnight, "fortnight"),
(UnitBase::Fortnight, "fortnights"), (WholeUnit::Fortnight, "fortnights"),
(UnitBase::GregorianYear, "year"), (WholeUnit::GregorianYear, "year"),
(UnitBase::GregorianYear, "years"), (WholeUnit::GregorianYear, "years"),
(UnitBase::JulianYear, "julianYear"), (WholeUnit::JulianYear, "julianYear"),
(UnitBase::JulianYear, "julianYears"), (WholeUnit::JulianYear, "julianYears"),
// Misc // Misc
(UnitBase::Barn, "b"), (WholeUnit::Barn, "b"),
(UnitBase::Barn, "barn"), (WholeUnit::Barn, "barn"),
(UnitBase::Hectare, "ha"), (WholeUnit::Hectare, "ha"),
(UnitBase::Hectare, "hectare"), (WholeUnit::Hectare, "hectare"),
(UnitBase::Hectare, "hectares"), (WholeUnit::Hectare, "hectares"),
(UnitBase::Acre, "acre"), (WholeUnit::Acre, "acre"),
(UnitBase::Acre, "acres"), (WholeUnit::Acre, "acres"),
// Volume // Volume
(UnitBase::Liter, "liter"), (WholeUnit::Liter, "liter"),
(UnitBase::Liter, "liters"), (WholeUnit::Liter, "liters"),
(UnitBase::Liter, "litre"), (WholeUnit::Liter, "litre"),
(UnitBase::Liter, "litres"), (WholeUnit::Liter, "litres"),
(UnitBase::USGallon, "usgal"), (WholeUnit::USGallon, "usgal"),
(UnitBase::USGallon, "gal"), (WholeUnit::USGallon, "gal"),
(UnitBase::USGallon, "gallon"), (WholeUnit::USGallon, "gallon"),
(UnitBase::USGallon, "gallons"), (WholeUnit::USGallon, "gallons"),
(UnitBase::Quart, "quart"), (WholeUnit::Quart, "quart"),
(UnitBase::Quart, "quarts"), (WholeUnit::Quart, "quarts"),
(UnitBase::Quart, "qt"), (WholeUnit::Quart, "qt"),
(UnitBase::ImperialGallon, "impgal"), (WholeUnit::ImperialGallon, "impgal"),
(UnitBase::ImperialGallon, "imperialGallon"), (WholeUnit::ImperialGallon, "imperialGallon"),
(UnitBase::ImperialGallon, "imperialGallons"), (WholeUnit::ImperialGallon, "imperialGallons"),
(UnitBase::Cup, "cup"), (WholeUnit::Cup, "cup"),
(UnitBase::Floz, "floz"), (WholeUnit::Floz, "floz"),
(UnitBase::Pint, "pint"), (WholeUnit::Pint, "pint"),
(UnitBase::Pint, "pints"), (WholeUnit::Pint, "pints"),
(UnitBase::Tablespoon, "tablespoon"), (WholeUnit::Tablespoon, "tablespoon"),
(UnitBase::Tablespoon, "tablespoons"), (WholeUnit::Tablespoon, "tablespoons"),
(UnitBase::Tablespoon, "tbsp"), (WholeUnit::Tablespoon, "tbsp"),
(UnitBase::Tablespoon, "Tbsp"), (WholeUnit::Tablespoon, "Tbsp"),
(UnitBase::Teaspoon, "teaspoon"), (WholeUnit::Teaspoon, "teaspoon"),
(UnitBase::Teaspoon, "teaspoons"), (WholeUnit::Teaspoon, "teaspoons"),
(UnitBase::Teaspoon, "tsp"), (WholeUnit::Teaspoon, "tsp"),
(UnitBase::Teaspoon, "Tsp"), (WholeUnit::Teaspoon, "Tsp"),
(UnitBase::Hogshead, "hogshead"), (WholeUnit::Hogshead, "hogshead"),
(UnitBase::Hogshead, "hogsheads"), (WholeUnit::Hogshead, "hogsheads"),
(UnitBase::Liter, "l", (WholeUnit::Liter, "l",
("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q") ("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q")
), ),
(UnitBase::Liter, "L", (WholeUnit::Liter, "L",
("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q") ("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q")
), ),
// Pressure // Pressure
(UnitBase::Atmosphere, "atm"), (WholeUnit::Atmosphere, "atm"),
(UnitBase::Atmosphere, "atmosphere"), (WholeUnit::Atmosphere, "atmosphere"),
(UnitBase::Atmosphere, "atmospheres"), (WholeUnit::Atmosphere, "atmospheres"),
(UnitBase::Pascal, "pascal"), (WholeUnit::Pascal, "pascal"),
(UnitBase::Barye, "Ba"), (WholeUnit::Barye, "Ba"),
(UnitBase::Psi, "psi"), (WholeUnit::Psi, "psi"),
(UnitBase::MillimeterMercury, "mmhg"), (WholeUnit::MillimeterMercury, "mmhg"),
(UnitBase::MillimeterMercury, "mmHg"), (WholeUnit::MillimeterMercury, "mmHg"),
(UnitBase::Torr, "torr"), (WholeUnit::Torr, "torr"),
(UnitBase::Torr, "Torr"), (WholeUnit::Torr, "Torr"),
(UnitBase::MeterSeaWater, "msw"), (WholeUnit::MeterSeaWater, "msw"),
(UnitBase::FootSeaWater, "fsw"), (WholeUnit::FootSeaWater, "fsw"),
(UnitBase::MeterSeaWater, "MSW"), (WholeUnit::MeterSeaWater, "MSW"),
(UnitBase::FootSeaWater, "FSW"), (WholeUnit::FootSeaWater, "FSW"),
(UnitBase::Pascal, "Pa", (WholeUnit::Pascal, "Pa",
("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q") ("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q")
), ),
(UnitBase::Bar, "bar", (WholeUnit::Bar, "bar",
("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q") ("Q","R","Y","Z","E","P","T","G","M","k","h","da","d","c","m","u","n","p","f","a","z","y","r","q")
) )
) )
@ -282,9 +282,9 @@ macro_rules! unit_db {
// Base // Base
UnitBase::Second => $X!( WholeUnit::Second => $X!(
UnitBase::Second, // Repeat the name of this base unit WholeUnit::Second, // Repeat the name of this base unit
"s", // String to display for this unit "s", // String to display for this unit
// "base", "float", or "rational." // "base", "float", or "rational."
@ -292,28 +292,28 @@ macro_rules! unit_db {
// if float or rational, this is not a base unit. See below. // if float or rational, this is not a base unit. See below.
base base
), ),
UnitBase::Meter => $X!( WholeUnit::Meter => $X!(
UnitBase::Meter, "m", WholeUnit::Meter, "m",
base base
), ),
UnitBase::Gram => $X!( WholeUnit::Gram => $X!(
UnitBase::Gram, "g", WholeUnit::Gram, "g",
base base
), ),
UnitBase::Ampere => $X!( WholeUnit::Ampere => $X!(
UnitBase::Ampere, "A", WholeUnit::Ampere, "A",
base base
), ),
UnitBase::Kelvin => $X!( WholeUnit::Kelvin => $X!(
UnitBase::Kelvin, "K", WholeUnit::Kelvin, "K",
base base
), ),
UnitBase::Mole => $X!( WholeUnit::Mole => $X!(
UnitBase::Mole, "mol", WholeUnit::Mole, "mol",
base base
), ),
UnitBase::Candela => $X!( WholeUnit::Candela => $X!(
UnitBase::Candela, "cd", WholeUnit::Candela, "cd",
base base
), ),
@ -322,8 +322,8 @@ macro_rules! unit_db {
// Time // Time
UnitBase::Minute => $X!( WholeUnit::Minute => $X!(
UnitBase::Minute, "min", WholeUnit::Minute, "min",
// "rational" and "float" determine what kind of Quantity // "rational" and "float" determine what kind of Quantity
// this unit's conversion factor will be. Use "rational" // this unit's conversion factor will be. Use "rational"
@ -338,276 +338,276 @@ macro_rules! unit_db {
"60", "60",
// The unit. Can be repeated for compound units. // The unit. Can be repeated for compound units.
// MUST BE BASE UNITS. // MUST BE BASE UNITS.
(UnitBase::Second, 1f64) (WholeUnit::Second, 1f64)
), ),
UnitBase::Hour => $X!( WholeUnit::Hour => $X!(
UnitBase::Hour, "h", WholeUnit::Hour, "h",
rational, "3600", rational, "3600",
(UnitBase::Second, 1f64) (WholeUnit::Second, 1f64)
), ),
UnitBase::Day => $X!( WholeUnit::Day => $X!(
UnitBase::Day, "day", WholeUnit::Day, "day",
rational, "86400", rational, "86400",
(UnitBase::Second, 1f64) (WholeUnit::Second, 1f64)
), ),
UnitBase::Week => $X!( WholeUnit::Week => $X!(
UnitBase::Week, "week", WholeUnit::Week, "week",
rational, "604800", rational, "604800",
(UnitBase::Second, 1f64) (WholeUnit::Second, 1f64)
), ),
UnitBase::Month => $X!( WholeUnit::Month => $X!(
UnitBase::Month, "month", WholeUnit::Month, "month",
rational, "2629746", rational, "2629746",
(UnitBase::Second, 1f64) (WholeUnit::Second, 1f64)
), ),
UnitBase::Fortnight => $X!( WholeUnit::Fortnight => $X!(
UnitBase::Fortnight, "fortnight", WholeUnit::Fortnight, "fortnight",
rational, "1209600", rational, "1209600",
(UnitBase::Second, 1f64) (WholeUnit::Second, 1f64)
), ),
UnitBase::GregorianYear => $X!( WholeUnit::GregorianYear => $X!(
UnitBase::GregorianYear, "year", WholeUnit::GregorianYear, "year",
rational, "31557000", rational, "31557000",
(UnitBase::Second, 1f64) (WholeUnit::Second, 1f64)
), ),
UnitBase::JulianYear => $X!( WholeUnit::JulianYear => $X!(
UnitBase::JulianYear, "julianYear", WholeUnit::JulianYear, "julianYear",
rational, "31557600", rational, "31557600",
(UnitBase::Second, 1f64) (WholeUnit::Second, 1f64)
), ),
// Length // Length
UnitBase::Angstrom => $X!( WholeUnit::Angstrom => $X!(
UnitBase::Angstrom, "Å", WholeUnit::Angstrom, "Å",
rational, "1e-10", rational, "1e-10",
(UnitBase::Meter, 1f64) (WholeUnit::Meter, 1f64)
), ),
UnitBase::Thou => $X!( WholeUnit::Thou => $X!(
UnitBase::Thou, "thou", WholeUnit::Thou, "thou",
rational, "0.0000254", rational, "0.0000254",
(UnitBase::Meter, 1f64) (WholeUnit::Meter, 1f64)
), ),
UnitBase::Point => $X!( WholeUnit::Point => $X!(
UnitBase::Point, "pt", WholeUnit::Point, "pt",
rational, "0.0003514598", rational, "0.0003514598",
(UnitBase::Meter, 1f64) (WholeUnit::Meter, 1f64)
), ),
UnitBase::Inch => $X!( WholeUnit::Inch => $X!(
UnitBase::Inch, "in", WholeUnit::Inch, "in",
rational, "0.0254", rational, "0.0254",
(UnitBase::Meter, 1f64) (WholeUnit::Meter, 1f64)
), ),
UnitBase::Foot => $X!( WholeUnit::Foot => $X!(
UnitBase::Foot, "ft", WholeUnit::Foot, "ft",
rational, "0.3048", rational, "0.3048",
(UnitBase::Meter, 1f64) (WholeUnit::Meter, 1f64)
), ),
UnitBase::Yard => $X!( WholeUnit::Yard => $X!(
UnitBase::Yard, "yd", WholeUnit::Yard, "yd",
rational, "0.9144", rational, "0.9144",
(UnitBase::Meter, 1f64) (WholeUnit::Meter, 1f64)
), ),
UnitBase::Furlong => $X!( WholeUnit::Furlong => $X!(
UnitBase::Furlong, "furlong", WholeUnit::Furlong, "furlong",
rational, "201.17", rational, "201.17",
(UnitBase::Meter, 1f64) (WholeUnit::Meter, 1f64)
), ),
UnitBase::Mile => $X!( WholeUnit::Mile => $X!(
UnitBase::Mile, "mi", WholeUnit::Mile, "mi",
rational, "1609.344", rational, "1609.344",
(UnitBase::Meter, 1f64) (WholeUnit::Meter, 1f64)
), ),
UnitBase::AstronomicalUnit => $X!( WholeUnit::AstronomicalUnit => $X!(
UnitBase::AstronomicalUnit, "AU", WholeUnit::AstronomicalUnit, "AU",
rational, "149597870700", rational, "149597870700",
(UnitBase::Meter, 1f64) (WholeUnit::Meter, 1f64)
), ),
UnitBase::Lightyear => $X!( WholeUnit::Lightyear => $X!(
UnitBase::Lightyear, "ly", WholeUnit::Lightyear, "ly",
rational, "9460730472580800", rational, "9460730472580800",
(UnitBase::Meter, 1f64) (WholeUnit::Meter, 1f64)
), ),
UnitBase::Parsec => $X!( WholeUnit::Parsec => $X!(
UnitBase::Parsec, "pc", WholeUnit::Parsec, "pc",
float, "3.085677581e16", float, "3.085677581e16",
(UnitBase::Meter, 1f64) (WholeUnit::Meter, 1f64)
), ),
UnitBase::Barn => $X!( WholeUnit::Barn => $X!(
UnitBase::Barn, "b", WholeUnit::Barn, "b",
rational, "1e-28", rational, "1e-28",
(UnitBase::Meter, 2f64) (WholeUnit::Meter, 2f64)
), ),
UnitBase::Hectare => $X!( WholeUnit::Hectare => $X!(
UnitBase::Hectare, "ha", WholeUnit::Hectare, "ha",
rational, "10000", rational, "10000",
(UnitBase::Meter, 2f64) (WholeUnit::Meter, 2f64)
), ),
UnitBase::Acre => $X!( // 66 x 660 feet WholeUnit::Acre => $X!( // 66 x 660 feet
UnitBase::Acre, "acre", WholeUnit::Acre, "acre",
rational, "4046.8564224", rational, "4046.8564224",
(UnitBase::Meter, 2f64) (WholeUnit::Meter, 2f64)
), ),
// Volume // Volume
UnitBase::Liter => $X!( WholeUnit::Liter => $X!(
UnitBase::Liter, "l", WholeUnit::Liter, "l",
rational, "0.001", rational, "0.001",
(UnitBase::Meter, 3f64) (WholeUnit::Meter, 3f64)
), ),
UnitBase::Hogshead => $X!( WholeUnit::Hogshead => $X!(
UnitBase::Hogshead, "hogshead", WholeUnit::Hogshead, "hogshead",
rational, "0.2385", // 63 gallons rational, "0.2385", // 63 gallons
(UnitBase::Meter, 3f64) (WholeUnit::Meter, 3f64)
), ),
UnitBase::USGallon => $X!( WholeUnit::USGallon => $X!(
UnitBase::USGallon, "gal", WholeUnit::USGallon, "gal",
rational, "0.003785411784", rational, "0.003785411784",
(UnitBase::Meter, 3f64) (WholeUnit::Meter, 3f64)
), ),
UnitBase::Quart => $X!( WholeUnit::Quart => $X!(
UnitBase::Quart, "qt", WholeUnit::Quart, "qt",
rational, "0.000946352946", rational, "0.000946352946",
(UnitBase::Meter, 3f64) (WholeUnit::Meter, 3f64)
), ),
UnitBase::ImperialGallon => $X!( WholeUnit::ImperialGallon => $X!(
UnitBase::ImperialGallon, "impgal", WholeUnit::ImperialGallon, "impgal",
rational, "0.00454609", rational, "0.00454609",
(UnitBase::Meter, 3f64) (WholeUnit::Meter, 3f64)
), ),
UnitBase::Cup => $X!( WholeUnit::Cup => $X!(
UnitBase::Cup, "cup", WholeUnit::Cup, "cup",
rational, "0.0002365882365", rational, "0.0002365882365",
(UnitBase::Meter, 3f64) (WholeUnit::Meter, 3f64)
), ),
UnitBase::Floz => $X!( WholeUnit::Floz => $X!(
UnitBase::Floz, "floz", WholeUnit::Floz, "floz",
rational, "0.0000295735295625", rational, "0.0000295735295625",
(UnitBase::Meter, 3f64) (WholeUnit::Meter, 3f64)
), ),
UnitBase::Pint => $X!( WholeUnit::Pint => $X!(
UnitBase::Pint, "pint", WholeUnit::Pint, "pint",
rational, "0.00056826125", rational, "0.00056826125",
(UnitBase::Meter, 3f64) (WholeUnit::Meter, 3f64)
), ),
UnitBase::Tablespoon => $X!( WholeUnit::Tablespoon => $X!(
UnitBase::Tablespoon, "tbsp", WholeUnit::Tablespoon, "tbsp",
rational, "0.00001478676478125", rational, "0.00001478676478125",
(UnitBase::Meter, 3f64) (WholeUnit::Meter, 3f64)
), ),
UnitBase::Teaspoon => $X!( WholeUnit::Teaspoon => $X!(
UnitBase::Teaspoon, "tsp", WholeUnit::Teaspoon, "tsp",
rational, "0.000005", rational, "0.000005",
(UnitBase::Meter, 3f64) (WholeUnit::Meter, 3f64)
), ),
// Pressure // Pressure
UnitBase::Pascal => $X!( WholeUnit::Pascal => $X!(
UnitBase::Pascal, "Pa", WholeUnit::Pascal, "Pa",
rational, "1000", rational, "1000",
(UnitBase::Gram, 1f64), (WholeUnit::Gram, 1f64),
(UnitBase::Meter, -1f64), (WholeUnit::Meter, -1f64),
(UnitBase::Second, -2f64) (WholeUnit::Second, -2f64)
), ),
UnitBase::Bar => $X!( WholeUnit::Bar => $X!(
UnitBase::Bar, "bar", WholeUnit::Bar, "bar",
rational, "100000000", rational, "100000000",
(UnitBase::Gram, 1f64), (WholeUnit::Gram, 1f64),
(UnitBase::Meter, -1f64), (WholeUnit::Meter, -1f64),
(UnitBase::Second, -2f64) (WholeUnit::Second, -2f64)
), ),
UnitBase::Barye => $X!( WholeUnit::Barye => $X!(
UnitBase::Barye, "Ba", WholeUnit::Barye, "Ba",
rational, "100", rational, "100",
(UnitBase::Gram, 1f64), (WholeUnit::Gram, 1f64),
(UnitBase::Meter, -1f64), (WholeUnit::Meter, -1f64),
(UnitBase::Second, -2f64) (WholeUnit::Second, -2f64)
), ),
UnitBase::Atmosphere => $X!( WholeUnit::Atmosphere => $X!(
UnitBase::Atmosphere, "atm", WholeUnit::Atmosphere, "atm",
rational, "101325000", rational, "101325000",
(UnitBase::Gram, 1f64), (WholeUnit::Gram, 1f64),
(UnitBase::Meter, -1f64), (WholeUnit::Meter, -1f64),
(UnitBase::Second, -2f64) (WholeUnit::Second, -2f64)
), ),
UnitBase::Psi => $X!( WholeUnit::Psi => $X!(
UnitBase::Psi, "psi", WholeUnit::Psi, "psi",
rational, "6894757.2931783", rational, "6894757.2931783",
(UnitBase::Gram, 1f64), (WholeUnit::Gram, 1f64),
(UnitBase::Meter, -1f64), (WholeUnit::Meter, -1f64),
(UnitBase::Second, -2f64) (WholeUnit::Second, -2f64)
), ),
UnitBase::MillimeterMercury => $X!( WholeUnit::MillimeterMercury => $X!(
UnitBase::MillimeterMercury, "mmHg", WholeUnit::MillimeterMercury, "mmHg",
rational, "133322.387415", rational, "133322.387415",
(UnitBase::Gram, 1f64), (WholeUnit::Gram, 1f64),
(UnitBase::Meter, -1f64), (WholeUnit::Meter, -1f64),
(UnitBase::Second, -2f64) (WholeUnit::Second, -2f64)
), ),
UnitBase::Torr => $X!( WholeUnit::Torr => $X!(
UnitBase::Torr, "torr", WholeUnit::Torr, "torr",
rational_frac, (101325000, 760), rational_frac, (101325000, 760),
(UnitBase::Gram, 1f64), (WholeUnit::Gram, 1f64),
(UnitBase::Meter, -1f64), (WholeUnit::Meter, -1f64),
(UnitBase::Second, -2f64) (WholeUnit::Second, -2f64)
), ),
UnitBase::MeterSeaWater => $X!( WholeUnit::MeterSeaWater => $X!(
UnitBase::MeterSeaWater, "MSW", WholeUnit::MeterSeaWater, "MSW",
rational, "10000000", rational, "10000000",
(UnitBase::Gram, 1f64), (WholeUnit::Gram, 1f64),
(UnitBase::Meter, -1f64), (WholeUnit::Meter, -1f64),
(UnitBase::Second, -2f64) (WholeUnit::Second, -2f64)
), ),
UnitBase::FootSeaWater => $X!( WholeUnit::FootSeaWater => $X!(
UnitBase::FootSeaWater, "FSW", WholeUnit::FootSeaWater, "FSW",
rational, "3064330", rational, "3064330",
(UnitBase::Gram, 1f64), (WholeUnit::Gram, 1f64),
(UnitBase::Meter, -1f64), (WholeUnit::Meter, -1f64),
(UnitBase::Second, -2f64) (WholeUnit::Second, -2f64)
), ),
} }

View File

@ -7,7 +7,7 @@ use std::ops::{
use crate::quantity::Scalar; use crate::quantity::Scalar;
use crate::quantity::Quantity; use crate::quantity::Quantity;
use super::FreeUnit; use super::FreeUnit;
use super::UnitBase; use super::WholeUnit;
use super::Prefix; use super::Prefix;
use super::fromstring_db; use super::fromstring_db;
use super::str_to_prefix; use super::str_to_prefix;
@ -203,14 +203,14 @@ impl Unit {
match s { match s {
$( $(
// No prefix--every unit has this // No prefix--every unit has this
$string => Some(FreeUnit::from_base($unit)), $string => Some(FreeUnit::from_whole($unit)),
// Arms for prefixes // Arms for prefixes
$($( $($(
concat!( concat!(
$prefix, $prefix,
$string $string
) => Some(FreeUnit::from_base_prefix($unit, str_to_prefix!($prefix))), ) => Some(FreeUnit::from_whole_prefix($unit, str_to_prefix!($prefix))),
)*)* )*)*
)* )*
_ => None _ => None