mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-02 03:44:27 +02:00
refactor!: pow functions as detail
This commit is contained in:
committed by
Mateusz Pusz
parent
2f5be094b2
commit
ca1867c9b2
@@ -25,7 +25,7 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
namespace units {
|
namespace units::detail {
|
||||||
|
|
||||||
constexpr std::intmax_t ipow10(std::intmax_t exp)
|
constexpr std::intmax_t ipow10(std::intmax_t exp)
|
||||||
{
|
{
|
||||||
@@ -58,4 +58,4 @@ constexpr Rep fpow10(std::intmax_t exp)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace units
|
} // namespace units::detail
|
@@ -25,8 +25,8 @@
|
|||||||
|
|
||||||
#include <units/bits/common_quantity.h>
|
#include <units/bits/common_quantity.h>
|
||||||
#include <units/bits/dimension_op.h>
|
#include <units/bits/dimension_op.h>
|
||||||
|
#include <units/bits/pow.h>
|
||||||
#include <units/bits/to_string.h>
|
#include <units/bits/to_string.h>
|
||||||
#include <units/pow.h>
|
|
||||||
#include <units/quantity_cast.h>
|
#include <units/quantity_cast.h>
|
||||||
|
|
||||||
#if COMP_MSVC || COMP_GCC >= 10
|
#if COMP_MSVC || COMP_GCC >= 10
|
||||||
@@ -343,9 +343,9 @@ template<typename D1, typename U1, typename Rep1, typename D2, typename U2, type
|
|||||||
using common_rep = decltype(lhs.count() * rhs.count());
|
using common_rep = decltype(lhs.count() * rhs.count());
|
||||||
const ratio r = U1::ratio * U2::ratio;
|
const ratio r = U1::ratio * U2::ratio;
|
||||||
if constexpr (treat_as_floating_point<common_rep>) {
|
if constexpr (treat_as_floating_point<common_rep>) {
|
||||||
return lhs.count() * rhs.count() * static_cast<common_rep>(r.num * fpow10<common_rep>(r.exp)) / static_cast<common_rep>(r.den);
|
return lhs.count() * rhs.count() * static_cast<common_rep>(r.num * detail::fpow10<common_rep>(r.exp)) / static_cast<common_rep>(r.den);
|
||||||
} else {
|
} else {
|
||||||
return lhs.count() * rhs.count() * static_cast<common_rep>(r.num * ipow10(r.exp)) / static_cast<common_rep>(r.den);
|
return lhs.count() * rhs.count() * static_cast<common_rep>(r.num * detail::ipow10(r.exp)) / static_cast<common_rep>(r.den);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -26,7 +26,7 @@
|
|||||||
#include <units/customization_points.h>
|
#include <units/customization_points.h>
|
||||||
#include <units/bits/dimension_op.h>
|
#include <units/bits/dimension_op.h>
|
||||||
#include <units/bits/external/type_traits.h>
|
#include <units/bits/external/type_traits.h>
|
||||||
#include <units/pow.h>
|
#include <units/bits/pow.h>
|
||||||
#include <units/quantity.h>
|
#include <units/quantity.h>
|
||||||
#include <units/quantity_point.h>
|
#include <units/quantity_point.h>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
@@ -63,13 +63,13 @@ struct quantity_cast_impl<To, CRatio, CRep, true, true, false> {
|
|||||||
static constexpr To cast(const Q& q)
|
static constexpr To cast(const Q& q)
|
||||||
{
|
{
|
||||||
if constexpr (treat_as_floating_point<CRep>) {
|
if constexpr (treat_as_floating_point<CRep>) {
|
||||||
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(fpow10<CRep>(CRatio.exp))));
|
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(detail::fpow10<CRep>(CRatio.exp))));
|
||||||
} else {
|
} else {
|
||||||
if constexpr (CRatio.exp > 0) {
|
if constexpr (CRatio.exp > 0) {
|
||||||
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(ipow10(CRatio.exp))));
|
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(detail::ipow10(CRatio.exp))));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) / static_cast<CRep>(ipow10(-CRatio.exp))));
|
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) / static_cast<CRep>(detail::ipow10(-CRatio.exp))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -93,21 +93,21 @@ struct quantity_cast_impl<To, CRatio, CRep, false, false, false> {
|
|||||||
{
|
{
|
||||||
if constexpr (treat_as_floating_point<CRep>) {
|
if constexpr (treat_as_floating_point<CRep>) {
|
||||||
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) *
|
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) *
|
||||||
static_cast<CRep>(fpow10<CRep>(CRatio.exp)) *
|
static_cast<CRep>(detail::fpow10<CRep>(CRatio.exp)) *
|
||||||
(static_cast<CRep>(CRatio.num) /
|
(static_cast<CRep>(CRatio.num) /
|
||||||
static_cast<CRep>(CRatio.den))));
|
static_cast<CRep>(CRatio.den))));
|
||||||
} else {
|
} else {
|
||||||
if constexpr (CRatio.exp > 0) {
|
if constexpr (CRatio.exp > 0) {
|
||||||
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) *
|
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) *
|
||||||
static_cast<CRep>(CRatio.num) *
|
static_cast<CRep>(CRatio.num) *
|
||||||
static_cast<CRep>(ipow10(CRatio.exp)) /
|
static_cast<CRep>(detail::ipow10(CRatio.exp)) /
|
||||||
static_cast<CRep>(CRatio.den)));
|
static_cast<CRep>(CRatio.den)));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) *
|
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) *
|
||||||
static_cast<CRep>(CRatio.num) /
|
static_cast<CRep>(CRatio.num) /
|
||||||
(static_cast<CRep>(CRatio.den) *
|
(static_cast<CRep>(CRatio.den) *
|
||||||
static_cast<CRep>(ipow10(-CRatio.exp)))));
|
static_cast<CRep>(detail::ipow10(-CRatio.exp)))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -128,13 +128,13 @@ struct quantity_cast_impl<To, CRatio, CRep, true, false, false> {
|
|||||||
static constexpr To cast(const Q& q)
|
static constexpr To cast(const Q& q)
|
||||||
{
|
{
|
||||||
if constexpr (treat_as_floating_point<CRep>) {
|
if constexpr (treat_as_floating_point<CRep>) {
|
||||||
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(fpow10<CRep>(CRatio.exp)) * (CRep{1} / static_cast<CRep>(CRatio.den))));
|
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(detail::fpow10<CRep>(CRatio.exp)) * (CRep{1} / static_cast<CRep>(CRatio.den))));
|
||||||
} else {
|
} else {
|
||||||
if constexpr (CRatio.exp > 0) {
|
if constexpr (CRatio.exp > 0) {
|
||||||
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(ipow10(CRatio.exp)) / static_cast<CRep>(CRatio.den)));
|
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(detail::ipow10(CRatio.exp)) / static_cast<CRep>(CRatio.den)));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) / (static_cast<CRep>(ipow10(-CRatio.exp)) * static_cast<CRep>(CRatio.den))));
|
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) / (static_cast<CRep>(detail::ipow10(-CRatio.exp)) * static_cast<CRep>(CRatio.den))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -155,13 +155,13 @@ struct quantity_cast_impl<To, CRatio, CRep, false, true, false> {
|
|||||||
static constexpr To cast(const Q& q)
|
static constexpr To cast(const Q& q)
|
||||||
{
|
{
|
||||||
if constexpr (treat_as_floating_point<CRep>) {
|
if constexpr (treat_as_floating_point<CRep>) {
|
||||||
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio.num) * static_cast<CRep>(fpow10<CRep>(CRatio.exp))));
|
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio.num) * static_cast<CRep>(detail::fpow10<CRep>(CRatio.exp))));
|
||||||
} else {
|
} else {
|
||||||
if constexpr (CRatio.exp > 0) {
|
if constexpr (CRatio.exp > 0) {
|
||||||
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio.num) * static_cast<CRep>(ipow10(CRatio.exp))));
|
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio.num) * static_cast<CRep>(detail::ipow10(CRatio.exp))));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio.num) / static_cast<CRep>(ipow10(-CRatio.exp))));
|
return To(static_cast<TYPENAME To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio.num) / static_cast<CRep>(detail::ipow10(-CRatio.exp))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -173,13 +173,13 @@ struct quantity_cast_impl<To, CRatio, CRep, true, true, false> {
|
|||||||
static constexpr To cast(const Q& q)
|
static constexpr To cast(const Q& q)
|
||||||
{
|
{
|
||||||
if constexpr (treat_as_floating_point<CRep>) {
|
if constexpr (treat_as_floating_point<CRep>) {
|
||||||
return To(static_cast<TYPENAME To::rep>(q.count() * fpow10<CRep>(CRatio.exp)));
|
return To(static_cast<TYPENAME To::rep>(q.count() * detail::fpow10<CRep>(CRatio.exp)));
|
||||||
} else {
|
} else {
|
||||||
if constexpr (CRatio.exp > 0) {
|
if constexpr (CRatio.exp > 0) {
|
||||||
return To(static_cast<TYPENAME To::rep>(q.count() * ipow10(CRatio.exp)));
|
return To(static_cast<TYPENAME To::rep>(q.count() * detail::ipow10(CRatio.exp)));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return To(static_cast<TYPENAME To::rep>(q.count() / ipow10(-CRatio.exp)));
|
return To(static_cast<TYPENAME To::rep>(q.count() / detail::ipow10(-CRatio.exp)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -200,13 +200,13 @@ struct quantity_cast_impl<To, CRatio, CRep, false, false, false> {
|
|||||||
static constexpr To cast(const Q& q)
|
static constexpr To cast(const Q& q)
|
||||||
{
|
{
|
||||||
if constexpr (treat_as_floating_point<CRep>) {
|
if constexpr (treat_as_floating_point<CRep>) {
|
||||||
return To(static_cast<TYPENAME To::rep>(q.count() * fpow10<CRep>(CRatio.exp) * (CRatio.num / CRatio.den)));
|
return To(static_cast<TYPENAME To::rep>(q.count() * detail::fpow10<CRep>(CRatio.exp) * (CRatio.num / CRatio.den)));
|
||||||
} else {
|
} else {
|
||||||
if constexpr (CRatio.exp > 0) {
|
if constexpr (CRatio.exp > 0) {
|
||||||
return To(static_cast<TYPENAME To::rep>(q.count() * CRatio.num * ipow10(CRatio.exp) / CRatio.den));
|
return To(static_cast<TYPENAME To::rep>(q.count() * CRatio.num * detail::ipow10(CRatio.exp) / CRatio.den));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return To(static_cast<TYPENAME To::rep>(q.count()) * CRatio.num / (CRatio.den * ipow10(-CRatio.exp)));
|
return To(static_cast<TYPENAME To::rep>(q.count()) * CRatio.num / (CRatio.den * detail::ipow10(-CRatio.exp)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -227,13 +227,13 @@ struct quantity_cast_impl<To, CRatio, CRep, true, false, false> {
|
|||||||
static constexpr To cast(const Q& q)
|
static constexpr To cast(const Q& q)
|
||||||
{
|
{
|
||||||
if constexpr (treat_as_floating_point<CRep>) {
|
if constexpr (treat_as_floating_point<CRep>) {
|
||||||
return To(static_cast<TYPENAME To::rep>(q.count() * fpow10<CRep>(CRatio.exp) / CRatio.den));
|
return To(static_cast<TYPENAME To::rep>(q.count() * detail::fpow10<CRep>(CRatio.exp) / CRatio.den));
|
||||||
} else {
|
} else {
|
||||||
if constexpr (CRatio.exp > 0) {
|
if constexpr (CRatio.exp > 0) {
|
||||||
return To(static_cast<TYPENAME To::rep>(q.count() * ipow10(CRatio.exp) / CRatio.den));
|
return To(static_cast<TYPENAME To::rep>(q.count() * detail::ipow10(CRatio.exp) / CRatio.den));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return To(static_cast<TYPENAME To::rep>(q.count() / (ipow10(-CRatio.exp) * CRatio.den)));
|
return To(static_cast<TYPENAME To::rep>(q.count() / (detail::ipow10(-CRatio.exp) * CRatio.den)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -254,13 +254,13 @@ struct quantity_cast_impl<To, CRatio, CRep, false, true, false> {
|
|||||||
static constexpr To cast(const Q& q)
|
static constexpr To cast(const Q& q)
|
||||||
{
|
{
|
||||||
if constexpr (treat_as_floating_point<CRep>) {
|
if constexpr (treat_as_floating_point<CRep>) {
|
||||||
return To(static_cast<TYPENAME To::rep>(q.count() * CRatio.num * fpow10<CRep>(CRatio.exp)));
|
return To(static_cast<TYPENAME To::rep>(q.count() * CRatio.num * detail::fpow10<CRep>(CRatio.exp)));
|
||||||
} else {
|
} else {
|
||||||
if constexpr (CRatio.exp > 0) {
|
if constexpr (CRatio.exp > 0) {
|
||||||
return To(static_cast<TYPENAME To::rep>(q.count() * CRatio.num * ipow10(CRatio.exp)));
|
return To(static_cast<TYPENAME To::rep>(q.count() * CRatio.num * detail::ipow10(CRatio.exp)));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return To(static_cast<TYPENAME To::rep>(q.count() * CRatio.num / ipow10(-CRatio.exp)));
|
return To(static_cast<TYPENAME To::rep>(q.count() * CRatio.num / detail::ipow10(-CRatio.exp)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user