forked from ckormanyos/wide-integer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example000a_builtin_convert.cpp
91 lines (66 loc) · 2.89 KB
/
example000a_builtin_convert.cpp
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 Christopher Kormanyos 2021 - 2024. //
// Distributed under 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 <examples/example_uintwide_t.h>
#include <math/wide_integer/uintwide_t.h>
namespace local
{
template<typename NumericType>
constexpr auto fabs(NumericType a) -> NumericType
{
return ((a < static_cast<NumericType>(INT8_C(0))) ? -a : a); // LCOV_EXCL_LINE
}
} // namespace local
#if defined(WIDE_INTEGER_NAMESPACE)
auto WIDE_INTEGER_NAMESPACE::math::wide_integer::example000a_builtin_convert() -> bool
#else
auto ::math::wide_integer::example000a_builtin_convert() -> bool
#endif
{
auto result_is_ok = true;
#if defined(WIDE_INTEGER_NAMESPACE)
using WIDE_INTEGER_NAMESPACE::math::wide_integer::int256_t;
#else
using ::math::wide_integer::int256_t;
#endif
{
constexpr int256_t n = -1234567.89; // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
constexpr auto result_n_is_ok = (n == -1234567); // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
static_assert(result_n_is_ok, "Error: example000a_builtin_convert not OK!");
result_is_ok = (result_n_is_ok && result_is_ok);
}
{
constexpr int256_t n = "-12345678900000000000000000000000";
constexpr auto f = static_cast<float>(n);
constexpr auto closeness = local::fabs(1.0F - local::fabs(f / -1.23456789E31F)); // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
constexpr auto result_f_is_ok = (closeness < std::numeric_limits<float>::epsilon());
static_assert(result_f_is_ok, "Error: example000a_builtin_convert not OK!");
result_is_ok = (result_f_is_ok && result_is_ok);
}
{
constexpr int256_t n = "-123456789000000000";
constexpr auto n64 = static_cast<std::int64_t>(n);
constexpr auto result_n_is_ok = (n64 == INT64_C(-123456789000000000));
static_assert((n64 == INT64_C(-123456789000000000)), "Error: example000a_builtin_convert not OK!");
result_is_ok = (result_n_is_ok && result_is_ok);
}
return result_is_ok;
}
// Enable this if you would like to activate this main() as a standalone example.
#if defined(WIDE_INTEGER_STANDALONE_EXAMPLE000A_BUILTIN_CONVERT)
#include <iomanip>
#include <iostream>
auto main() -> int
{
#if defined(WIDE_INTEGER_NAMESPACE)
const auto result_is_ok = WIDE_INTEGER_NAMESPACE::math::wide_integer::example000a_builtin_convert();
#else
const auto result_is_ok = ::math::wide_integer::example000a_builtin_convert();
#endif
std::cout << "result_is_ok: " << std::boolalpha << result_is_ok << std::endl;
return (result_is_ok ? 0 : -1);
}
#endif