refactor: op[U] for quantity and quantity_point replaced with .in(U)

Resolves #469
This commit is contained in:
Mateusz Pusz
2023-08-23 16:46:15 +02:00
parent d8f646a1e4
commit ae92b49775
24 changed files with 113 additions and 113 deletions

View File

@ -109,7 +109,7 @@ int main()
constexpr auto v2 = 70 * mph; constexpr auto v2 = 70 * mph;
constexpr auto v3 = avg_speed(220. * isq::distance[km], 2 * h); constexpr auto v3 = avg_speed(220. * isq::distance[km], 2 * h);
constexpr auto v4 = avg_speed(isq::distance(140. * mi), 2 * h); constexpr auto v4 = avg_speed(isq::distance(140. * mi), 2 * h);
constexpr auto v5 = v3[m / s]; constexpr auto v5 = v3.in(m / s);
constexpr auto v6 = value_cast<m / s>(v4); constexpr auto v6 = value_cast<m / s>(v4);
constexpr auto v7 = value_cast<int>(v6); constexpr auto v7 = value_cast<int>(v6);
@ -123,4 +123,4 @@ int main()
} }
``` ```
_Try it on the [Compiler Explorer](https://godbolt.org/z/zPnerKqzh)._ _Try it on the [Compiler Explorer](https://godbolt.org/z/E4rvPGa3n)._

View File

@ -62,7 +62,7 @@ int main()
constexpr auto v2 = 70 * mph; constexpr auto v2 = 70 * mph;
constexpr auto v3 = avg_speed(220. * isq::distance[km], 2 * h); constexpr auto v3 = avg_speed(220. * isq::distance[km], 2 * h);
constexpr auto v4 = avg_speed(isq::distance(140. * mi), 2 * h); constexpr auto v4 = avg_speed(isq::distance(140. * mi), 2 * h);
constexpr auto v5 = v3[m / s]; constexpr auto v5 = v3.in(m / s);
constexpr auto v6 = value_cast<m / s>(v4); constexpr auto v6 = value_cast<m / s>(v4);
constexpr auto v7 = value_cast<int>(v6); constexpr auto v7 = value_cast<int>(v6);
@ -76,8 +76,8 @@ int main()
} }
``` ```
!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/zPnerKqzh)" !!! example "[Try it on Compiler Explorer](https://godbolt.org/z/E4rvPGa3n)"
!!! note !!! note
You can find more code examples in the Examples chapter. You can find more code examples in the [Examples](../users_guide/examples/tags_index.md) chapter.

View File

@ -61,7 +61,7 @@ constexpr auto speed_of_light_in_vacuum = 1 * si::si2019::speed_of_light_in_vacu
QuantityOf<isq::permittivity_of_vacuum> auto q = 1 / (permeability_of_vacuum * pow<2>(speed_of_light_in_vacuum)); QuantityOf<isq::permittivity_of_vacuum> auto q = 1 / (permeability_of_vacuum * pow<2>(speed_of_light_in_vacuum));
std::cout << "permittivity of vacuum = " << q << " = " << q[F / m] << "\n"; std::cout << "permittivity of vacuum = " << q << " = " << q.in(F / m) << "\n";
``` ```
The above first prints the following: The above first prints the following:
@ -99,18 +99,18 @@ std::cout << "in `GeV` and `c`:\n"
<< "m = " << m1 << "\n" << "m = " << m1 << "\n"
<< "E = " << E << "\n"; << "E = " << E << "\n";
const auto p2 = p1[GeV / (m / s)]; const auto p2 = p1.in(GeV / (m / s));
const auto m2 = m1[GeV / pow<2>(m / s)]; const auto m2 = m1.in(GeV / pow<2>(m / s));
const auto E2 = total_energy(p2, m2, c)[GeV]; const auto E2 = total_energy(p2, m2, c).in(GeV);
std::cout << "\nin `GeV`:\n" std::cout << "\nin `GeV`:\n"
<< "p = " << p2 << "\n" << "p = " << p2 << "\n"
<< "m = " << m2 << "\n" << "m = " << m2 << "\n"
<< "E = " << E2 << "\n"; << "E = " << E2 << "\n";
const auto p3 = p1[kg * m / s]; const auto p3 = p1.in(kg * m / s);
const auto m3 = m1[kg]; const auto m3 = m1.in(kg);
const auto E3 = total_energy(p3, m3, c)[J]; const auto E3 = total_energy(p3, m3, c).in(J);
std::cout << "\nin SI base units:\n" std::cout << "\nin SI base units:\n"
<< "p = " << p3 << "\n" << "p = " << p3 << "\n"

View File

@ -264,7 +264,7 @@ using namespace mp_units::si::unit_symbols;
auto speed = 60. * isq::speed[km / h]; auto speed = 60. * isq::speed[km / h];
auto duration = 8 * s; auto duration = 8 * s;
auto acceleration = speed / duration; auto acceleration = speed / duration;
std::cout << "acceleration: " << acceleration << " (" << acceleration[m / s2] << ")\n"; std::cout << "acceleration: " << acceleration << " (" << acceleration.in(m / s2) << ")\n";
``` ```
The `acceleration`, being the result of the above code, has the following type The `acceleration`, being the result of the above code, has the following type

View File

@ -72,7 +72,7 @@ int main()
std::cout << "A car driving " << distance << " in " << duration std::cout << "A car driving " << distance << " in " << duration
<< " has an average speed of " << speed << " has an average speed of " << speed
<< " (" << speed[km / h] << ")\n"; << " (" << speed.in(km / h) << ")\n";
} }
``` ```
@ -82,7 +82,7 @@ The code above prints:
A car driving 110 km in 2 h has an average speed of 15.2778 m/s (55 km/h) A car driving 110 km in 2 h has an average speed of 15.2778 m/s (55 km/h)
``` ```
!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/4zecYqn5z)" !!! example "[Try it on Compiler Explorer](https://godbolt.org/z/zWe8ecf93)"
### Easy to understand compilation error messages ### Easy to understand compilation error messages
@ -146,7 +146,7 @@ int main()
std::cout << "A car driving " << distance << " in " << duration std::cout << "A car driving " << distance << " in " << duration
<< " has an average speed of " << speed << " has an average speed of " << speed
<< " (" << speed[km / h] << ")\n"; << " (" << speed.in(km / h) << ")\n";
} }
``` ```
@ -154,7 +154,7 @@ int main()
A car driving 110 km in 2 h has an average speed of 15.2778 m/s (55 km/h) A car driving 110 km in 2 h has an average speed of 15.2778 m/s (55 km/h)
``` ```
!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/jhfWjGadz)" !!! example "[Try it on Compiler Explorer](https://godbolt.org/z/q3PzMzqsh)"
In case we will accidentally make the same calculation error as before, this time, we will In case we will accidentally make the same calculation error as before, this time, we will
get a bit longer error message also containing information about the quantity type: get a bit longer error message also containing information about the quantity type:

View File

@ -76,9 +76,9 @@ the following:
```cpp ```cpp
auto q1 = 42 * W; auto q1 = 42 * W;
std::cout << q1 << "\n"; std::cout << q1 << "\n";
std::cout << q1[J / s] << "\n"; std::cout << q1.in(J / s) << "\n";
std::cout << q1[N * m / s] << "\n"; std::cout << q1.in(N * m / s) << "\n";
std::cout << q1[kg * m2 / s3] << "\n"; std::cout << q1.in(kg * m2 / s3) << "\n";
``` ```
prints: prints:

View File

@ -44,7 +44,7 @@ associated with this quantity.
before passing it to the text output: before passing it to the text output:
```cpp ```cpp
std::cout << v1[km / h] << '\n'; // 110 km/h std::cout << v1.in(km / h) << '\n'; // 110 km/h
std::cout << value_cast<m / s>(v1) << '\n'; // 30.5556 m/s std::cout << value_cast<m / s>(v1) << '\n'; // 30.5556 m/s
``` ```

View File

@ -4,7 +4,7 @@
```cpp ```cpp
auto q1 = 5 * km; auto q1 = 5 * km;
std::cout << q1[m] << '\n'; std::cout << q1.in(m) << '\n';
quantity<si::metre, int> q2 = q1; quantity<si::metre, int> q2 = q1;
``` ```
@ -23,7 +23,7 @@ In case a user would like to perform an opposite transformation:
```cpp ```cpp
auto q1 = 5 * m; auto q1 = 5 * m;
std::cout << q1[km] << '\n'; std::cout << q1.in(km) << '\n';
quantity<si::kilo<si::metre>, int> q2 = q1; quantity<si::kilo<si::metre>, int> q2 = q1;
``` ```
@ -34,7 +34,7 @@ representation type:
```cpp ```cpp
auto q1 = 5. * m; auto q1 = 5. * m;
std::cout << q1[km] << '\n'; std::cout << q1.in(km) << '\n';
quantity<si::kilo<si::metre>> q2 = q1; quantity<si::kilo<si::metre>> q2 = q1;
``` ```

View File

@ -47,15 +47,15 @@ int main()
std::cout << "at " << tt << " voltage is "; std::cout << "at " << tt << " voltage is ";
if (Vt >= 1 * V) if (Vt >= 1 * V)
std::cout << Vt[V]; std::cout << Vt.in(V);
else if (Vt >= 1 * mV) else if (Vt >= 1 * mV)
std::cout << Vt[mV]; std::cout << Vt.in(mV);
else if (Vt >= 1 * uV) else if (Vt >= 1 * uV)
std::cout << Vt[uV]; std::cout << Vt.in(uV);
else if (Vt >= 1 * nV) else if (Vt >= 1 * nV)
std::cout << Vt[nV]; std::cout << Vt.in(nV);
else else
std::cout << Vt[pV]; std::cout << Vt.in(pV);
std::cout << "\n"; std::cout << "\n";
} }
} }

