-
Notifications
You must be signed in to change notification settings - Fork 8
/
example001_roots_sqrt.cpp
79 lines (61 loc) · 3.13 KB
/
example001_roots_sqrt.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
///////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2020 - 2022. //
// 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 <cstdint>
#include <examples/example_decwide_t.h>
#include <math/wide_decimal/decwide_t.h>
#include <util/memory/util_n_slot_array_allocator.h>
#if defined(WIDE_DECIMAL_NAMESPACE)
auto WIDE_DECIMAL_NAMESPACE::math::wide_decimal::example001_roots_sqrt() -> bool
#else
auto ::math::wide_decimal::example001_roots_sqrt() -> bool
#endif
{
using local_limb_type = std::uint32_t;
constexpr std::int32_t wide_decimal_digits10 = INT32_C(101);
#if defined(WIDE_DECIMAL_NAMESPACE)
constexpr std::int32_t local_elem_number =
WIDE_DECIMAL_NAMESPACE::math::wide_decimal::detail::decwide_t_helper<wide_decimal_digits10, local_limb_type>::elem_number;
#else
constexpr std::int32_t local_elem_number =
::math::wide_decimal::detail::decwide_t_helper<wide_decimal_digits10, local_limb_type>::elem_number;
#endif
using local_allocator_type = util::n_slot_array_allocator<void, local_elem_number, 32U>; // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
#if defined(WIDE_DECIMAL_NAMESPACE)
using dec101_t = WIDE_DECIMAL_NAMESPACE::math::wide_decimal::decwide_t<wide_decimal_digits10,
local_limb_type,
local_allocator_type,
float,
std::int32_t>;
#else
using dec101_t = ::math::wide_decimal::decwide_t<wide_decimal_digits10,
local_limb_type,
local_allocator_type,
float,
std::int32_t>;
#endif
const dec101_t s = sqrt(dec101_t(123456U) / 100);
// N[Sqrt[123456/100], 111]
// 35.1363060095963986639333846404180557597515182871693145281659761647177108954528909286350312191322209780537650946
const dec101_t control
(
"35.1363060095963986639333846404180557597515182871693145281659761647177108954528909286350312191322209780537650946"
);
using std::fabs;
const dec101_t closeness = fabs(1 - fabs(s / control));
const auto result_is_ok = (closeness < (std::numeric_limits<dec101_t>::epsilon() * static_cast<std::uint32_t>(UINT8_C(10))));
return result_is_ok;
}
// Enable this if you would like to activate this main() as a standalone example.
#if defined(WIDE_DECIMAL_STANDALONE_EXAMPLE001_ROOTS_SQRT)
#include <iomanip>
#include <iostream>
auto main() -> int
{
const auto result_is_ok = ::math::wide_decimal::example001_roots_sqrt();
std::cout << "result_is_ok: " << std::boolalpha << result_is_ok << std::endl;
}
#endif