units icon indicating copy to clipboard operation
units copied to clipboard

Support for additional dimensions

Open dhouck opened this issue 6 years ago • 4 comments

I don't know if this is supported but poorly documented, or not supported; if it's the former it would be nice to have a documented example.

Is it possible to add a dimension other than the ones included in the library already (length, time, information, etc.)? For example, if I want to model economic activity as well, is there a way to let me type:

auto cost_of_gold = 41.63_dollars / 1_g;

(In the real world, of course, money isn't a particularly good fit because exchange rates change all the time; it just works as a good example here.)

dhouck avatar May 21 '18 23:05 dhouck

User-defined dimensions are supported in v3.0.0, which isn't currently stable. There's work ongoing to improve the documentation regarding that (see #124). Here's an almost minimal, currently working example:

namespace dimension {

    struct Display_length_tag {
        static constexpr const char* name{"display length"};
        static constexpr const char* abbreviation{"px"};
    };

    using Display_length = units::make_dimension<Display_length_tag>;

} // namespace dimension

namespace display_length {

    // Represents a quantity of pixels.
    template <class Arithmetic>
    using Pixels = units::unit<
        units::unit_conversion<std::ratio<1>, dimension::Display_length>,
        Arithmetic>;

} // namespace display_length

It defines the dimension display length and its base unit pixels.

JohelEGP avatar May 22 '18 23:05 JohelEGP

@dhouck like @johelegp it's not supported in v2.3.1 but will be in v3. There will be some macro wrapper around the example above that will simplify defining dimensions, and examples in the documentation.

Any feedback on the current documentation would also be highly appreciated!

nholthaus avatar May 25 '18 21:05 nholthaus

an aside: the problem with monetary units for this library has always been that (most) conversions can't happen at compile-time because you'd have to tie in to some exchange-rate database, so the type system and dimensional analysis can't really buy you anything (unless you accidently compare dollars to feet?). I don't plan to ever support them.

nholthaus avatar May 25 '18 21:05 nholthaus

Thanks, that should help.

Yeah, money isn't necessarily a good fit (at least in the real world; it could work for simplified programs or fictional economies), but it's an example of a dimension that's not otherwise accounted for. I'd have used information, but you already support that one; maybe I should have used something from this list instead. My actual use case is harder to summarize quickly.

dhouck avatar May 26 '18 04:05 dhouck