View File

@ -81,24 +81,24 @@ void quantities_with_typed_units()
std::cout << meter << '\n'; std::cout << meter << '\n';
std::cout << " = " << meter[astronomical_unit] << '\n'; std::cout << " = " << meter.in(astronomical_unit) << '\n';
std::cout << " = " << meter[iau::angstrom] << '\n'; std::cout << " = " << meter.in(iau::angstrom) << '\n';
std::cout << " = " << meter[imperial::chain] << '\n'; std::cout << " = " << meter.in(imperial::chain) << '\n';
std::cout << " = " << meter[imperial::fathom] << '\n'; std::cout << " = " << meter.in(imperial::fathom) << '\n';
std::cout << " = " << meter[usc::fathom] << '\n'; std::cout << " = " << meter.in(usc::fathom) << '\n';
std::cout << " = " << meter[international::foot] << '\n'; std::cout << " = " << meter.in(international::foot) << '\n';
std::cout << " = " << meter[usc::survey1893::us_survey_foot] << '\n'; std::cout << " = " << meter.in(usc::survey1893::us_survey_foot) << '\n';
std::cout << " = " << meter[international::inch] << '\n'; std::cout << " = " << meter.in(international::inch) << '\n';
std::cout << " = " << meter[iau::light_year] << '\n'; std::cout << " = " << meter.in(iau::light_year) << '\n';
std::cout << " = " << meter[international::mile] << '\n'; std::cout << " = " << meter.in(international::mile) << '\n';
std::cout << " = " << meter[international::nautical_mile] << '\n'; std::cout << " = " << meter.in(international::nautical_mile) << '\n';
std::cout << " = " << meter[iau::parsec] << '\n'; std::cout << " = " << meter.in(iau::parsec) << '\n';
std::cout << " = " << meter[typographic::pica_dtp] << '\n'; std::cout << " = " << meter.in(typographic::pica_dtp) << '\n';
std::cout << " = " << meter[typographic::pica_us] << '\n'; std::cout << " = " << meter.in(typographic::pica_us) << '\n';
std::cout << " = " << meter[typographic::point_dtp] << '\n'; std::cout << " = " << meter.in(typographic::point_dtp) << '\n';
std::cout << " = " << meter[typographic::point_us] << '\n'; std::cout << " = " << meter.in(typographic::point_us) << '\n';
std::cout << " = " << meter[imperial::rod] << '\n'; std::cout << " = " << meter.in(imperial::rod) << '\n';
std::cout << " = " << meter[international::yard] << '\n'; std::cout << " = " << meter.in(international::yard) << '\n';
} }
void calcs_comparison() void calcs_comparison()
@ -119,8 +119,8 @@ void calcs_comparison()
"or small values in other units to the base unit.\n" "or small values in other units to the base unit.\n"
"This is both inefficient and inaccurate\n\n"; "This is both inefficient and inaccurate\n\n";
const auto L1B = L1A[m]; const auto L1B = L1A.in(m);
const auto L2B = L2A[m]; const auto L2B = L2A.in(m);
const auto LrB = L1B + L2B; const auto LrB = L1B + L2B;
std::cout << MP_UNITS_STD_FMT::format("{:%.30eQ %q}\n + {:%.30eQ %q}\n = {:%.30eQ %q}\n\n", L1B, L2B, LrB); std::cout << MP_UNITS_STD_FMT::format("{:%.30eQ %q}\n + {:%.30eQ %q}\n = {:%.30eQ %q}\n\n", L1B, L2B, LrB);

