File: git_issue_430.cpp

package info (click to toggle)
scipy 1.16.0-1exp7
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 234,820 kB
  • sloc: cpp: 503,145; python: 344,611; ansic: 195,638; javascript: 89,566; fortran: 56,210; cs: 3,081; f90: 1,150; sh: 848; makefile: 785; pascal: 284; csh: 135; lisp: 134; xml: 56; perl: 51
file content (197 lines) | stat: -rw-r--r-- 7,111 bytes parent folder | download | duplicates (9)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright Matt Borland 2023
// Copyright John Maddock 2023
// 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)
//
// See: https://godbolt.org/z/Ev4ManrsW

#include <boost/math/special_functions/round.hpp>
#include <boost/math/special_functions/trunc.hpp>
#include <boost/math/special_functions/next.hpp>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <cstdint>
#include <cmath>

template <typename Real>
void test_llround_near_boundary()
{
   using std::ldexp;
   Real boundary = ldexp(static_cast<Real>(1), std::numeric_limits<long long>::digits);

   Real value;
   int i;

    for (value = boundary, i = 0; i < 100; value = boost::math::float_next(value), ++i)
    {
        BOOST_CHECK_THROW(boost::math::llround(value), boost::math::rounding_error);
    }
    for (value = boost::math::float_prior(boundary), i = 0; i < 1000; value = boost::math::float_prior(value), ++i)
    {
        BOOST_CHECK_EQUAL(static_cast<Real>(boost::math::llround(value)), boost::math::round(value));
    }
    for (value = boost::math::float_prior(-boundary), i = 0; i < 100; value = boost::math::float_prior(value), ++i)
    {
        BOOST_CHECK_THROW(boost::math::llround(value), boost::math::rounding_error);
    }
    for (value = -boundary, i = 0; i < 1000; value = boost::math::float_next(value), ++i)
    {
        BOOST_CHECK_EQUAL(static_cast<Real>(boost::math::llround(value)), boost::math::round(value));
    }
}

template <typename Real>
void test_lround_near_boundary()
{
   using std::ldexp;
   Real boundary = ldexp(static_cast<Real>(1), std::numeric_limits<long>::digits);

   Real value;
   int i;

    for (value = boundary, i = 0; i < 100; value = boost::math::float_next(value), ++i)
    {
        BOOST_CHECK_THROW(boost::math::lround(value), boost::math::rounding_error);
    }
    for (value = boost::math::float_prior(boundary), i = 0; i < 1000; value = boost::math::float_prior(value), ++i)
    {
        BOOST_CHECK_EQUAL(static_cast<Real>(boost::math::lround(value)), boost::math::round(value));
    }
    for (value = boost::math::float_prior(-boundary), i = 0; i < 100; value = boost::math::float_prior(value), ++i)
    {
        BOOST_CHECK_THROW(boost::math::lround(value), boost::math::rounding_error);
    }
    for (value = -boundary, i = 0; i < 1000; value = boost::math::float_next(value), ++i)
    {
        BOOST_CHECK_EQUAL(static_cast<Real>(boost::math::lround(value)), boost::math::round(value));
    }
}

template <typename Real>
void test_iround_near_boundary()
{
   using std::ldexp;
   Real boundary = ldexp(static_cast<Real>(1), std::numeric_limits<int>::digits);

   Real value;
   int i;

    for (value = boundary, i = 0; i < 100; value = boost::math::float_next(value), ++i)
    {
        BOOST_CHECK_THROW(boost::math::iround(value), boost::math::rounding_error);
    }
    for (value = boost::math::float_prior(boundary), i = 0; i < 1000; value = boost::math::float_prior(value), ++i)
    {
        BOOST_CHECK_EQUAL(static_cast<Real>(boost::math::iround(value)), boost::math::round(value));
    }
    for (value = boost::math::float_prior(-boundary), i = 0; i < 100; value = boost::math::float_prior(value), ++i)
    {
        BOOST_CHECK_THROW(boost::math::iround(value), boost::math::rounding_error);
    }
    for (value = -boundary, i = 0; i < 1000; value = boost::math::float_next(value), ++i)
    {
        BOOST_CHECK_EQUAL(static_cast<Real>(boost::math::iround(value)), boost::math::round(value));
    }  
}

