aurora-opensource / au

A C++14-compatible physical units library with no dependencies and a single-file delivery option. Emphasis on safety, accessibility, performance, and developer experience.
Apache License 2.0
322 stars 19 forks source link

Support constants #90

Open chiphogg opened 1 year ago

chiphogg commented 1 year ago

In some sense, we can already do this.

constexpr auto RAD = radians(1);

However, this brings int into the equation, via the 1. This could affect the arithmetic of the underlying Reps in some equations where it gets used. It could also run afoul of our guards against integer division.

It would be nice if we could express constants solely in the realm of dimensions and magnitudes. We might need to make a new type template, such as Constant. It would probably be a "monovalue type" (in the nomenclature introduced in #88). Perhaps something like this:

constexpr auto RAD = make_constant(Radians{});

Alternatively: maybe a "constant" is just an instance of a unit? We would need to enable multiplying and dividing a quantity by a unit, if so. Then we would write:

constexpr auto RAD = Radians{};

It's interesting to imagine how this would look with other constants. Consider something like $E = mc^2$. We might write kilo(grams)(10.0) * squared(C)... and the result might be labeled as 10 kg * c^2. Of course we could also apply .as(joules) to the result to get a more familiar unit.

This is an intriguing idea worth exploring more later.

chiphogg commented 1 year ago

Having thought more about this: no, a constant should not just be an instance of a unit. A "unit" could be basically any type, so we'd need SFINAE, which would be expensive. This argues in favor of using a Constant template.

It's probably still good to keep this template as a monovalue type, and use a labeled unit.

chiphogg commented 10 months ago

Here's a running checklist for what it would take to call this feature "done". (I'll check things off as they're implemented locally in my client, rather than on remote.)

geoffviola commented 10 months ago

We may want to add some documentation for how to add a new constant, after we settle on the presentation.