View File

@ -46,7 +46,7 @@ int main()
std::cout << "conversion factor in mp-units...\n\n"; std::cout << "conversion factor in mp-units...\n\n";
constexpr auto lengthA = 2.0 * m; constexpr auto lengthA = 2.0 * m;
constexpr auto lengthB = lengthA[mm]; constexpr auto lengthB = lengthA.in(mm);
std::cout << MP_UNITS_STD_FMT::format("lengthA( {} ) and lengthB( {} )\n", lengthA, lengthB) std::cout << MP_UNITS_STD_FMT::format("lengthA( {} ) and lengthB( {} )\n", lengthA, lengthB)
<< "represent the same length in different units.\n\n"; << "represent the same length in different units.\n\n";

View File

@ -48,7 +48,7 @@ int main()
constexpr auto v2 = 70 * mph; constexpr auto v2 = 70 * mph;
constexpr auto v3 = avg_speed(220. * km, 2 * h); constexpr auto v3 = avg_speed(220. * km, 2 * h);
constexpr auto v4 = avg_speed(isq::distance(140. * mi), 2 * isq::duration[h]); constexpr auto v4 = avg_speed(isq::distance(140. * mi), 2 * isq::duration[h]);
constexpr auto v5 = v3[m / s]; constexpr auto v5 = v3.in(m / s);
constexpr auto v6 = value_cast<m / s>(v4); constexpr auto v6 = value_cast<m / s>(v4);
constexpr auto v7 = value_cast<int>(v6); constexpr auto v7 = value_cast<int>(v6);

