From 8a98bd862941d1395e2193c731ac8db716241015 Mon Sep 17 00:00:00 2001 From: Pharap <2933055+Pharap@users.noreply.github.com> Date: Tue, 20 Mar 2018 15:35:49 +0000 Subject: [PATCH] Add descriptive error for multiplication (#26) Provides a better error message for when multiplication can't be used because the required precision type is too large. --- src/FixedPoints/SFixedFreeFunctions.h | 6 +++++- src/FixedPoints/UFixedFreeFunctions.h | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/FixedPoints/SFixedFreeFunctions.h b/src/FixedPoints/SFixedFreeFunctions.h index e6df4bd..b116d65 100644 --- a/src/FixedPoints/SFixedFreeFunctions.h +++ b/src/FixedPoints/SFixedFreeFunctions.h @@ -20,7 +20,9 @@ FIXED_POINTS_BEGIN_NAMESPACE template< unsigned Integer, unsigned Fraction > constexpr SFixed multiply(const SFixed & left, const SFixed & right) -{ +{ + static_assert(((Integer + 1) * 2 + Fraction * 2) <= FIXED_POINTS_DETAILS::BitSize::Value, "Multiplication cannot be performed, the result type would be too large"); + using ResultType = SFixed; using InternalType = typename ResultType::InternalType; return ResultType::fromInternal(static_cast(static_cast(left.getInternal()) * static_cast(right.getInternal()))); @@ -169,6 +171,8 @@ constexpr SFixed operator -(const SFixed & template< unsigned Integer, unsigned Fraction > constexpr SFixed operator *(const SFixed & left, const SFixed & right) { + static_assert(((Integer + 1) * 2 + Fraction * 2) <= FIXED_POINTS_DETAILS::BitSize::Value, "Multiplication cannot be performed, the intermediary type would be too large"); + using InternalType = typename SFixed::InternalType; using PrecisionType = typename SFixed::InternalType; return SFixed::fromInternal(static_cast((static_cast(left.getInternal()) * static_cast(right.getInternal())) >> Fraction)); diff --git a/src/FixedPoints/UFixedFreeFunctions.h b/src/FixedPoints/UFixedFreeFunctions.h index 0d2fb19..0c9e452 100644 --- a/src/FixedPoints/UFixedFreeFunctions.h +++ b/src/FixedPoints/UFixedFreeFunctions.h @@ -21,6 +21,8 @@ FIXED_POINTS_BEGIN_NAMESPACE template< unsigned Integer, unsigned Fraction > constexpr UFixed multiply(const UFixed & left, const UFixed & right) { + static_assert((Integer * 2 + Fraction * 2) <= FIXED_POINTS_DETAILS::BitSize::Value, "Multiplication cannot be performed, the result type would be too large"); + using ResultType = UFixed; using InternalType = typename ResultType::InternalType; return ResultType::fromInternal(static_cast(static_cast(left.getInternal()) * static_cast(right.getInternal()))); @@ -169,6 +171,8 @@ constexpr UFixed operator -(const UFixed & template< unsigned Integer, unsigned Fraction > constexpr UFixed operator *(const UFixed & left, const UFixed & right) { + static_assert((Integer * 2 + Fraction * 2) <= FIXED_POINTS_DETAILS::BitSize::Value, "Multiplication cannot be performed, the intermediary type would be too large"); + using InternalType = typename UFixed::InternalType; using PrecisionType = typename UFixed::InternalType; return UFixed::fromInternal(static_cast((static_cast(left.getInternal()) * static_cast(right.getInternal())) >> Fraction));