|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2018 Mateusz Pusz |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +#pragma once |
| 24 | + |
| 25 | +#include <mp-units/bits/module_macros.h> |
| 26 | +#include <mp-units/systems/si/prefixes.h> |
| 27 | + |
| 28 | +#ifndef MP_UNITS_IN_MODULE_INTERFACE |
| 29 | +#if MP_UNITS_HOSTED |
| 30 | +#ifdef MP_UNITS_IMPORT_STD |
| 31 | +import std; |
| 32 | +#else |
| 33 | +#include <cmath> |
| 34 | +#endif // MP_UNITS_IMPORT_STD |
| 35 | +#endif // MP_UNITS_HOSTED |
| 36 | +#endif // MP_UNITS_IN_MODULE_INTERFACE |
| 37 | + |
| 38 | +namespace mp_units::si { |
| 39 | + |
| 40 | +MP_UNITS_EXPORT_BEGIN |
| 41 | + |
| 42 | +#if MP_UNITS_HOSTED |
| 43 | + |
| 44 | +enum class prefix_range : std::uint8_t { engineering, full }; |
| 45 | + |
| 46 | +/** |
| 47 | + * @brief Calls an invocable with a quantity scaled to an automatically selected SI prefix |
| 48 | + * |
| 49 | + * Selects an SI prefix such that the integral part of the quantity's numerical value |
| 50 | + * has at least @c min_integral_digits digits. |
| 51 | + * |
| 52 | + * @tparam Q Quantity type |
| 53 | + * @tparam Func Invocable type that accepts a quantity |
| 54 | + * @tparam U PrefixableUnit type |
| 55 | + * |
| 56 | + * @param func Invocable to call with the scaled quantity |
| 57 | + * @param q Quantity to scale |
| 58 | + * @param u Unit to use as the base for prefix selection |
| 59 | + * @param range Prefix selection mode: |
| 60 | + * - @c prefix_range::engineering selects only powers of 1000 (kilo, mega, milli, etc.), |
| 61 | + * resulting in values in range [1.0, 1000) |
| 62 | + * - @c prefix_range::full selects all SI prefixes including deca, hecto, deci, centi, |
| 63 | + * resulting in values in range [1.0, 10.0) |
| 64 | + * @param min_integral_digits Minimum number of digits in the integral part (default: 1) |
| 65 | + * |
| 66 | + * @return The result of calling @c func with the scaled quantity |
| 67 | + * |
| 68 | + * @note For @c min_integral_digits=1: |
| 69 | + * - engineering mode: values displayed in range [1.0, 999.999...] |
| 70 | + * - full mode: values displayed in range [1.0, 9.999...] |
| 71 | + */ |
| 72 | +template<Quantity Q, std::invocable<Q> Func, PrefixableUnit U, auto Character = quantity_character::real_scalar> |
| 73 | + requires RepresentationOf<typename Q::rep, Character> && treat_as_floating_point<typename Q::rep> && |
| 74 | + requires(Q::rep v) { |
| 75 | + requires requires { abs(v); } || requires { std::abs(v); }; |
| 76 | + requires requires { log10(v); } || requires { std::log10(v); }; |
| 77 | + requires requires { floor(v); } || requires { std::floor(v); }; |
| 78 | + } |
| 79 | +constexpr decltype(auto) invoke_with_prefixed(Func func, Q q, U u, prefix_range range = prefix_range::engineering, |
| 80 | + int min_integral_digits = 1) |
| 81 | +{ |
| 82 | + if (q == 0) return func(q.in(u)); |
| 83 | + |
| 84 | + using std::abs, std::log10, std::floor; |
| 85 | + |
| 86 | + // Calculate the order of magnitude to determine appropriate prefix |
| 87 | + const auto value = q.numerical_value_in(u); |
| 88 | + const auto mag = static_cast<int>(floor(log10(abs(value)))); |
| 89 | + |
| 90 | + // Exponent ensures value has at least min_integral_digits in integral part |
| 91 | + // For min_integral_digits=1: select prefix giving value in [1, base) where base is 1000 or 10 |
| 92 | + const int exponent = mag - (min_integral_digits - 1); |
| 93 | + |
| 94 | + // Try each SI prefix from largest to smallest |
| 95 | + // clang-format off |
| 96 | + if (exponent >= 30) return func(q.in(si::quetta<U{}>)); |
| 97 | + else if (exponent >= 27) return func(q.in(si::ronna<U{}>)); |
| 98 | + else if (exponent >= 24) return func(q.in(si::yotta<U{}>)); |
| 99 | + else if (exponent >= 21) return func(q.in(si::zetta<U{}>)); |
| 100 | + else if (exponent >= 18) return func(q.in(si::exa<U{}>)); |
| 101 | + else if (exponent >= 15) return func(q.in(si::peta<U{}>)); |
| 102 | + else if (exponent >= 12) return func(q.in(si::tera<U{}>)); |
| 103 | + else if (exponent >= 9) return func(q.in(si::giga<U{}>)); |
| 104 | + else if (exponent >= 6) return func(q.in(si::mega<U{}>)); |
| 105 | + else if (exponent >= 3) return func(q.in(si::kilo<U{}>)); |
| 106 | + else if (exponent >= 2 && range == prefix_range::full) return func(q.in(si::hecto<U{}>)); |
| 107 | + else if (exponent >= 1 && range == prefix_range::full) return func(q.in(si::deca<U{}>)); |
| 108 | + else if (exponent >= 0) return func(q.in(U{})); |
| 109 | + else if (exponent >= -1 && range == prefix_range::full) return func(q.in(si::deci<U{}>)); |
| 110 | + else if (exponent >= -2 && range == prefix_range::full) return func(q.in(si::centi<U{}>)); |
| 111 | + else if (exponent >= -3) return func(q.in(si::milli<U{}>)); |
| 112 | + else if (exponent >= -6) return func(q.in(si::micro<U{}>)); |
| 113 | + else if (exponent >= -9) return func(q.in(si::nano<U{}>)); |
| 114 | + else if (exponent >= -12) return func(q.in(si::pico<U{}>)); |
| 115 | + else if (exponent >= -15) return func(q.in(si::femto<U{}>)); |
| 116 | + else if (exponent >= -18) return func(q.in(si::atto<U{}>)); |
| 117 | + else if (exponent >= -21) return func(q.in(si::zepto<U{}>)); |
| 118 | + else if (exponent >= -24) return func(q.in(si::yocto<U{}>)); |
| 119 | + else if (exponent >= -27) return func(q.in(si::ronto<U{}>)); |
| 120 | + else return func(q.in(si::quecto<U{}>)); |
| 121 | + // clang-format on |
| 122 | +} |
| 123 | + |
| 124 | +#endif // MP_UNITS_HOSTED |
| 125 | + |
| 126 | +MP_UNITS_EXPORT_END |
| 127 | + |
| 128 | +} // namespace mp_units::si |
0 commit comments