View File

@ -135,7 +135,7 @@ void example()
const auto t = measurement{1.2, 0.1} * s; const auto t = measurement{1.2, 0.1} * s;
const QuantityOf<isq::velocity> auto v = a * t; const QuantityOf<isq::velocity> auto v = a * t;
std::cout << a << " * " << t << " = " << v << " = " << v[km / h] << '\n'; std::cout << a << " * " << t << " = " << v << " = " << v.in(km / h) << '\n';
const auto length = measurement{123., 1.} * m; const auto length = measurement{123., 1.} * m;
std::cout << "10 * " << length << " = " << 10 * length << '\n'; std::cout << "10 * " << length << " = " << 10 * length << '\n';

View File

@ -20,9 +20,13 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!! Before you commit any changes to this file please make sure to check if it !!!
// !!! renders correctly in the documentation "Examples" section. !!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include <mp-units/format.h> #include <mp-units/format.h>
#include <mp-units/systems/si/constants.h> #include <mp-units/systems/si/si.h>
#include <mp-units/systems/si/unit_symbols.h>
#include <iostream> #include <iostream>
template<class T> template<class T>
@ -37,18 +41,18 @@ int main()
std::cout << "The seven defining constants of the SI and the seven corresponding units they define:\n"; std::cout << "The seven defining constants of the SI and the seven corresponding units they define:\n";
std::cout << MP_UNITS_STD_FMT::format("- hyperfine transition frequency of Cs: {} = {:%.0Q %q}\n", std::cout << MP_UNITS_STD_FMT::format("- hyperfine transition frequency of Cs: {} = {:%.0Q %q}\n",
1. * si2019::hyperfine_structure_transition_frequency_of_cs, 1. * si2019::hyperfine_structure_transition_frequency_of_cs,
(1. * si2019::hyperfine_structure_transition_frequency_of_cs)[Hz]); (1. * si2019::hyperfine_structure_transition_frequency_of_cs).in(Hz));
std::cout << MP_UNITS_STD_FMT::format("- speed of light in vacuum: {} = {:%.0Q %q}\n", std::cout << MP_UNITS_STD_FMT::format("- speed of light in vacuum: {} = {:%.0Q %q}\n",
1. * si2019::speed_of_light_in_vacuum, 1. * si2019::speed_of_light_in_vacuum,
(1. * si2019::speed_of_light_in_vacuum)[m / s]); (1. * si2019::speed_of_light_in_vacuum).in(m / s));
std::cout << MP_UNITS_STD_FMT::format("- Planck constant: {} = {:%.8eQ %q}\n", std::cout << MP_UNITS_STD_FMT::format("- Planck constant: {} = {:%.8eQ %q}\n",
1. * si2019::planck_constant, (1. * si2019::planck_constant)[J * s]); 1. * si2019::planck_constant, (1. * si2019::planck_constant).in(J * s));
std::cout << MP_UNITS_STD_FMT::format("- elementary charge: {} = {:%.9eQ %q}\n", std::cout << MP_UNITS_STD_FMT::format("- elementary charge: {} = {:%.9eQ %q}\n",
1. * si2019::elementary_charge, (1. * si2019::elementary_charge)[C]); 1. * si2019::elementary_charge, (1. * si2019::elementary_charge).in(C));
std::cout << MP_UNITS_STD_FMT::format("- Boltzmann constant: {} = {:%.6eQ %q}\n", std::cout << MP_UNITS_STD_FMT::format("- Boltzmann constant: {} = {:%.6eQ %q}\n",
1. * si2019::boltzmann_constant, (1. * si2019::boltzmann_constant)[J / K]); 1. * si2019::boltzmann_constant, (1. * si2019::boltzmann_constant).in(J / K));
std::cout << MP_UNITS_STD_FMT::format("- Avogadro constant: {} = {:%.8eQ %q}\n", std::cout << MP_UNITS_STD_FMT::format("- Avogadro constant: {} = {:%.8eQ %q}\n",
1. * si2019::avogadro_constant, (1. * si2019::avogadro_constant)[1 / mol]); 1. * si2019::avogadro_constant, (1. * si2019::avogadro_constant).in(1 / mol));
// TODO uncomment the below when ISQ is done // TODO uncomment the below when ISQ is done
// std::cout << MP_UNITS_STD_FMT::format("- luminous efficacy: {} = {}\n", // std::cout << MP_UNITS_STD_FMT::format("- luminous efficacy: {} = {}\n",
// si2019::luminous_efficacy(1.), // si2019::luminous_efficacy(1.),

View File

