Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ test_libbitcoin_system_test_SOURCES = \
test/math/byteswap.cpp \
test/math/cast.cpp \
test/math/division.cpp \
test/math/expectations.cpp \
test/math/limits.cpp \
test/math/logarithm.cpp \
test/math/multiplication.cpp \
Expand Down
1 change: 1 addition & 0 deletions builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,7 @@ if (with-tests)
"../../test/math/byteswap.cpp"
"../../test/math/cast.cpp"
"../../test/math/division.cpp"
"../../test/math/expectations.cpp"
"../../test/math/limits.cpp"
"../../test/math/logarithm.cpp"
"../../test/math/multiplication.cpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@
</ClCompile>
<ClCompile Include="..\..\..\..\test\math\cast.cpp" />
<ClCompile Include="..\..\..\..\test\math\division.cpp" />
<ClCompile Include="..\..\..\..\test\math\expectations.cpp" />
<ClCompile Include="..\..\..\..\test\math\limits.cpp" />
<ClCompile Include="..\..\..\..\test\math\logarithm.cpp" />
<ClCompile Include="..\..\..\..\test\math\multiplication.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,9 @@
<ClCompile Include="..\..\..\..\test\math\division.cpp">
<Filter>src\math</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\math\expectations.cpp">
<Filter>src\math</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\math\limits.cpp">
<Filter>src\math</Filter>
</ClCompile>
Expand Down
2 changes: 2 additions & 0 deletions include/bitcoin/system/constraints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ using if_little_endian_integral_integer = bool_if<
is_integral_integer<Integer> &&
is_little_endian>;

/// Floating pont types (native, floating point, non-bool).

template <typename Integer>
using if_floating_point = bool_if<
is_floating_point<Integer>>;
Expand Down
54 changes: 47 additions & 7 deletions include/bitcoin/system/impl/math/cast.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -209,31 +209,71 @@ constexpr Unsigned to_unsigned(Unsigned value) NOEXCEPT

// Floating point casts.
// ----------------------------------------------------------------------------
// inlines below be not constant until c++23.

template <typename Integer, typename Float,
if_integral_integer<Integer>,
if_floating_point<Float>>
constexpr bool to_integer(Integer& out, Float value) NOEXCEPT
inline bool to_integer(Integer& out, Float value, bool whole) NOEXCEPT
{
if (!std::isfinite(value))
return false;

// TODO: Could use tolerance for fp epsilon to handle representation error.
Float integer{};
const Float fractional = std::modf(value, &integer);
if (fractional != 0.0)
if ((std::modf(value, &integer) != 0.0) && whole)
return false;

if (integer > static_cast<Float>(std::numeric_limits<Integer>::max()) ||
integer < static_cast<Float>(std::numeric_limits<Integer>::min()))
return false;

// Floating point conversion in c++ requires explicit or implicit cast.
BC_PUSH_WARNING(NO_CASTS_FOR_ARITHMETIC_CONVERSION)
out = static_cast<Integer>(integer);
BC_POP_WARNING()
out = to_integer<Integer>(integer);
return true;
}

template <typename Integer, typename Float,
if_integral_integer<Integer>,
if_floating_point<Float>>
inline Integer to_ceilinged_integer(Float value) NOEXCEPT
{
return to_truncated_integer<Integer>(std::ceil(value));
}

template <typename Integer, typename Float,
if_integral_integer<Integer>,
if_floating_point<Float>>
inline Integer to_floored_integer(Float value) NOEXCEPT
{
return to_truncated_integer<Integer>(std::floor(value));
}

template <typename Integer, typename Float,
if_integral_integer<Integer>,
if_floating_point<Float>>
inline Integer to_rounded_integer(Float value) NOEXCEPT
{
return to_truncated_integer<Integer>(std::round(value));
}

template <typename Integer, typename Float,
if_integral_integer<Integer>,
if_floating_point<Float>>
inline Integer to_truncated_integer(Float value) NOEXCEPT
{
if (!std::isfinite(value))
return std::numeric_limits<Integer>::max();

const auto integer = std::trunc(value);
if (integer > static_cast<Float>(std::numeric_limits<Integer>::max()))
return std::numeric_limits<Integer>::max();

if (integer < static_cast<Float>(std::numeric_limits<Integer>::min()))
return std::numeric_limits<Integer>::min();

return to_integer<Integer>(integer);
}

template <typename Integer, typename Float,
if_integral_integer<Integer>,
if_floating_point<Float>>
Expand Down
42 changes: 38 additions & 4 deletions include/bitcoin/system/math/cast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,53 @@ constexpr Unsigned to_unsigned(Unsigned value) NOEXCEPT;
/// Floating point casts.
/// ---------------------------------------------------------------------------

/// Cast floating point to integral integer, overflow guarded.
/// Cast floating point to integral integer (truncated).
/// True for any finite value that falls within Integer domain.
/// False if fractional part is non-zero when whole is set true.
template <typename Integer = size_t, typename Float,
if_integral_integer<Integer> = true,
if_floating_point<Float> = true>
constexpr bool to_integer(Integer& out, Float value) NOEXCEPT;
inline bool to_integer(Integer& out, Float value, bool whole=true) NOEXCEPT;

/// Cast floating point to integral integer, overflow unguarded.
/// Cast floating point to integral integer (ceilinged).
/// Any non-finite value returns the integer domain maximum.
/// Overflow/underflow is clamped to respective integer upper/lower limit.
template <typename Integer = size_t, typename Float,
if_integral_integer<Integer> = true,
if_floating_point<Float> = true>
inline Integer to_ceilinged_integer(Float value) NOEXCEPT;

/// Cast floating point to integral integer (floored).
/// Any non-finite value returns the integer domain maximum.
/// Overflow/underflow is clamped to respective integer upper/lower limit.
template <typename Integer = size_t, typename Float,
if_integral_integer<Integer> = true,
if_floating_point<Float> = true>
inline Integer to_floored_integer(Float value) NOEXCEPT;

/// Cast floating point to integral integer (rounded).
/// Any non-finite value returns the integer domain maximum.
/// Overflow/underflow is clamped to respective integer upper/lower limit.
template <typename Integer = size_t, typename Float,
if_integral_integer<Integer> = true,
if_floating_point<Float> = true>
inline Integer to_rounded_integer(Float value) NOEXCEPT;

/// Cast floating point to integral integer (truncated).
/// Any non-finite value returns the integer domain maximum.
/// Overflow/underflow is clamped to respective integer upper/lower limit.
template <typename Integer = size_t, typename Float,
if_integral_integer<Integer> = true,
if_floating_point<Float> = true>
inline Integer to_truncated_integer(Float value) NOEXCEPT;

/// Cast floating point to integral integer, overflow/underflow UNGUARDED.
template <typename Integer = size_t, typename Float,
if_integral_integer<Integer> = true,
if_floating_point<Float> = true>
constexpr Integer to_integer(Float value) NOEXCEPT;

/// Cast integral integer to floating point, overflow unguarded.
/// Cast integral integer to floating point, overflow/underflow not possible.
template <typename Float = double, typename Integer,
if_floating_point<Float> = true,
if_integral_integer<Integer> = true>
Expand Down
2 changes: 1 addition & 1 deletion test/math/addition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
#include "../test.hpp"

// creates addition_tests namespace
// creates addition_tests namespace
BOOST_AUTO_TEST_SUITE(addition_tests)

constexpr uint32_t pos_uint32 = 42;
Expand Down
Loading
Loading