template <typename Real>
void test_lltrunc_near_boundary()
{
   using std::ldexp;
   Real boundary = ldexp(static_cast<Real>(1), std::numeric_limits<long long>::digits);

   Real value;
   int i;    

    for (value = boundary, i = 0; i < 100; value = boost::math::float_next(value), ++i)
    {
        BOOST_CHECK_THROW(boost::math::lltrunc(value), boost::math::rounding_error);
    }
    for (value = boost::math::float_prior(boundary), i = 0; i < 1000; value = boost::math::float_prior(value), ++i)
    {
        BOOST_CHECK_EQUAL(static_cast<Real>(boost::math::lltrunc(value)), boost::math::lltrunc(value));
    }
    for (value = boost::math::float_prior(-boundary), i = 0; i < 100; value = boost::math::float_prior(value), ++i)
    {
        BOOST_CHECK_THROW(boost::math::lltrunc(value), boost::math::rounding_error);
    }
    for (value = -boundary, i = 0; i < 1000; value = boost::math::float_next(value), ++i)
    {
        BOOST_CHECK_EQUAL(static_cast<Real>(boost::math::lltrunc(value)), boost::math::lltrunc(value));
    } 
}

template <typename Real>
void test_ltrunc_near_boundary()
{
   using std::ldexp;
   Real boundary = ldexp(static_cast<Real>(1), std::numeric_limits<long>::digits);

   Real value;
   int i;    

    for (value = boundary, i = 0; i < 100; value = boost::math::float_next(value), ++i)
    {
        BOOST_CHECK_THROW(boost::math::ltrunc(value), boost::math::rounding_error);
    }
    for (value = boost::math::float_prior(boundary), i = 0; i < 1000; value = boost::math::float_prior(value), ++i)
    {
        BOOST_CHECK_EQUAL(static_cast<Real>(boost::math::ltrunc(value)), boost::math::ltrunc(value));
    }
    for (value = boost::math::float_prior(-boundary), i = 0; i < 100; value = boost::math::float_prior(value), ++i)
    {
        BOOST_CHECK_THROW(boost::math::ltrunc(value), boost::math::rounding_error);
    }
    for (value = -boundary, i = 0; i < 1000; value = boost::math::float_next(value), ++i)
    {
        BOOST_CHECK_EQUAL(static_cast<Real>(boost::math::ltrunc(value)), boost::math::ltrunc(value));
    } 
}

template <typename Real>
void test_itrunc_near_boundary()
{
   using std::ldexp;
   Real boundary = ldexp(static_cast<Real>(1), std::numeric_limits<int>::digits);

   Real value;
   int i;    

    for (value = boundary, i = 0; i < 100; value = boost::math::float_next(value), ++i)
    {
        BOOST_CHECK_THROW(boost::math::itrunc(value), boost::math::rounding_error);
    }
    for (value = boost::math::float_prior(boundary), i = 0; i < 1000; value = boost::math::float_prior(value), ++i)
    {
        BOOST_CHECK_EQUAL(static_cast<Real>(boost::math::itrunc(value)), boost::math::itrunc(value));
    }
    for (value = boost::math::float_prior(-boundary), i = 0; i < 100; value = boost::math::float_prior(value), ++i)
    {
        BOOST_CHECK_THROW(boost::math::itrunc(value), boost::math::rounding_error);
    }
    for (value = -boundary, i = 0; i < 1000; value = boost::math::float_next(value), ++i)
    {
        BOOST_CHECK_EQUAL(static_cast<Real>(boost::math::itrunc(value)), boost::math::itrunc(value));
    } 
}


BOOST_AUTO_TEST_CASE( test_main )
{
    // Round
    test_llround_near_boundary<float>();
    test_llround_near_boundary<double>();

    test_lround_near_boundary<float>();

    test_iround_near_boundary<float>();

    // Trunc
    test_lltrunc_near_boundary<float>();
    test_lltrunc_near_boundary<double>();

    test_ltrunc_near_boundary<float>();

    test_itrunc_near_boundary<float>();
}