diff --git a/src/units.h b/src/units.h index b55548be1642a..adeed5bc33e61 100644 --- a/src/units.h +++ b/src/units.h @@ -334,41 +334,54 @@ const mass mass_max = units::mass( std::numeric_limits: units::mass::unit_type{} ); template -inline constexpr quantity from_milligram( +inline constexpr quantity from_microgram( const value_type v ) { - return quantity( v, mass_in_milligram_tag{} ); + return quantity( v, mass_in_microgram_tag{} ); } template -inline constexpr quantity from_gram( +inline constexpr quantity from_milligram( + const value_type v ) +{ + return from_microgram( v * 1000 ); +} + +template +inline constexpr quantity from_gram( const value_type v ) { return from_milligram( v * 1000 ); } template -inline constexpr quantity from_kilogram( +inline constexpr quantity from_kilogram( const value_type v ) { return from_gram( v * 1000 ); } template -inline constexpr value_type to_milligram( const quantity &v ) +inline constexpr value_type to_microgram( const quantity &v ) { return v.value(); } template -inline constexpr value_type to_gram( const quantity &v ) +inline constexpr value_type to_milligram( const quantity &v ) { return v.value() / 1000.0; } +template +inline constexpr value_type to_gram( const quantity &v ) +{ + return v.value() / 1'000'000.0; +} + inline constexpr double to_kilogram( const mass &v ) { - return v.value() / 1000000.0; + return v.value() / 1'000'000'000.0; } // Specific energy @@ -783,9 +796,9 @@ inline constexpr quantity default_length_f // Streaming operators for debugging and tests // (for UI output other functions should be used which render in the user's // chosen units) -inline std::ostream &operator<<( std::ostream &o, mass_in_milligram_tag ) +inline std::ostream &operator<<( std::ostream &o, mass_in_microgram_tag ) { - return o << "mg"; + return o << "μg"; } inline std::ostream &operator<<( std::ostream &o, volume_in_milliliter_tag ) @@ -867,7 +880,10 @@ inline constexpr units::quantity operat return units::from_milliliter( v * 1000 ); } -// Implicitly converted to mass, which has int as value_type! +inline constexpr units::mass operator"" _microgram( const unsigned long long v ) +{ + return units::from_microgram( v ); +} inline constexpr units::mass operator"" _milligram( const unsigned long long v ) { return units::from_milligram( v ); @@ -882,19 +898,24 @@ inline constexpr units::mass operator"" _kilogram( const unsigned long long v ) return units::from_kilogram( v ); } -inline constexpr units::quantity operator"" _milligram( +inline constexpr units::quantity operator"" _microgram( + const long double v ) +{ + return units::from_microgram( v ); +} +inline constexpr units::quantity operator"" _milligram( const long double v ) { return units::from_milligram( v ); } -inline constexpr units::quantity operator"" _gram( +inline constexpr units::quantity operator"" _gram( const long double v ) { return units::from_gram( v ); } -inline constexpr units::quantity operator"" _kilogram( +inline constexpr units::quantity operator"" _kilogram( const long double v ) { return units::from_kilogram( v ); @@ -1147,6 +1168,8 @@ const std::vector> power_units = { { } }; const std::vector> mass_units = { { + { "ug", 1_microgram }, + { "μg", 1_microgram }, { "mg", 1_milligram }, { "g", 1_gram }, { "kg", 1_kilogram }, diff --git a/src/units_fwd.h b/src/units_fwd.h index 8a018c3b826e0..e695386fc450e 100644 --- a/src/units_fwd.h +++ b/src/units_fwd.h @@ -16,11 +16,11 @@ class volume_in_milliliter_tag using volume = quantity; -class mass_in_milligram_tag +class mass_in_microgram_tag { }; -using mass = quantity; +using mass = quantity; class specific_energy_in_joule_per_gram_tag {