-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better precision and abbreviation change for astronomical_unit
#475
Comments
I am trying to make the workaround solution in the meantime by basically combining two of the example files - creating a new unit and using it to define a different set of base units. I currently have the following draft test code: #[macro_use]
extern crate uom;
use uom::fmt::DisplayStyle::*;
use uom::si::f64::*;
use uom::si::length::meter;
unit! {
system: uom::si;
quantity: uom::si::length;
@au: 1.495_978_707_E11; "au", "astronomical unit", "astronomical units";
}
mod solar_system_units {
use crate::au;
ISQ!(
uom::si,
f64,
(au, kilogram, day, ampere, kelvin, mole, candela)
);
}
fn main() {
let l1 = Length::new::<meter>(1000.0);
let l2 = Length::new::<au>(1.0);
println!(
"{} = {}",
l1.into_format_args(meter, Abbreviation),
l1.into_format_args(au, Abbreviation)
);
println!(
"{} = {}",
l2.into_format_args(au, Abbreviation),
l2.into_format_args(meter, Abbreviation)
);
} which produces a rather confusing compiler error, telling me to import
I am unfortunately not too familiar with macros yet, so I am not sure whether what I am trying to do is even remotely supposed to work. |
astronomical_unit
astronomical_unit
If all your doing is extending an existing quantity with units, then you don't need to create a new set of quantities. Deleting the For ease of use you could create a custom mod si {
pub use uom::si::*;
pub mod length {
pub use uom::si::length::*;
unit! {
system: uom::si;
quantity: uom::si::length;
@au: 1.495_978_707_E11; "au", "astronomical unit", "astronomical units";
}
}
}
|
My impression was that I had to create a new set of quantities to adjust the units in which each quantity is stored under the hood. My goal is to store distances as AU, mass as zettagrams (did not give that in the example, but have added it since), and time as days, in order to maintain best precision with large values. ISQ!(uom::si, f64, (au, zettagram, day, ampere, kelvin, mole, candela) While also redefining the AU as in the example. To achieve all of this, I believe I have to create the new set of quantities, right? |
Try this: #[macro_use]
extern crate uom;
use uom::fmt::DisplayStyle::*;
mod si {
pub use uom::si::*;
pub mod length {
pub use uom::si::length::*;
unit! {
system: uom::si;
quantity: uom::si::length;
@au: 1.495_978_707_E11; "au", "astronomical unit", "astronomical units";
}
}
}
mod custom_base {
ISQ!(super::si, f64, (au, zettagram, day, ampere, kelvin, mole, candela));
}
fn main() {
let l1 = custom_base::Length::new::<si::length::meter>(1000.0);
let l2 = custom_base::Length::new::<si::length::au>(1.0);
println!(
"{} = {}",
l1.into_format_args(si::length::meter, Abbreviation),
l1.into_format_args(si::length::au, Abbreviation)
);
println!(
"{} = {}",
l2.into_format_args(si::length::au, Abbreviation),
l2.into_format_args(si::length::meter, Abbreviation)
);
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn base_unit_of_length_should_be_au() {
let l = custom_base::Length::new::<si::length::au>(1.0);
assert_eq!(l.value, 1.0);
}
#[test]
fn base_unit_of_time_should_be_day() {
let t_base = custom_base::Time::new::<si::time::day>(1.0);
let t_year = custom_base::Time::new::<si::time::year>(1.0);
assert_eq!(t_base.value, 1.0);
assert_eq!(t_year.value, 365.0);
}
} |
Just for more context, you don't need to create a new set of qunatities, i.e like the mks example, but create an alias to the If you want to pack all this into one module, then you can do the following: // Custom system of units
mod csi {
ISQ!(si, f64, (au, zettagram, day, ampere, kelvin, mole, candela));
mod si {
pub use uom::si::*;
pub mod length {
pub use uom::si::length::*;
unit! {
system: uom::si;
quantity: uom::si::length;
@au: 1.495_978_707_E11; "au", "astronomical unit", "astronomical units";
}
}
}
pub use si::*;
}
fn main() {
let l1 = csi::Length::new::<csi::length::meter>(1000.0);
let l2 = csi::Length::new::<csi::length::au>(1.0);
println!(
"{} = {}",
l1.into_format_args(csi::length::meter, Abbreviation),
l1.into_format_args(csi::length::au, Abbreviation)
);
println!(
"{} = {}",
l2.into_format_args(csi::length::au, Abbreviation),
l2.into_format_args(csi::length::meter, Abbreviation)
);
}
|
Okay, thanks, the workaround works like that! Still hope for the AU abbreviation and the exact value to be adjusted, though. |
Hi, I am planning to use
uom
for my astronomy project and I have been looking into its use to make sure it's gonna work well for me. Since I am aiming for the best calculation precision possible, I am planning to define a custom a custom set ofQuantity
type aliases that would use AU as the underlying base unit for distance (and perhaps also change some other base units) - since that would make the distances closer to 1 compared to what they would be if I used (kilo)meters, resulting in better floating point accuracy in the calculations.However, I noticed some issues with the current definition of the
astronomical_unit
inuom
:My proposal would therefore be to change the relevant line to this:
I would be happy to make a PR with the tweak if you give me a green light. Thanks!
The text was updated successfully, but these errors were encountered: