diff --git a/Cargo.lock b/Cargo.lock index ba455f9..b9c79ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,7 +22,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "daisy" -version = "0.1.0" +version = "0.1.1" dependencies = [ "cfg-if", "rug", diff --git a/Cargo.toml b/Cargo.toml index 2d79e2a..4e9bbab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "daisy" -version = "0.1.0" +version = "0.1.1" edition = "2021" [profile.release] diff --git a/src/parser/find_subs.rs b/src/parser/find_subs.rs index 99efbea..88842ee 100644 --- a/src/parser/find_subs.rs +++ b/src/parser/find_subs.rs @@ -64,7 +64,7 @@ pub(in crate::parser) fn find_subs( //"upsilon" => {Some("υ")}, "phi" => {Some("φ")}, "chi" => {Some("χ")}, - "psi" => {Some("ψ")}, + //"psi" => {Some("ψ")}, Conflict with pound / square inch "omega" => {Some("ω")}, _ => {None} }; diff --git a/src/quantity/scalar/rationalbase.rs b/src/quantity/scalar/rationalbase.rs index 9c1181b..e220511 100644 --- a/src/quantity/scalar/rationalbase.rs +++ b/src/quantity/scalar/rationalbase.rs @@ -25,25 +25,20 @@ pub struct RationalBase where { pub val: Rational } -/* -fn to_string_radix(&self, radix: i32, num_digits: Option) -> String { - self.to_float().to_string_radix(radix, num_digits) -} - -fn to_sign_string_exp(&self, radix: i32, num_digits: Option) -> (bool, String, Option) { - self.to_float().to_sign_string_exp(radix, num_digits) -} -*/ - impl ToString for RationalBase{ fn to_string(&self) -> String { return self.val.to_string(); } } +impl RationalBase { + pub fn from_frac(t: i64, b: i64) -> Option { + let v = Rational::from((t, b)); + return Some(RationalBase{ val: v }); + } +} + impl ScalarBase for RationalBase { - - fn from_f64(f: f64) -> Option { let v = Rational::from_f64(f); if v.is_none() { return None } diff --git a/src/quantity/scalar/scalar.rs b/src/quantity/scalar/scalar.rs index 05d8672..a5fe0aa 100644 --- a/src/quantity/scalar/scalar.rs +++ b/src/quantity/scalar/scalar.rs @@ -116,6 +116,12 @@ impl Scalar { return Some(wrap_rational!(r.unwrap())); } + pub fn new_rational_from_frac(t: i64, b: i64) -> Option { + let r = RationalBase::from_frac(t, b); + if r.is_none() { return None; } + return Some(wrap_rational!(r.unwrap())); + } + pub fn new_float_from_string(s: &str) -> Option { let v = FloatBase::from_string(s); if v.is_none() { return None; } diff --git a/src/quantity/unit/freeunit.rs b/src/quantity/unit/freeunit.rs index 8567e86..63ac88c 100644 --- a/src/quantity/unit/freeunit.rs +++ b/src/quantity/unit/freeunit.rs @@ -60,6 +60,24 @@ macro_rules! unpack_base_factor { ]) }) }; + + ( + $unit:expr, + $display_string:expr, + rational_frac, + ($t:expr, $b:expr), + $( ($u:expr, $p:expr) ),* + ) => { + Some(Quantity { + scalar: Scalar::new_rational_from_frac($t, $b).unwrap(), + unit: Unit::from_array(&[ + $( + (FreeUnit::from_base($u), Scalar::new_rational($p).unwrap()), + )* + (FreeUnit::from_base($unit), Scalar::new_rational(-1f64).unwrap()) + ]) + }) + }; } diff --git a/src/quantity/unit/mod.rs b/src/quantity/unit/mod.rs index 8af610b..d1e3bd7 100644 --- a/src/quantity/unit/mod.rs +++ b/src/quantity/unit/mod.rs @@ -29,8 +29,10 @@ pub enum UnitBase { Point, // pt, typesetting unit Inch, Foot, - Mile, + Yard, Furlong, + Mile, + AstronomicalUnit, // Time units Minute, @@ -39,6 +41,19 @@ pub enum UnitBase { Week, Month, Fortnight, + GregorianYear, + JulianYear, + + // Pressure + Pascal, + Atmosphere, + Bar, + Barye, + Psi, + MillimeterMercury, + Torr, + MeterSeaWater, + FootSeaWater } @@ -64,46 +79,12 @@ macro_rules! fromstring_db { (UnitBase::Ampere, "amperes"), (UnitBase::Gram, "gram"), (UnitBase::Gram, "grams"), + (UnitBase::Gram, "gramme"), + (UnitBase::Gram, "grammes"), (UnitBase::Kelvin, "kelvin"), (UnitBase::Mole, "mole"), (UnitBase::Candela, "candela"), - // Length - (UnitBase::Angstrom, "angstrom"), - (UnitBase::Angstrom, "Å"), - (UnitBase::Thou, "thou"), - (UnitBase::Point, "pt"), - (UnitBase::Point, "point"), - (UnitBase::Inch, "in"), - (UnitBase::Inch, "inch"), - (UnitBase::Foot, "ft"), - (UnitBase::Foot, "foot"), - (UnitBase::Foot, "feet"), - (UnitBase::Mile, "mi"), - (UnitBase::Mile, "mile"), - (UnitBase::Mile, "miles"), - - // Time - (UnitBase::Second, "sec"), - (UnitBase::Second, "second"), - (UnitBase::Second, "seconds"), - (UnitBase::Minute, "min"), - (UnitBase::Minute, "minute"), - (UnitBase::Minute, "minutes"), - (UnitBase::Hour, "h"), - (UnitBase::Hour, "hour"), - (UnitBase::Hour, "hours"), - (UnitBase::Day, "d"), - (UnitBase::Day, "day"), - (UnitBase::Day, "days"), - (UnitBase::Week, "w"), - (UnitBase::Week, "week"), - (UnitBase::Week, "weeks"), - (UnitBase::Month, "month"), - (UnitBase::Month, "months"), - (UnitBase::Fortnight, "fortnight"), - (UnitBase::Fortnight, "fortnights"), - (UnitBase::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") ), @@ -130,6 +111,77 @@ macro_rules! fromstring_db { (UnitBase::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") + ), + + // Length + (UnitBase::Angstrom, "angstrom"), + (UnitBase::Angstrom, "Å"), + (UnitBase::Thou, "thou"), + (UnitBase::Point, "pt"), + (UnitBase::Point, "point"), + (UnitBase::Inch, "in"), + (UnitBase::Inch, "inch"), + (UnitBase::Foot, "ft"), + (UnitBase::Foot, "foot"), + (UnitBase::Foot, "feet"), + (UnitBase::Yard, "yard"), + (UnitBase::Yard, "yd"), + (UnitBase::Yard, "yards"), + (UnitBase::Mile, "mi"), + (UnitBase::Mile, "mile"), + (UnitBase::Mile, "miles"), + (UnitBase::AstronomicalUnit, "au"), + (UnitBase::AstronomicalUnit, "AU"), + (UnitBase::AstronomicalUnit, "astronomicalUnit"), + (UnitBase::AstronomicalUnit, "astronomicalUnits"), + + // Time + (UnitBase::Second, "sec"), + (UnitBase::Second, "second"), + (UnitBase::Second, "seconds"), + (UnitBase::Minute, "min"), + (UnitBase::Minute, "minute"), + (UnitBase::Minute, "minutes"), + (UnitBase::Hour, "h"), + (UnitBase::Hour, "hour"), + (UnitBase::Hour, "hours"), + (UnitBase::Day, "d"), + (UnitBase::Day, "day"), + (UnitBase::Day, "days"), + (UnitBase::Week, "w"), + (UnitBase::Week, "week"), + (UnitBase::Week, "weeks"), + (UnitBase::Month, "month"), + (UnitBase::Month, "months"), + (UnitBase::Fortnight, "fortnight"), + (UnitBase::Fortnight, "fortnights"), + (UnitBase::GregorianYear, "year"), + (UnitBase::GregorianYear, "years"), + (UnitBase::JulianYear, "julianYear"), + (UnitBase::JulianYear, "julianYears"), + + // Pressure + (UnitBase::Atmosphere, "atm"), + (UnitBase::Atmosphere, "atmosphere"), + (UnitBase::Atmosphere, "atmospheres"), + (UnitBase::Pascal, "pascal"), + (UnitBase::Barye, "Ba"), + (UnitBase::Psi, "psi"), + (UnitBase::MillimeterMercury, "mmhg"), + (UnitBase::MillimeterMercury, "mmHg"), + (UnitBase::Torr, "torr"), + (UnitBase::Torr, "Torr"), + (UnitBase::MeterSeaWater, "msw"), + (UnitBase::FootSeaWater, "fsw"), + (UnitBase::MeterSeaWater, "MSW"), + (UnitBase::FootSeaWater, "FSW"), + + (UnitBase::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") + ), + + (UnitBase::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") ) ) } @@ -146,6 +198,9 @@ macro_rules! unit_db { ($a:expr, $X:ident) => { match $a { + + // Base + UnitBase::Second => $X!( UnitBase::Second, // Repeat the name of this base unit @@ -182,6 +237,10 @@ macro_rules! unit_db { ), + + + // Time + UnitBase::Minute => $X!( UnitBase::Minute, "min", @@ -231,6 +290,22 @@ macro_rules! unit_db { (UnitBase::Second, 1f64) ), + UnitBase::GregorianYear => $X!( + UnitBase::GregorianYear, "year", + rational, "31557000", + (UnitBase::Second, 1f64) + ), + + UnitBase::JulianYear => $X!( + UnitBase::JulianYear, "julianYear", + rational, "31557600", + (UnitBase::Second, 1f64) + ), + + + + // Length + UnitBase::Angstrom => $X!( UnitBase::Angstrom, "Å", rational, "1e-10", @@ -239,39 +314,129 @@ macro_rules! unit_db { UnitBase::Thou => $X!( UnitBase::Thou, "thou", - float, "0.0000254", + rational, "0.0000254", (UnitBase::Meter, 1f64) ), UnitBase::Point => $X!( UnitBase::Point, "pt", - float, "0.000352778", + rational, "0.0003514598", (UnitBase::Meter, 1f64) ), UnitBase::Inch => $X!( UnitBase::Inch, "in", - float, "0.0254", + rational, "0.0254", (UnitBase::Meter, 1f64) ), UnitBase::Foot => $X!( UnitBase::Foot, "ft", - float, "0.3048", + rational, "0.3048", (UnitBase::Meter, 1f64) ), - UnitBase::Mile => $X!( - UnitBase::Mile, "mi", - float, "1609", + UnitBase::Yard => $X!( + UnitBase::Yard, "yd", + rational, "0.9144", (UnitBase::Meter, 1f64) ), UnitBase::Furlong => $X!( UnitBase::Furlong, "furlong", - float, "201.168", + rational, "201.17", (UnitBase::Meter, 1f64) ), + + UnitBase::Mile => $X!( + UnitBase::Mile, "mi", + rational, "1609.344", + (UnitBase::Meter, 1f64) + ), + + UnitBase::AstronomicalUnit => $X!( + UnitBase::AstronomicalUnit, "AU", + rational, "149597870700", + (UnitBase::Meter, 1f64) + ), + + + + + // Pressure + + UnitBase::Pascal => $X!( + UnitBase::Pascal, "Pa", + rational, "1000", + (UnitBase::Gram, 1f64), + (UnitBase::Meter, -1f64), + (UnitBase::Second, -2f64) + ), + + UnitBase::Bar => $X!( + UnitBase::Bar, "bar", + rational, "100000000", + (UnitBase::Gram, 1f64), + (UnitBase::Meter, -1f64), + (UnitBase::Second, -2f64) + ), + + UnitBase::Barye => $X!( + UnitBase::Barye, "Ba", + rational, "100", + (UnitBase::Gram, 1f64), + (UnitBase::Meter, -1f64), + (UnitBase::Second, -2f64) + ), + + UnitBase::Atmosphere => $X!( + UnitBase::Atmosphere, "atm", + rational, "101325000", + (UnitBase::Gram, 1f64), + (UnitBase::Meter, -1f64), + (UnitBase::Second, -2f64) + ), + + UnitBase::Psi => $X!( + UnitBase::Psi, "psi", + rational, "6894757.2931783", + (UnitBase::Gram, 1f64), + (UnitBase::Meter, -1f64), + (UnitBase::Second, -2f64) + ), + + UnitBase::MillimeterMercury => $X!( + UnitBase::MillimeterMercury, "mmHg", + rational, "133322.387415", + (UnitBase::Gram, 1f64), + (UnitBase::Meter, -1f64), + (UnitBase::Second, -2f64) + ), + + UnitBase::Torr => $X!( + UnitBase::Torr, "torr", + rational_frac, (101325000, 760), + (UnitBase::Gram, 1f64), + (UnitBase::Meter, -1f64), + (UnitBase::Second, -2f64) + ), + + UnitBase::MeterSeaWater => $X!( + UnitBase::MeterSeaWater, "MSW", + rational, "10000000", + (UnitBase::Gram, 1f64), + (UnitBase::Meter, -1f64), + (UnitBase::Second, -2f64) + ), + + UnitBase::FootSeaWater => $X!( + UnitBase::FootSeaWater, "FSW", + rational, "3064330", + (UnitBase::Gram, 1f64), + (UnitBase::Meter, -1f64), + (UnitBase::Second, -2f64) + ), + } diff --git a/units.md b/units.md index dd3712c..e94bde8 100644 --- a/units.md +++ b/units.md @@ -1,10 +1,5 @@ | Unit | Syntax | |------------------------------------------------------------------------------|-----------------------------------------------------------------------------| - | [Ampere](https://en.wikipedia.org/wiki/Ampere) | `amperes`, `ampere`, `A` | - | [Ångström](https://en.wikipedia.org/wiki/Ångström) | `angstrom`, `Å` | - | [Astronomical unit](https://en.wikipedia.org/wiki/Astronomical_unit) | `AU`, `au`, `astronomicalunits`, `astronomicalunit` | - | [Atmosphere](https://en.wikipedia.org/wiki/Atmosphere_(unit)) | `atm` | - | [Bar](https://en.wikipedia.org/wiki/Bar_(unit)) | `bar` | | [Barn](https://en.wikipedia.org/wiki/Barn_(unit)) | `barn` | | [Becquerel](https://en.wikipedia.org/wiki/Becquerel) | `becquerel`, `Bq` | | [Bel](https://en.wikipedia.org/wiki/Decibel) | `bel` | @@ -13,7 +8,6 @@ | [British thermal unit](https://en.wikipedia.org/wiki/British_thermal_unit) | `BTU` | | [Byte](https://en.wikipedia.org/wiki/Byte) | `Bytes`, `bytes`, `Byte`, `byte`, `B`, `Octets`, `octets`, `Octet`, `octet` | | [Calorie](https://en.wikipedia.org/wiki/Calorie) | `calories`, `calorie`, `cal` | - | [Candela](https://en.wikipedia.org/wiki/Candela) | `candela`, `cd` | | [Coulomb](https://en.wikipedia.org/wiki/Coulomb) | `coulomb`, `C` | | [Cup](https://en.wikipedia.org/wiki/Cup_(unit)) | `cups`, `cup` | | [DPI](https://en.wikipedia.org/wiki/Dots_per_inch) | `dpi` | @@ -24,21 +18,15 @@ | [Euro](https://en.wikipedia.org/wiki/Euro) | `euros`, `euro`, `EUR`, `€` | | [Farad](https://en.wikipedia.org/wiki/Farad) | `farad`, `F` | | [Fluid ounce](https://en.wikipedia.org/wiki/Fluid_ounce) | `fluidounces`, `fluidounce`, `floz` | - | [Foot](https://en.wikipedia.org/wiki/Foot_(unit)) | `feet`, `foot`, `ft` | - | [Fortnight](https://en.wikipedia.org/wiki/Fortnight) | `fortnights`, `fortnight` | | [Frame](https://en.wikipedia.org/wiki/Film_frame) | `frames`, `frame` | | [Frames per second](https://en.wikipedia.org/wiki/Frame_rate) | `fps` | - | [Furlong](https://en.wikipedia.org/wiki/Furlong) | `furlongs`, `furlong` | | [Gallon](https://en.wikipedia.org/wiki/Gallon) | `gallons`, `gallon`, `gal` | | [Gauss](https://en.wikipedia.org/wiki/Gauss_(unit)) | `gauss` | - | [Gram](https://en.wikipedia.org/wiki/Gram) | `grams`, `gram`, `grammes`, `gramme`, `g` | | [Gray](https://en.wikipedia.org/wiki/Gray_(unit)) | `gray`, `Gy` | | [Hectare](https://en.wikipedia.org/wiki/Hectare) | `hectare`, `ha` | | [Henry](https://en.wikipedia.org/wiki/Henry_(unit)) | `henrys`, `henries`, `henry`, `H` | | [Hertz](https://en.wikipedia.org/wiki/Hertz) | `hertz`, `Hz` | | [Hogshead](https://en.wikipedia.org/wiki/Hogshead) | `hogsheads`, `hogshead` | - | [Hour](https://en.wikipedia.org/wiki/Hour) | `hours`, `hour`, `h` | - | [Inch](https://en.wikipedia.org/wiki/Inch) | `inches`, `inch`, `in` | | [Joule](https://en.wikipedia.org/wiki/Joule) | `joules`, `joule`, `J` | | [Katal](https://en.wikipedia.org/wiki/Katal) | `katal`, `kat` | | [Kelvin](https://en.wikipedia.org/wiki/Kelvin) | `kelvin`, `K` | @@ -46,13 +34,9 @@ | [Liter](https://en.wikipedia.org/wiki/Liter) | `liters`, `liter`, `litres`, `litre`, `L`, `l` | | [Lumen](https://en.wikipedia.org/wiki/Lumen_(unit)) | `lumen`, `lm` | | [Lux](https://en.wikipedia.org/wiki/Lux) | `lux`, `lx` | - | [Meter](https://en.wikipedia.org/wiki/Meter) | `meters`, `meter`, `metres`, `metre`, `m` | - | [Mile](https://en.wikipedia.org/wiki/Mile) | `miles`, `mile` | | [Miles per hour](https://en.wikipedia.org/wiki/Miles_per_hour) | `mph` | | [Millimeter of mercury](https://en.wikipedia.org/wiki/Millimeter_of_mercury) | `mmHg` | - | [Minute](https://en.wikipedia.org/wiki/Minute) | `minutes`, `minute`, `min` | | [Mole](https://en.wikipedia.org/wiki/Mole_(unit)) | `mole`, `mol` | - | [Month](https://en.wikipedia.org/wiki/Month) | `months`, `month` | | [Newton](https://en.wikipedia.org/wiki/Newton_(unit)) | `newton`, `N` | | [Ohm](https://en.wikipedia.org/wiki/Ohm) | `ohms`, `ohm`, `Ω` | | [Ounce](https://en.wikipedia.org/wiki/Ounce) | `ounces`, `ounce`, `oz` | @@ -62,7 +46,6 @@ | [Parts-per-billion](https://en.wikipedia.org/wiki/Parts-per_notation) | `ppb` | | [Parts-per-trillion](https://en.wikipedia.org/wiki/Parts-per_notation) | `ppt` | | [Parts-per-quadrillion](https://en.wikipedia.org/wiki/Parts-per_notation) | `ppq` | - | [Pascal](https://en.wikipedia.org/wiki/Pascal_(unit)) | `pascal`, `Pa` | | [Percent](https://en.wikipedia.org/wiki/Parts-per_notation) | `percent`, `pct` | | [Person](https://en.wiktionary.org/wiki/person) | `persons`, `person`, `people` | | [Piece](https://en.wiktionary.org/wiki/piece) | `pieces`, `piece` | @@ -70,24 +53,17 @@ | [Pixel](https://en.wikipedia.org/wiki/Pixel) | `pixels`, `pixel`, `px` | | [Pound-force](https://en.wikipedia.org/wiki/Pound_%28force%29) | `pound_force`, `lbf` | | [Pound](https://en.wikipedia.org/wiki/Pound_(mass)) | `pounds`, `pound`, `lb` | - | [Psi](https://en.wikipedia.org/wiki/Pounds_per_square_inch) | `psi` | | [RPM](https://en.wikipedia.org/wiki/RPM) | `RPM`, `rpm` | | [Radian](https://en.wikipedia.org/wiki/Radian) | `radians`, `radian`, `rad` | | [Rod](https://en.wikipedia.org/wiki/Rod_(unit)) | `rods`, `rod` | - | [Second](https://en.wikipedia.org/wiki/Second) | `seconds`, `second`, `sec`, `s` | | [Siemens](https://en.wikipedia.org/wiki/Siemens_(unit)) | `siemens`, `S` | | [Sievert](https://en.wikipedia.org/wiki/Sievert) | `sievert`, `Sv` | | [Tablespoon](https://en.wikipedia.org/wiki/Tablespoon) | `tablespoons`, `tablespoon`, `tbsp` | | [Teaspoon](https://en.wikipedia.org/wiki/Teaspoon) | `teaspoons`, `teaspoon`, `tsp` | | [Tesla](https://en.wikipedia.org/wiki/Tesla_(unit)) | `tesla`, `T` | - | [Thou](https://en.wikipedia.org/wiki/Thousandth_of_an_inch) | `thou`, `mils`, `mil` | | [Tonne](https://en.wikipedia.org/wiki/Tonne) | `tonnes`, `tonne`, `tons`, `ton`, `t` | | [US Dollar](https://en.wikipedia.org/wiki/USD) | `dollars`, `dollar`, `USD`, `$` | | [Volt](https://en.wikipedia.org/wiki/Volt) | `volts`, `volt`, `V` | | [Watt-hour](https://en.wikipedia.org/wiki/Kilowatt_hour) | `Wh` | | [Watt](https://en.wikipedia.org/wiki/Watt) | `watts`, `watt`, `W` | - | [Weber](https://en.wikipedia.org/wiki/Weber_(unit)) | `weber`, `Wb` | - | [Week](https://en.wikipedia.org/wiki/Week) | `weeks`, `week` | - | [Yard](https://en.wikipedia.org/wiki/Yard) | `yards`, `yard`, `yd` | - | [Gregorian year](https://en.wikipedia.org/wiki/Gregorian_year) | `years`, `year` | - | [Julian year](https://en.wikipedia.org/wiki/Julian_year_(astronomy)) | `julianYears`, `julianYear` | + | [Weber](https://en.wikipedia.org/wiki/Weber_(unit)) | `weber`, `Wb` | \ No newline at end of file