@ -60,8 +60,9 @@ template<QuantityOf<isq::energy> T1, QuantityOf<isq::wavenumber> T2, QuantityOf<
QuantityOf<isq::thermodynamic_temperature> T4, QuantityOf<isq::wavelength> T5> QuantityOf<isq::thermodynamic_temperature> T4, QuantityOf<isq::wavelength> T5>
void print_line_si(const std::tuple<T1, T2, T3, T4, T5>& t) void print_line_si(const std::tuple<T1, T2, T3, T4, T5>& t)
{ {
MP_UNITS_STD_FMT::println("| {:<15} | {:<15} | {:<15} | {:<15} | {:<15} |", std::get<0>(t)[eV], MP_UNITS_STD_FMT::println("| {:<15} | {:<15} | {:<15} | {:<15} | {:<15} |", std::get<0>(t).in(eV),
std::get<1>(t)[1 / cm], std::get<2>(t)[THz], std::get<3>(t)[K], std::get<4>(t)[um]); std::get<1>(t).in(1 / cm), std::get<2>(t).in(THz), std::get<3>(t).in(K),
std::get<4>(t).in(um));
} }
int main() int main()

View File

@ -126,10 +126,10 @@ int main()
const auto fill_ratio = fill_level / height; const auto fill_ratio = fill_level / height;
std::cout << MP_UNITS_STD_FMT::format("fill height at {} = {} ({} full)\n", fill_time, fill_level, std::cout << MP_UNITS_STD_FMT::format("fill height at {} = {} ({} full)\n", fill_time, fill_level,
fill_ratio[percent]); fill_ratio.in(percent));
std::cout << MP_UNITS_STD_FMT::format("fill weight at {} = {} ({})\n", fill_time, filled_weight, filled_weight[N]); std::cout << MP_UNITS_STD_FMT::format("fill weight at {} = {} ({})\n", fill_time, filled_weight, filled_weight.in(N));
std::cout << MP_UNITS_STD_FMT::format("spare capacity at {} = {}\n", fill_time, spare_capacity); std::cout << MP_UNITS_STD_FMT::format("spare capacity at {} = {}\n", fill_time, spare_capacity);
std::cout << MP_UNITS_STD_FMT::format("input flow rate = {}\n", input_flow_rate); std::cout << MP_UNITS_STD_FMT::format("input flow rate = {}\n", input_flow_rate);
std::cout << MP_UNITS_STD_FMT::format("float rise rate = {}\n", float_rise_rate); std::cout << MP_UNITS_STD_FMT::format("float rise rate = {}\n", float_rise_rate);
std::cout << MP_UNITS_STD_FMT::format("tank full E.T.A. at current flow rate = {}\n", fill_time_left[s]); std::cout << MP_UNITS_STD_FMT::format("tank full E.T.A. at current flow rate = {}\n", fill_time_left.in(s));
} }

View File

@ -44,5 +44,5 @@ int main()
const auto torque = isq_angle::torque(lever * force * angular::sin(angle) / (1 * isq_angle::cotes_angle)); const auto torque = isq_angle::torque(lever * force * angular::sin(angle) / (1 * isq_angle::cotes_angle));
std::cout << "Applying a perpendicular force of " << force << " to a " << lever << " long lever results in " std::cout << "Applying a perpendicular force of " << force << " to a " << lever << " long lever results in "
<< torque[N * m / rad] << " of torque.\n"; << torque.in(N * m / rad) << " of torque.\n";
} }

View File

@ -48,31 +48,31 @@ void si_example()
using namespace mp_units::si::unit_symbols; using namespace mp_units::si::unit_symbols;
constexpr auto GeV = si::giga<si::electronvolt>; constexpr auto GeV = si::giga<si::electronvolt>;
constexpr QuantityOf<isq::speed> auto c = 1. * si::si2019::speed_of_light_in_vacuum; constexpr QuantityOf<isq::speed> auto c = 1. * si::si2019::speed_of_light_in_vacuum;
auto c2 = pow<2>(c); constexpr auto c2 = pow<2>(c);
const auto p1 = isq::momentum(4. * GeV / c); const auto p1 = isq::momentum(4. * GeV / c);
const QuantityOf<isq::mass> auto m1 = 3. * GeV / c2; const QuantityOf<isq::mass> auto m1 = 3. * GeV / c2;
const auto E = total_energy(p1, m1, c); const auto E = total_energy(p1, m1, c);
std::cout << "\n*** SI units (c = " << c << " = " << c[si::metre / s] << ") ***\n"; std::cout << "\n*** SI units (c = " << c << " = " << c.in(si::metre / s) << ") ***\n";
std::cout << "\n[in `GeV` and `c`]\n" std::cout << "\n[in `GeV` and `c`]\n"
<< "p = " << p1 << "\n" << "p = " << p1 << "\n"
<< "m = " << m1 << "\n" << "m = " << m1 << "\n"
<< "E = " << E << "\n"; << "E = " << E << "\n";
const auto p2 = p1[GeV / (m / s)]; const auto p2 = p1.in(GeV / (m / s));
const auto m2 = m1[GeV / pow<2>(m / s)]; const auto m2 = m1.in(GeV / pow<2>(m / s));
const auto E2 = total_energy(p2, m2, c)[GeV]; const auto E2 = total_energy(p2, m2, c).in(GeV);
std::cout << "\n[in `GeV`]\n" std::cout << "\n[in `GeV`]\n"
<< "p = " << p2 << "\n" << "p = " << p2 << "\n"
<< "m = " << m2 << "\n" << "m = " << m2 << "\n"
<< "E = " << E2 << "\n"; << "E = " << E2 << "\n";
const auto p3 = p1[kg * m / s]; const auto p3 = p1.in(kg * m / s);
const auto m3 = m1[kg]; const auto m3 = m1.in(kg);
const auto E3 = total_energy(p3, m3, c)[J]; const auto E3 = total_energy(p3, m3, c).in(J);
std::cout << "\n[in SI base units]\n" std::cout << "\n[in SI base units]\n"
<< "p = " << p3 << "\n" << "p = " << p3 << "\n"

