File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1919#ifndef LIBBITCOIN_SYSTEM_MATH_CAST_IPP
2020#define LIBBITCOIN_SYSTEM_MATH_CAST_IPP
2121
22+ #include < cmath>
2223#include < bitcoin/system/define.hpp>
2324
2425namespace libbitcoin {
@@ -209,6 +210,30 @@ constexpr Unsigned to_unsigned(Unsigned value) NOEXCEPT
209210// Floating point casts.
210211// ----------------------------------------------------------------------------
211212
213+ template <typename Integer, typename Float,
214+ if_integral_integer<Integer>,
215+ if_floating_point<Float>>
216+ constexpr bool to_integer (Integer& out, Float value) NOEXCEPT
217+ {
218+ if (!std::isfinite (value))
219+ return false ;
220+
221+ Float integer{};
222+ const Float fractional = std::modf (value, &integer);
223+ if (fractional != 0.0 )
224+ return false ;
225+
226+ if (integer > static_cast <Float>(std::numeric_limits<Integer>::max ()) ||
227+ integer < static_cast <Float>(std::numeric_limits<Integer>::min ()))
228+ return false ;
229+
230+ // Floating point conversion in c++ requires explicit or implicit cast.
231+ BC_PUSH_WARNING (NO_CASTS_FOR_ARITHMETIC_CONVERSION)
232+ out = static_cast <Integer>(integer);
233+ BC_POP_WARNING ()
234+ return true ;
235+ }
236+
212237template <typename Integer, typename Float,
213238 if_integral_integer<Integer>,
214239 if_floating_point<Float>>
Original file line number Diff line number Diff line change @@ -142,6 +142,12 @@ constexpr Unsigned to_unsigned(Unsigned value) NOEXCEPT;
142142// / Floating point casts.
143143// / ---------------------------------------------------------------------------
144144
145+ // / Cast floating point to integral integer, overflow guarded.
146+ template <typename Integer = size_t , typename Float,
147+ if_integral_integer<Integer> = true ,
148+ if_floating_point<Float> = true >
149+ constexpr bool to_integer (Integer& out, Float value) NOEXCEPT;
150+
145151// / Cast floating point to integral integer, overflow unguarded.
146152template <typename Integer = size_t , typename Float,
147153 if_integral_integer<Integer> = true ,
You can’t perform that action at this time.
0 commit comments