diff --git a/include/boost/math/differentiation/autodiff_reverse.hpp b/include/boost/math/differentiation/autodiff_reverse.hpp index a47aa43cf..7061a379b 100644 --- a/include/boost/math/differentiation/autodiff_reverse.hpp +++ b/include/boost/math/differentiation/autodiff_reverse.hpp @@ -411,7 +411,7 @@ private: void make_rvar_from_expr(const expression &expr) { make_multi_node>(); - expr.template propagatex<0>(node_, inner_t(1.0)); + expr.template propagatex<0>(node_, inner_t(static_cast(1.0))); } RealType get_item_impl(std::true_type) const { @@ -429,7 +429,7 @@ public: make_leaf_node(); } rvar(const RealType value) - : value_(inner_t{value}) + : value_(inner_t{static_cast(value)}) { make_leaf_node(); } @@ -458,6 +458,16 @@ public: value_ = expr.evaluate(); make_rvar_from_expr(expr); } + + template::value + && !std::is_same::value>::type> + rvar(T v) + : value_(inner_t{static_cast(v)}) + { + make_leaf_node(); + } + template rvar &operator=(const expression &expr) { @@ -560,7 +570,7 @@ public: gradient_tape &tape = get_active_tape(); auto it = tape.find(node_); - it->update_adjoint_v(inner_t(1.0)); + it->update_adjoint_v(inner_t(static_cast(1.0))); while (it != tape.begin()) { it->backward(); --it; diff --git a/include/boost/math/differentiation/detail/reverse_mode_autodiff_basic_operator_expressions.hpp b/include/boost/math/differentiation/detail/reverse_mode_autodiff_basic_operator_expressions.hpp index 27a3fdd7d..904401aed 100644 --- a/include/boost/math/differentiation/detail/reverse_mode_autodiff_basic_operator_expressions.hpp +++ b/include/boost/math/differentiation/detail/reverse_mode_autodiff_basic_operator_expressions.hpp @@ -39,13 +39,13 @@ struct add_expr : public abstract_binary_expression(1.0)); } static const inner_t right_derivative(const inner_t & /*l*/, const inner_t & /*r*/, const inner_t & /*v*/) { - return inner_t(1.0); + return inner_t(static_cast(1.0)); } }; template @@ -70,7 +70,7 @@ struct add_const_expr const inner_t & /*v*/, const RealType & /*constant*/) { - return inner_t(1.0); + return inner_t(static_cast(1.0)); } }; /****************************************************************************************************************/ @@ -164,13 +164,13 @@ struct sub_expr : public abstract_binary_expression(1.0)); } static const inner_t right_derivative(const inner_t & /*l*/, const inner_t & /*r*/, const inner_t & /*v*/) { - return inner_t(-1.0); + return inner_t(static_cast(-1.0)); } }; diff --git a/include/boost/math/differentiation/detail/reverse_mode_autodiff_stl_expressions.hpp b/include/boost/math/differentiation/detail/reverse_mode_autodiff_stl_expressions.hpp index 72d7fe453..29819867f 100644 --- a/include/boost/math/differentiation/detail/reverse_mode_autodiff_stl_expressions.hpp +++ b/include/boost/math/differentiation/detail/reverse_mode_autodiff_stl_expressions.hpp @@ -50,7 +50,8 @@ struct fabs_expr : public abstract_unary_expression 0.0 ? inner_t{1.0} : inner_t{-1.0}; + return argv > 0.0 ? inner_t{static_cast(1.0)} + : inner_t{static_cast(-1.0)}; } }; diff --git a/test/test_autodiff_reverse.hpp b/test/test_autodiff_reverse.hpp index 04c3566d9..b5671003e 100644 --- a/test/test_autodiff_reverse.hpp +++ b/test/test_autodiff_reverse.hpp @@ -10,6 +10,11 @@ #define BOOST_MP_NO_QUAD #define BOOST_MATH_DISABLE_FLOAT128 #include +#include // always safe + +#if __has_include() +#include +#endif #include #include diff --git a/test/test_reverse_mode_autodiff_basic_math_ops.cpp b/test/test_reverse_mode_autodiff_basic_math_ops.cpp index c2cc70003..b8e476c9a 100644 --- a/test/test_reverse_mode_autodiff_basic_math_ops.cpp +++ b/test/test_reverse_mode_autodiff_basic_math_ops.cpp @@ -33,8 +33,8 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(addition, T, all_float_types) gradient_tape& tape = get_active_tape(); tape.zero_grad(); test_rvar_p_rvar.backward(); - BOOST_REQUIRE_EQUAL(x1.adjoint(), 1.0); - BOOST_REQUIRE_EQUAL(x2.adjoint(), 1.0); + BOOST_REQUIRE_EQUAL(x1.adjoint(), T(1.0)); + BOOST_REQUIRE_EQUAL(x2.adjoint(), T(1.0)); tape.zero_grad(); rvar z = x1 + x1 + x1 + x1 + x1; @@ -44,7 +44,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(addition, T, all_float_types) BOOST_AUTO_TEST_CASE_TEMPLATE(multiplication, T, all_float_types) { - RandomSample rng{-100, 100}; + RandomSample rng{T(-100), T(100)}; T x1_v = rng.next(); T x2_v = rng.next(); T test_rvar_p_rvar_v = x1_v * x2_v; @@ -70,7 +70,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(multiplication, T, all_float_types) auto z = x1 * x1 * x1 * x1 * x1; rvar zz(z); zz.backward(); - BOOST_REQUIRE_CLOSE(x1.adjoint(), 5 * x1_v * x1_v * x1_v * x1_v, boost_close_tol()); + BOOST_REQUIRE_CLOSE(x1.adjoint(), T(5) * x1_v * x1_v * x1_v * x1_v, boost_close_tol()); tape.clear(); } @@ -278,8 +278,8 @@ std::vector>>> df_4_a(T x, T y) BOOST_AUTO_TEST_CASE_TEMPLATE(first_derivative, T, all_float_types) { //RandomSample rng{-1, 1}; - T x = 1.1224; //rng.next(); - T y = 5.213414; //rng.next(); + T x(1.1224); //rng.next(); + T y(5.213414); //rng.next(); rvar x_ad = x; rvar y_ad = y; diff --git a/test/test_reverse_mode_autodiff_constructors.cpp b/test/test_reverse_mode_autodiff_constructors.cpp index d62b12a4f..b1704d4a0 100644 --- a/test/test_reverse_mode_autodiff_constructors.cpp +++ b/test/test_reverse_mode_autodiff_constructors.cpp @@ -9,7 +9,7 @@ BOOST_AUTO_TEST_SUITE(explicit_rvar_constructors) BOOST_AUTO_TEST_CASE_TEMPLATE(rvar_constructors_and_utils, T, all_float_types) { - RandomSample rng{-100, 100}; + RandomSample rng{T(-100), T(100)}; /* raw constructors */ T x_value = rng.next(); rdiff::rvar x0(x_value); @@ -29,7 +29,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(rvar_constructors_and_utils, T, all_float_types) BOOST_AUTO_TEST_CASE_TEMPLATE(make_rvar_constructors, T, all_float_types) { - RandomSample rng{-100, 100}; + RandomSample rng{T(-100), T(100)}; T x_value = rng.next(); rdiff::rvar x1 = rdiff::make_rvar(x_value); @@ -226,9 +226,9 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(inplace_multiplication, T, all_float_types) BOOST_AUTO_TEST_CASE_TEMPLATE(inplace_division, T, all_float_types) { using namespace rdiff; - RandomSample rng{-1, 1}; + RandomSample rng{T(-1), T(1)}; T x1_v = rng.next(); - T x2_v = rng.next() + 1e-2; + T x2_v = rng.next() + T(1e-2); T expected = x1_v / x2_v; @@ -255,8 +255,8 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(inplace_division, T, all_float_types) BOOST_AUTO_TEST_CASE_TEMPLATE(test_rvar_ostream_output, T, all_float_types) { using namespace rdiff; - rvar x = T{2.0}; - rvar y = T{3.0}; + rvar x = static_cast(2.0); + rvar y = static_cast(3.0); rvar z = x * y; z.backward();