View File

@ -164,15 +164,15 @@ public:
#endif #endif
template<Unit U> template<Unit U>
requires requires(quantity q) { q[U{}]; } requires requires(quantity q) { q.in(U{}); }
[[nodiscard]] constexpr rep number_in(U) const noexcept [[nodiscard]] constexpr rep number_in(U) const noexcept
{ {
return (*this)[U{}].number(); return (*this).in(U{}).number();
} }
template<Unit U> template<Unit U>
requires detail::QuantityConvertibleTo<quantity, quantity<::mp_units::reference<quantity_spec, U{}>{}, Rep>> requires detail::QuantityConvertibleTo<quantity, quantity<::mp_units::reference<quantity_spec, U{}>{}, Rep>>
[[nodiscard]] constexpr quantity<::mp_units::reference<quantity_spec, U{}>{}, Rep> operator[](U) const [[nodiscard]] constexpr quantity<::mp_units::reference<quantity_spec, U{}>{}, Rep> in(U) const
{ {
return quantity<quantity_spec[U{}], Rep>{*this}; return quantity<quantity_spec[U{}], Rep>{*this};
} }

View File

@ -164,9 +164,9 @@ public:
template<Unit U> template<Unit U>
requires detail::QuantityConvertibleTo<quantity_type, quantity<::mp_units::reference<quantity_spec, U{}>{}, Rep>> requires detail::QuantityConvertibleTo<quantity_type, quantity<::mp_units::reference<quantity_spec, U{}>{}, Rep>>
[[nodiscard]] constexpr quantity_point<::mp_units::reference<quantity_spec, U{}>{}, PO, Rep> operator[](U) const [[nodiscard]] constexpr quantity_point<::mp_units::reference<quantity_spec, U{}>{}, PO, Rep> in(U) const
{ {
return make_quantity_point<PO>(quantity_from_origin()[U{}]); return make_quantity_point<PO>(quantity_from_origin().in(U{}));
} }
// member unary operators // member unary operators

View File

