Skip to content

Commit 8a98bd8

Browse files
authored
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.
1 parent 39d259c commit 8a98bd8

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/FixedPoints/SFixedFreeFunctions.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ FIXED_POINTS_BEGIN_NAMESPACE
2020

2121
template< unsigned Integer, unsigned Fraction >
2222
constexpr SFixed<Integer * 2, Fraction * 2> multiply(const SFixed<Integer, Fraction> & left, const SFixed<Integer, Fraction> & right)
23-
{
23+
{
24+
static_assert(((Integer + 1) * 2 + Fraction * 2) <= FIXED_POINTS_DETAILS::BitSize<intmax_t>::Value, "Multiplication cannot be performed, the result type would be too large");
25+
2426
using ResultType = SFixed<Integer * 2, Fraction * 2>;
2527
using InternalType = typename ResultType::InternalType;
2628
return ResultType::fromInternal(static_cast<InternalType>(static_cast<InternalType>(left.getInternal()) * static_cast<InternalType>(right.getInternal())));
@@ -169,6 +171,8 @@ constexpr SFixed<Integer, Fraction> operator -(const SFixed<Integer, Fraction> &
169171
template< unsigned Integer, unsigned Fraction >
170172
constexpr SFixed<Integer, Fraction> operator *(const SFixed<Integer, Fraction> & left, const SFixed<Integer, Fraction> & right)
171173
{
174+
static_assert(((Integer + 1) * 2 + Fraction * 2) <= FIXED_POINTS_DETAILS::BitSize<intmax_t>::Value, "Multiplication cannot be performed, the intermediary type would be too large");
175+
172176
using InternalType = typename SFixed<Integer, Fraction>::InternalType;
173177
using PrecisionType = typename SFixed<Integer * 2, Fraction * 2>::InternalType;
174178
return SFixed<Integer, Fraction>::fromInternal(static_cast<InternalType>((static_cast<PrecisionType>(left.getInternal()) * static_cast<PrecisionType>(right.getInternal())) >> Fraction));

src/FixedPoints/UFixedFreeFunctions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ FIXED_POINTS_BEGIN_NAMESPACE
2121
template< unsigned Integer, unsigned Fraction >
2222
constexpr UFixed<Integer * 2, Fraction * 2> multiply(const UFixed<Integer, Fraction> & left, const UFixed<Integer, Fraction> & right)
2323
{
24+
static_assert((Integer * 2 + Fraction * 2) <= FIXED_POINTS_DETAILS::BitSize<uintmax_t>::Value, "Multiplication cannot be performed, the result type would be too large");
25+
2426
using ResultType = UFixed<Integer * 2, Fraction * 2>;
2527
using InternalType = typename ResultType::InternalType;
2628
return ResultType::fromInternal(static_cast<InternalType>(static_cast<InternalType>(left.getInternal()) * static_cast<InternalType>(right.getInternal())));
@@ -169,6 +171,8 @@ constexpr UFixed<Integer, Fraction> operator -(const UFixed<Integer, Fraction> &
169171
template< unsigned Integer, unsigned Fraction >
170172
constexpr UFixed<Integer, Fraction> operator *(const UFixed<Integer, Fraction> & left, const UFixed<Integer, Fraction> & right)
171173
{
174+
static_assert((Integer * 2 + Fraction * 2) <= FIXED_POINTS_DETAILS::BitSize<uintmax_t>::Value, "Multiplication cannot be performed, the intermediary type would be too large");
175+
172176
using InternalType = typename UFixed<Integer, Fraction>::InternalType;
173177
using PrecisionType = typename UFixed<Integer * 2, Fraction * 2>::InternalType;
174178
return UFixed<Integer, Fraction>::fromInternal(static_cast<InternalType>((static_cast<PrecisionType>(left.getInternal()) * static_cast<PrecisionType>(right.getInternal())) >> Fraction));

0 commit comments

Comments
 (0)