Skip to content

Commit 488cd2f

Browse files
committed
Action review comments replacing fenv manipulation
1 parent 6068d7a commit 488cd2f

5 files changed

Lines changed: 36 additions & 27 deletions

File tree

include/boost/math/distributions/logistic.hpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,6 @@ namespace boost { namespace math {
148148
}
149149
BOOST_MATH_STD_USING
150150
RealType power = (location - x) / scale;
151-
if(power > tools::log_max_value<RealType>())
152-
return 0;
153-
if(power < -tools::log_max_value<RealType>())
154-
return 1;
155151
return logistic_sigmoid(-power, Policy());
156152
}
157153

@@ -255,10 +251,6 @@ namespace boost { namespace math {
255251
return result;
256252
}
257253
RealType power = (x - location) / scale;
258-
if(power > tools::log_max_value<RealType>())
259-
return 0;
260-
if(power < -tools::log_max_value<RealType>())
261-
return 1;
262254
return logistic_sigmoid(-power, Policy());
263255
}
264256

include/boost/math/special_functions/logistic_sigmoid.hpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#define BOOST_MATH_SF_EXPIT_HPP
99

1010
#include <boost/math/policies/policy.hpp>
11+
#include <boost/math/tools/precision.hpp>
1112
#include <cmath>
12-
#include <cfenv>
1313

1414
namespace boost {
1515
namespace math {
@@ -21,17 +21,16 @@ RealType logistic_sigmoid(RealType x, const Policy&)
2121

2222
using promoted_real_type = typename policies::evaluation<RealType, Policy>::type;
2323

24-
#ifndef BOOST_MATH_HAS_GPU_SUPPORT
25-
std::fexcept_t flags;
26-
std::fegetexceptflag(&flags, FE_ALL_EXCEPT);
27-
#endif
24+
if(-x >= tools::log_max_value<RealType>())
25+
{
26+
return 0;
27+
}
28+
if(-x <= -tools::log_max_value<RealType>())
29+
{
30+
return 1;
31+
}
2832

2933
const auto res {static_cast<RealType>(1 / (1 + exp(static_cast<promoted_real_type>(-x))))};
30-
31-
#ifndef BOOST_MATH_HAS_GPU_SUPPORT
32-
std::fesetexceptflag(&flags, FE_ALL_EXCEPT);
33-
#endif
34-
3534
return res;
3635
}
3736

include/boost/math/special_functions/logit.hpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <boost/math/tools/config.hpp>
1111
#include <boost/math/policies/policy.hpp>
12+
#include <boost/math/policies/error_handling.hpp>
1213
#include <cmath>
1314
#include <cfenv>
1415

@@ -23,10 +24,10 @@ RealType logit(RealType p, const Policy&)
2324

2425
using promoted_real_type = typename policies::evaluation<RealType, Policy>::type;
2526

26-
#ifndef BOOST_MATH_HAS_GPU_SUPPORT
27-
std::fexcept_t flags;
28-
std::fegetexceptflag(&flags, FE_ALL_EXCEPT);
29-
#endif
27+
if (p < tools::min_value<RealType>())
28+
{
29+
return -policies::raise_overflow_error<RealType>("logit", "sub-normals will overflow ln(x/(1-x))", Policy());
30+
}
3031

3132
static const RealType crossover {RealType{1}/4};
3233
const auto promoted_p {static_cast<promoted_real_type>(p)};
@@ -40,10 +41,6 @@ RealType logit(RealType p, const Policy&)
4041
result = static_cast<RealType>(log(promoted_p / (1 - promoted_p)));
4142
}
4243

43-
#ifndef BOOST_MATH_HAS_GPU_SUPPORT
44-
std::fesetexceptflag(&flags, FE_ALL_EXCEPT);
45-
#endif
46-
4744
return result;
4845
}
4946

test/test_logistic_sigmoid.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "math_unit_test.hpp"
1010
#include <array>
1111
#include <cfloat>
12+
#include <cfenv>
1213

1314
#pragma STDC FENV_ACCESS ON
1415

test/test_logit.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#include <boost/multiprecision/cpp_bin_float.hpp>
99
#include "math_unit_test.hpp"
1010
#include <array>
11-
#include <cfloat>
11+
#include <limits>
12+
#include <cfenv>
1213

1314
#pragma STDC FENV_ACCESS ON
1415

@@ -67,6 +68,25 @@ void test()
6768

6869
CHECK_EQUAL(fe, false);
6970
}
71+
72+
#if defined(_CPPUNWIND) || defined(__EXCEPTIONS)
73+
74+
BOOST_MATH_IF_CONSTEXPR (std::is_arithmetic<RealType>::value)
75+
{
76+
bool thrown {false};
77+
try
78+
{
79+
boost::math::logit(std::numeric_limits<RealType>::denorm_min());
80+
}
81+
catch (...)
82+
{
83+
thrown = true;
84+
}
85+
86+
CHECK_EQUAL(thrown, true);
87+
}
88+
89+
#endif // Exceptional environments
7090
}
7191

7292
int main()

0 commit comments

Comments
 (0)