nholthaus / units

a compile-time, header-only, dimensional analysis and unit conversion library built on c++14 with no dependencies.
http://nholthaus.github.io/units/
MIT License
947 stars 135 forks source link

Equivalent units #234

Closed gitsbi closed 3 years ago

gitsbi commented 4 years ago

I am currently checking out this unit library for inclusion in our next project. One problem I came across is that it seems impossible to create two units that are equivalent in their properties/dimensions, yet must not be mixed. For example, in electrical engineering there is, besides power (measured in W), also reactive power (var) and apparent power (voltampere). All are the result of multiplying voltage with current, but it is important that they are different units that the compiler sees as incompatible.

An in-house library I used to use supported that by their units having a unique_tag template parameter (which defaulted to void), and units with different tags being incompatible.

Am I overlooking something (well possible) or does this (otherwise great!) library not currently offer such a feature?

nholthaus commented 4 years ago

Better category support is something we're working for version three. The original SI/ dimension based system doesn't work for the case your describing, and the new system will allow for fully customized user defined dimensions. The tag idea isn't bad at all though.

You may want to check out the v3.x branch where we implemented the new dimension system (not yet really documented, but it's simple enough), like the alpha-2 release. https://github.com/nholthaus/units/releases/tag/v3.0.0.alpha-2.

Unfortunately we need some of the language changes to c++20 affecting template argument deduction to fully realize our vision for 3.0, and since it's going to contain some non-backwards compatible changes we're pretty set on waiting that out before any official release.

On Thu, Sep 12, 2019, 12:36 PM sbi notifications@github.com wrote:

I am currently checking out this unit library for inclusion in our next project. One problem I came across is that it seems impossible to create two units that are equivalent in their properties/dimensions, yet must not be mixed. For example, in electrical engineering there is, besides power (measured in W), also reactive power (var) and apparent power (voltampere). All are the result of multiplying voltage with current, but it is important that they are different units that the compiler sees as incompatible.

An in-house library I used to use supported that by their units having a unique_tag template parameter (which defaulted to void), and units with different tags being incompatible.

Am I overlooking something (well possible) or does this (otherwise great!) library not currently offer such a feature?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/nholthaus/units/issues/234?email_source=notifications&email_token=ACOYOH2W653C2OZISHHWRYDQJJVYDA5CNFSM4IWG4VFKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HLBIKNQ, or mute the thread https://github.com/notifications/unsubscribe-auth/ACOYOH6YLUZESEUS6KYFJHTQJJVYDANCNFSM4IWG4VFA .

GElliott commented 4 years ago

This project is currently described as: "a compile-time, header-only, dimensional analysis and unit conversion library built on c++14 with no dependencies."

I'm disappointed to hear that C++20 is the future for this library. Safety-related systems can really benefit from this library, and many of these are stuck with C++14 (at best) today (see: QNX GNU toolchain).

kunaltyagi commented 4 years ago

@GElliott I'm also in need something similar and I'm at the experimenting stage. The 3.x branch works with C++17 which shouldn't be a big change compared to C++14 (Most errors are related to the <type_trait>_v missing. That can be modified (I guess by adding overloads in std namespace).

I have a working system for now, but I really just started 2 days ago, so YMMV

gitsbi commented 4 years ago

Unfortunately, we need a unit test library soon, and it's questionable when we will have access to a compiler better than C++03 on one of our target platforms, and which version that will be. It definitely won't be C++20, though. I see more issues with this library, but those might not be show stoppers. But non-SI units that derive from SI units are an absolute must for us.

All this is a hassle to do in C++03 (BTDT), but it can be done comparatively easily in C++11. Yet, I have not found a library that has been around for a while and is actively maintained which has the mix of features we need and relies on nothing more that C++11. So it really looks like we need to do our own. 😞

GElliott commented 4 years ago

@gitsbi You might want to take a look at boost's units library if you need C++03 support. I have no doubt that you'll want to write your own wrappers---the API is pretty complex. Last I checked, it also didn't support constexpr (possibly resolved by now). That said, I've seen it used successfully in safety-critical commercial products.

What I like most about @nholthaus's library is (1) straightforward API and (2) constexpr support.

gitsbi commented 3 years ago

@nholthaus You might want to have a look at https://github.com/gitsbi/unlib to see what I have been cooking up since then.