@ -304,7 +304,7 @@ template<ReferenceOf<angular_measure> auto R, typename Rep>
requires requires { sin(q.number()); } || requires { std::sin(q.number()); } requires requires { sin(q.number()); } || requires { std::sin(q.number()); }
{ {
using std::sin; using std::sin;
return make_quantity<one>(static_cast<Rep>(sin(q[si::radian].number()))); return make_quantity<one>(static_cast<Rep>(sin(q.in(si::radian).number())));
} }
template<ReferenceOf<angular_measure> auto R, typename Rep> template<ReferenceOf<angular_measure> auto R, typename Rep>
@ -313,7 +313,7 @@ template<ReferenceOf<angular_measure> auto R, typename Rep>
requires requires { cos(q.number()); } || requires { std::cos(q.number()); } requires requires { cos(q.number()); } || requires { std::cos(q.number()); }
{ {
using std::cos; using std::cos;
return make_quantity<one>(static_cast<Rep>(cos(q[si::radian].number()))); return make_quantity<one>(static_cast<Rep>(cos(q.in(si::radian).number())));
} }
template<ReferenceOf<angular_measure> auto R, typename Rep> template<ReferenceOf<angular_measure> auto R, typename Rep>
@ -322,7 +322,7 @@ template<ReferenceOf<angular_measure> auto R, typename Rep>
requires requires { tan(q.number()); } || requires { std::tan(q.number()); } requires requires { tan(q.number()); } || requires { std::tan(q.number()); }
{ {
using std::tan; using std::tan;
return make_quantity<one>(static_cast<Rep>(tan(q[si::radian].number()))); return make_quantity<one>(static_cast<Rep>(tan(q.in(si::radian).number())));
} }
template<ReferenceOf<dimensionless> auto R, typename Rep> template<ReferenceOf<dimensionless> auto R, typename Rep>
@ -362,7 +362,7 @@ template<ReferenceOf<angle> auto R, typename Rep>
requires requires { sin(q.number()); } || requires { std::sin(q.number()); } requires requires { sin(q.number()); } || requires { std::sin(q.number()); }
{ {
using std::sin; using std::sin;
return make_quantity<one>(static_cast<Rep>(sin(q[radian].number()))); return make_quantity<one>(static_cast<Rep>(sin(q.in(radian).number())));
} }
template<ReferenceOf<angle> auto R, typename Rep> template<ReferenceOf<angle> auto R, typename Rep>
@ -371,7 +371,7 @@ template<ReferenceOf<angle> auto R, typename Rep>
requires requires { cos(q.number()); } || requires { std::cos(q.number()); } requires requires { cos(q.number()); } || requires { std::cos(q.number()); }
{ {
using std::cos; using std::cos;
return make_quantity<one>(static_cast<Rep>(cos(q[radian].number()))); return make_quantity<one>(static_cast<Rep>(cos(q.in(radian).number())));
} }
template<ReferenceOf<angle> auto R, typename Rep> template<ReferenceOf<angle> auto R, typename Rep>
@ -380,7 +380,7 @@ template<ReferenceOf<angle> auto R, typename Rep>
requires requires { tan(q.number()); } || requires { std::tan(q.number()); } requires requires { tan(q.number()); } || requires { std::tan(q.number()); }
{ {
using std::tan; using std::tan;
return make_quantity<one>(static_cast<Rep>(tan(q[radian].number()))); return make_quantity<one>(static_cast<Rep>(tan(q.in(radian).number())));
} }
template<ReferenceOf<dimensionless> auto R, typename Rep> template<ReferenceOf<dimensionless> auto R, typename Rep>

View File

