refactor: pow10 functions to pow.h

This commit is contained in:
Johel Ernesto Guerrero Peña
2020-08-31 23:19:38 -04:00
committed by Mateusz Pusz
parent 40fb1e1e8b
commit 2f5be094b2
3 changed files with 63 additions and 32 deletions

61
src/include/units/pow.h Normal file
View File

@@ -0,0 +1,61 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include <cstdint>
#include <cassert>
namespace units {
constexpr std::intmax_t ipow10(std::intmax_t exp)
{
assert(exp >= 0);
if (exp == 0) return 1;
std::intmax_t result = 1;
while (exp > 0) {
result *= 10;
--exp;
}
return result;
}
template<typename Rep>
constexpr Rep fpow10(std::intmax_t exp)
{
if (exp == 0) return Rep(1.0);
Rep result = Rep(1.0);
if (exp < 0) {
while (exp < 0) {
result = result / Rep(10.0);
++exp;
}
} else {
while (exp > 0) {
result = result * Rep(10.0);
--exp;
}
}
return result;
}
} // namespace units

View File

@@ -26,6 +26,7 @@
#include <units/bits/common_quantity.h>
#include <units/bits/dimension_op.h>
#include <units/bits/to_string.h>
#include <units/pow.h>
#include <units/quantity_cast.h>
#if COMP_MSVC || COMP_GCC >= 10

View File

@@ -26,6 +26,7 @@
#include <units/customization_points.h>
#include <units/bits/dimension_op.h>
#include <units/bits/external/type_traits.h>
#include <units/pow.h>
#include <units/quantity.h>
#include <units/quantity_point.h>
#include <cassert>
@@ -37,38 +38,6 @@
namespace units {
constexpr std::intmax_t ipow10(std::intmax_t exp)
{
assert(exp >= 0);
if (exp == 0) return 1;
std::intmax_t result = 1;
while (exp > 0) {
result *= 10;
--exp;
}
return result;
}
template<typename Rep>
constexpr Rep fpow10(std::intmax_t exp)
{
if (exp == 0) return Rep(1.0);
Rep result = Rep(1.0);
if (exp < 0) {
while (exp < 0) {
result = result / Rep(10.0);
++exp;
}
} else {
while (exp > 0) {
result = result * Rep(10.0);
--exp;
}
}
return result;
}
// QuantityOf
template<typename T, typename Dim>
concept QuantityOf = Quantity<T> && Dimension<Dim> && equivalent_dim<typename T::dimension, Dim>;