bernedom / SI

A header only C++ library that provides type safety and user defined literals for physical units
https://si.dominikberner.ch/doc/
MIT License
486 stars 40 forks source link

Return value with known magnitude #104

Closed ypearson closed 2 years ago

ypearson commented 2 years ago

If you have an Length object and you call obj.value() it may not be clear what unit it returns eg. // somewhere in the code SI::milli_metre_t<long double> x{100}; //somewhere else in the code x.value(); // is this in millimeter or meter?

Provide a way to clearly describe what magnitude the raw value should represent eg. constexpr long double value_as(const Length u) { return (*this/u).value()} //usage long double m = x.value_as(meter); // m=0.1 long double mm = x.value_as(millimeter); // mm=100

Describe alternatives you've considered Use value() and track base unit in comments or notes

bernedom commented 2 years ago

The magnitude or ratio is always in the ratio of the unit.


SI::milli_metre_t<long double> x{100};
x.value(); // will return 100 

You can use the function SI::unit_cast(unit u) for that and I added a function as<T>() to unit in the associated pull request. https://github.com/bernedom/SI/pull/105..

so this should work


SI::kilo_metre_t<long double> x{1};
constexpr auto mm = x.as<SI::milli_metre_t>().value() // Will return 1000000 'mm' is of type SI::milli_metre_t
constexpr auto mm2 = SI::unit_cast<S::milli_metre_t>(x); // has the same effect as above

This will obivously only work for units of the same dimension and compatible types

ypearson commented 2 years ago

Excited to use this new feature!