File: test_logistic_sigmoid.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (91 lines) | stat: -rw-r--r-- 2,921 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright Matt Borland 2025.
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/math/special_functions/logistic_sigmoid.hpp>
#include <boost/multiprecision/cpp_bin_float.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include "math_unit_test.hpp"
#include <array>
#include <cfloat>
#include <cfenv>

#pragma STDC FENV_ACCESS ON

template <typename RealType>
void test()
{
    const std::array<RealType, 5> x_values = {
        0,
        1,
        1000,
        0.5,
        0.75
    };
    const std::array<RealType, 5> y_values = {
        static_cast<RealType>(1) / 2,
        static_cast<RealType>(0.73105857863000487925115924182183627436514464016505651927636590791904045307),
        static_cast<RealType>(1),
        static_cast<RealType>(0.62245933120185456463890056574550847875327936530891016305943716265854500),
        static_cast<RealType>(0.6791786991753929731596801157765790212342212482195760219829517436805)
    };

    for (std::size_t i = 0; i < x_values.size(); ++i)
    {
        const RealType test_value {boost::math::logistic_sigmoid(x_values[i])};
        BOOST_MATH_IF_CONSTEXPR (std::is_same<RealType, float>::value || std::is_same<RealType, double>::value)
        {
            CHECK_ULP_CLOSE(test_value, y_values[i], 1);
        }
        else
        {
            RealType comparison_value = y_values[i];
            CHECK_MOLLIFIED_CLOSE(test_value, comparison_value, static_cast<RealType>(1e-15));
        }

        bool fe {false};
        if (std::fetestexcept(FE_OVERFLOW))
        {
            fe = true;                                  // LCOV_EXCL_LINE
            std::cerr << "FE_OVERFLOW" << std::endl;    // LCOV_EXCL_LINE
        }
        if (std::fetestexcept(FE_UNDERFLOW))
        {
            fe = true;                                  // LCOV_EXCL_LINE
            std::cerr << "FE_UNDERFLOW" << std::endl;   // LCOV_EXCL_LINE
        }
        if (std::fetestexcept(FE_DIVBYZERO))
        {
            fe = true;                                  // LCOV_EXCL_LINE
            std::cerr << "FE_DIVBYZERO" << std::endl;   // LCOV_EXCL_LINE
        }
        if (std::fetestexcept(FE_INVALID))
        {
            fe = true;                                  // LCOV_EXCL_LINE
            std::cerr << "FE_INVALID" << std::endl;     // LCOV_EXCL_LINE
        }

        CHECK_EQUAL(fe, false);
    }
}

int main()
{
    std::feclearexcept(FE_ALL_EXCEPT);
    test<float>();

    std::feclearexcept(FE_ALL_EXCEPT);
    test<double>();

    std::feclearexcept(FE_ALL_EXCEPT);
    test<long double>();

    std::feclearexcept(FE_ALL_EXCEPT);
    test<boost::multiprecision::cpp_bin_float_quad>();

    test<boost::multiprecision::cpp_dec_float_50>();

    return boost::math::test::report_errors();
}