File: git_issue_652.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 (227 lines) | stat: -rw-r--r-- 6,547 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright 2025 Christopher Kormanyos
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/core/lightweight_test.hpp>
#include <boost/multiprecision/cpp_bin_float.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>

#include <array>
#include <cstddef>
#include <cstdint>
#include <limits>

namespace local {

template<typename FloatType, typename LongIshType>
auto test_max_convert() -> void
{
  using float_type = FloatType;

  using float_array_type = std::array<float_type, std::size_t { UINT8_C(7) }>;

  const float_array_type factors =
  {
    float_type { "0.999" },
    float_type { 1 },
    float_type { "1.011" },
    float_type { "2.022" },
    float_type { "3.033" },
    float_type { "4.044" },
    float_type { "5.055" }
  };

  for(std::size_t i = std::size_t { UINT8_C(0) }; i < std::tuple_size<float_array_type>::value; ++i)
  {
    static_cast<void>(i);

    using long_type = LongIshType;

    const float_type
      flt_val
      {
        float_type { (std::numeric_limits<long_type>::max)() } * factors[i]
      };

    const long_type conversion_result { static_cast<long_type>(flt_val) };

    if(i == std::size_t { UINT8_C(0) })
    {
      BOOST_TEST(conversion_result < (std::numeric_limits<long_type>::max)());
    }
    else
    {
      BOOST_TEST(conversion_result == (std::numeric_limits<long_type>::max)());
    }
  }
}

namespace detail {

template<typename FloatType, typename LongIshType>
auto test_convert_neg_to_ll_types_impl(std::vector<LongIshType>& ll_ctrls) -> void
{
  ll_ctrls.clear();

  using float_type = FloatType;

  using std::ldexp;

  using float_array_type = std::array<float_type, std::size_t { UINT8_C(9) }>;

  const float_array_type factors =
  {
    float_type { -ldexp(float_type { 1 }, -28) },
    float_type { "-1.0E-9" },
    float_type { "-0.999" },
    float_type { -1 },
    float_type { "-1.011" },
    float_type { "-2.022" },
    float_type { "-3.033" },
    float_type { "-4.044" },
    float_type { "-5.055" }
  };

  for(std::size_t i = std::size_t { UINT8_C(0) }; i < factors.size(); ++i)
  {
    static_cast<void>(i);

    using float_type = FloatType;
    using long_type = LongIshType;

    const float_type
      flt_val
      {
        static_cast<float_type>((std::numeric_limits<long_type>::max)()) * factors[i]
      };

    const long_type conversion_result { static_cast<long_type>(flt_val) };

    ll_ctrls.push_back(conversion_result);
  }
}

template<typename FloatType, typename LongIshType>
auto test_convert_pos_to_ll_types_impl(std::vector<LongIshType>& ll_ctrls) -> void
{
  ll_ctrls.clear();

  using float_type = FloatType;

  using std::ldexp;

  using float_array_type = std::array<float_type, std::size_t { UINT8_C(9) }>;

  const float_array_type factors =
  {
    float_type { +ldexp(float_type { 1 }, -28) },
    float_type { "+1.0E-9" },
    float_type { "+0.999" },
    float_type { -1 },
    float_type { "+1.011" },
    float_type { "+2.022" },
    float_type { "+3.033" },
    float_type { "+4.044" },
    float_type { "+5.055" }
  };

  for(std::size_t i = std::size_t { UINT8_C(0) }; i < factors.size(); ++i)
  {
    static_cast<void>(i);

    using float_type = FloatType;
    using long_type = LongIshType;

    const float_type
      flt_val
      {
        static_cast<float_type>((std::numeric_limits<long_type>::max)()) * factors[i]
      };

    const long_type conversion_result { static_cast<long_type>(flt_val) };

    ll_ctrls.push_back(conversion_result);
  }
}

} // namespace detail

auto test_convert_pos_to_ll_types() -> void
{
  std::vector<unsigned long long> ull_ctrls_dec;
  std::vector<unsigned long long> ull_ctrls_bin;

  std::vector<signed long long> sll_ctrls_dec;
  std::vector<signed long long> sll_ctrls_bin;

  detail::test_convert_pos_to_ll_types_impl<::boost::multiprecision::cpp_dec_float_50, unsigned long long>(ull_ctrls_dec);
  detail::test_convert_pos_to_ll_types_impl<::boost::multiprecision::cpp_bin_float_50, unsigned long long>(ull_ctrls_bin);

  detail::test_convert_pos_to_ll_types_impl<::boost::multiprecision::cpp_dec_float_50, signed long long>(sll_ctrls_dec);
  detail::test_convert_pos_to_ll_types_impl<::boost::multiprecision::cpp_bin_float_50, signed long long>(sll_ctrls_bin);

  BOOST_TEST(ull_ctrls_dec.size() == ull_ctrls_bin.size());
  BOOST_TEST(!ull_ctrls_dec.empty());

  for(std::size_t i { std::size_t { UINT8_C(0) } }; i < ull_ctrls_dec.size(); ++i)
  {
    BOOST_TEST(ull_ctrls_dec[i] == ull_ctrls_bin[i]);
  }

  BOOST_TEST(sll_ctrls_dec.size() == sll_ctrls_bin.size());
  BOOST_TEST(!sll_ctrls_dec.empty());

  for(std::size_t i { std::size_t { UINT8_C(0) } }; i < sll_ctrls_dec.size(); ++i)
  {
    BOOST_TEST(sll_ctrls_dec[i] == sll_ctrls_bin[i]);
  }
}

auto test_convert_neg_to_ll_types() -> void
{
  std::vector<unsigned long long> ull_ctrls_dec;
  std::vector<unsigned long long> ull_ctrls_bin;

  std::vector<signed long long> sll_ctrls_dec;
  std::vector<signed long long> sll_ctrls_bin;

  detail::test_convert_neg_to_ll_types_impl<::boost::multiprecision::cpp_dec_float_50, unsigned long long>(ull_ctrls_dec);
  detail::test_convert_neg_to_ll_types_impl<::boost::multiprecision::cpp_bin_float_50, unsigned long long>(ull_ctrls_bin);

  detail::test_convert_neg_to_ll_types_impl<::boost::multiprecision::cpp_dec_float_50, signed long long>(sll_ctrls_dec);
  detail::test_convert_neg_to_ll_types_impl<::boost::multiprecision::cpp_bin_float_50, signed long long>(sll_ctrls_bin);

  BOOST_TEST(ull_ctrls_dec.size() == ull_ctrls_bin.size());
  BOOST_TEST(!ull_ctrls_dec.empty());

  for(std::size_t i { std::size_t { UINT8_C(0) } }; i < ull_ctrls_dec.size(); ++i)
  {
    BOOST_TEST(ull_ctrls_dec[i] == ull_ctrls_bin[i]);
  }

  BOOST_TEST(sll_ctrls_dec.size() == sll_ctrls_bin.size());
  BOOST_TEST(!sll_ctrls_dec.empty());

  for(std::size_t i { std::size_t { UINT8_C(0) } }; i < sll_ctrls_dec.size(); ++i)
  {
    BOOST_TEST(sll_ctrls_dec[i] == sll_ctrls_bin[i]);
  }
}

} // namespace local

auto main() -> int
{
  local::test_max_convert<::boost::multiprecision::cpp_dec_float_50, unsigned long long>();

  local::test_max_convert<::boost::multiprecision::cpp_bin_float_50, unsigned long long>();

  local::test_max_convert<::boost::multiprecision::cpp_dec_float_50, signed long long>();

  local::test_max_convert<::boost::multiprecision::cpp_bin_float_50, signed long long>();

  local::test_convert_neg_to_ll_types();
  local::test_convert_pos_to_ll_types();

  return boost::report_errors();
}