@ -31,17 +31,14 @@
#include <cmath> #include <cmath>
#include <matrix> #include <matrix>
template<typename Rep = double> template<typename Rep = double>
using vector = STD_LA::fixed_size_column_vector<Rep, 3>; using vector = STD_LA::fixed_size_column_vector<Rep, 3>;
template<typename Rep> template<typename Rep>
inline constexpr bool mp_units::is_vector<vector<Rep>> = true; inline constexpr bool mp_units::is_vector<vector<Rep>> = true;
namespace STD_LA {
template<typename Rep> template<typename Rep>
std::ostream& operator<<(std::ostream& os, const ::vector<Rep>& v) std::ostream& operator<<(std::ostream& os, const vector<Rep>& v)
{ {
os << "|"; os << "|";
for (auto i = 0U; i < v.size(); ++i) { for (auto i = 0U; i < v.size(); ++i) {
@ -51,8 +48,6 @@ std::ostream& operator<<(std::ostream& os, const ::vector<Rep>& v)
return os; return os;
} }
} // namespace STD_LA
namespace { namespace {
using namespace mp_units; using namespace mp_units;
@ -88,7 +83,7 @@ TEST_CASE("vector quantity", "[la]")
SECTION("non-truncating") SECTION("non-truncating")
{ {
const auto v = vector<int>{3, 2, 1} * isq::position_vector[km]; const auto v = vector<int>{3, 2, 1} * isq::position_vector[km];
CHECK(v[m].number() == vector<int>{3000, 2000, 1000}); CHECK(v.in(m).number() == vector<int>{3000, 2000, 1000});
} }
SECTION("truncating") SECTION("truncating")

View File

@ -576,21 +576,21 @@ static_assert(is_of_type<(ground_level + isq::height(short(42) * m)).point_for(m
// converting to a different unit // converting to a different unit
/////////////////////////////////// ///////////////////////////////////
static_assert((mean_sea_level + 2. * km)[km].quantity_from_origin().number() == 2.); static_assert((mean_sea_level + 2. * km).in(km).quantity_from_origin().number() == 2.);
static_assert((mean_sea_level + 2. * km)[m].quantity_from_origin().number() == 2000.); static_assert((mean_sea_level + 2. * km).in(m).quantity_from_origin().number() == 2000.);
static_assert((mean_sea_level + 2000. * m)[km].quantity_from_origin().number() == 2.); static_assert((mean_sea_level + 2000. * m).in(km).quantity_from_origin().number() == 2.);
static_assert((ground_level + 2. * km)[km].quantity_from_origin().number() == 2.); static_assert((ground_level + 2. * km).in(km).quantity_from_origin().number() == 2.);
static_assert((ground_level + 2. * km)[m].quantity_from_origin().number() == 2000.); static_assert((ground_level + 2. * km).in(m).quantity_from_origin().number() == 2000.);
static_assert((ground_level + 2000. * m)[km].quantity_from_origin().number() == 2.); static_assert((ground_level + 2000. * m).in(km).quantity_from_origin().number() == 2.);
static_assert((tower_peak + 2. * km)[km].quantity_from_origin().number() == 2.); static_assert((tower_peak + 2. * km).in(km).quantity_from_origin().number() == 2.);
static_assert((tower_peak + 2. * km)[m].quantity_from_origin().number() == 2000.); static_assert((tower_peak + 2. * km).in(m).quantity_from_origin().number() == 2000.);
static_assert((tower_peak + 2000. * m)[km].quantity_from_origin().number() == 2.); static_assert((tower_peak + 2000. * m).in(km).quantity_from_origin().number() == 2.);
#if MP_UNITS_COMP_GCC != 10 || MP_UNITS_COMP_GCC_MINOR > 2 #if MP_UNITS_COMP_GCC != 10 || MP_UNITS_COMP_GCC_MINOR > 2
template<template<auto, auto, typename> typename QP> template<template<auto, auto, typename> typename QP>
concept invalid_unit_conversion = requires { concept invalid_unit_conversion = requires {
requires !requires { QP<isq::height[m], mean_sea_level, int>(2000 * m)[km]; }; // truncating conversion requires !requires { QP<isq::height[m], mean_sea_level, int>(2000 * m).in(km); }; // truncating conversion
requires !requires { QP<isq::height[m], mean_sea_level, int>(2 * m)[s]; }; // invalid unit requires !requires { QP<isq::height[m], mean_sea_level, int>(2 * m).in(s); }; // invalid unit
}; };
static_assert(invalid_unit_conversion<quantity_point>); static_assert(invalid_unit_conversion<quantity_point>);
#endif #endif

View File

@ -198,17 +198,17 @@ static_assert(quantity<isq::length[km]>(1500 * m).number() == 1.5);
// converting to a different unit // converting to a different unit
/////////////////////////////////// ///////////////////////////////////
static_assert(quantity<isq::length[km]>(2. * km)[km].number() == 2.); static_assert(quantity<isq::length[km]>(2. * km).in(km).number() == 2.);
static_assert(quantity<isq::length[km]>(2. * km)[m].number() == 2000.); static_assert(quantity<isq::length[km]>(2. * km).in(m).number() == 2000.);
static_assert(quantity<isq::length[m]>(2000. * m)[km].number() == 2.); static_assert(quantity<isq::length[m]>(2000. * m).in(km).number() == 2.);
static_assert(quantity<isq::length[km], int>(2 * km)[km].number() == 2); static_assert(quantity<isq::length[km], int>(2 * km).in(km).number() == 2);
static_assert(quantity<isq::length[km], int>(2 * km)[m].number() == 2000); static_assert(quantity<isq::length[km], int>(2 * km).in(m).number() == 2000);
#if MP_UNITS_COMP_GCC != 10 || MP_UNITS_COMP_GCC_MINOR > 2 #if MP_UNITS_COMP_GCC != 10 || MP_UNITS_COMP_GCC_MINOR > 2
template<template<auto, typename> typename Q> template<template<auto, typename> typename Q>
concept invalid_unit_conversion = requires { concept invalid_unit_conversion = requires {
requires !requires { Q<isq::length[m], int>(2000 * m)[km]; }; // truncating conversion requires !requires { Q<isq::length[m], int>(2000 * m).in(km); }; // truncating conversion
requires !requires { Q<isq::length[m], int>(2 * m)[s]; }; // invalid unit requires !requires { Q<isq::length[m], int>(2 * m).in(s); }; // invalid unit
}; };
static_assert(invalid_unit_conversion<quantity>); static_assert(invalid_unit_conversion<quantity>);
#endif #endif
@ -648,7 +648,7 @@ static_assert((7 * one % (2 * one)).number() == 1);
static_assert((10 * m2 * (10 * m2)) / (50 * m2) == 2 * m2); static_assert((10 * m2 * (10 * m2)) / (50 * m2) == 2 * m2);
static_assert((10 * km / (5 * m)).number() == 2); static_assert((10 * km / (5 * m)).number() == 2);
static_assert((10 * km / (5 * m))[one].number() == 2000); static_assert((10 * km / (5 * m)).in(one).number() == 2000);
static_assert((10 * s * (2 * kHz)).number() == 20); static_assert((10 * s * (2 * kHz)).number() == 20);
// commutativity and associativity // commutativity and associativity
@ -872,10 +872,10 @@ static_assert(!(123 * km >= 321'000 * m));
static_assert(is_of_type<10 * km / (5 * km), quantity<one, int>>); static_assert(is_of_type<10 * km / (5 * km), quantity<one, int>>);
static_assert((50. * m / (100. * m))[percent].number() == 50); static_assert((50. * m / (100. * m)).in(percent).number() == 50);
static_assert(50. * m / (100. * m) == 50 * percent); static_assert(50. * m / (100. * m) == 50 * percent);
static_assert(((50. * percent)[one]).number() == 0.5); static_assert((50. * percent).in(one).number() == 0.5);
////////////////// //////////////////