-
Notifications
You must be signed in to change notification settings - Fork 8
/
example006_logarithm.cpp
88 lines (66 loc) · 3.39 KB
/
example006_logarithm.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
///////////////////////////////////////////////////////////////////
// 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>
#if defined(WIDE_DECIMAL_NAMESPACE)
auto WIDE_DECIMAL_NAMESPACE::math::wide_decimal::example006_logarithm() -> bool
#else
auto ::math::wide_decimal::example006_logarithm() -> bool
#endif
{
// Compute 1,000 values of Log[(123456789/1000000) * (3^n)],
// the result of which is Log[(123456789/1000000)] + (n Log[3])
#if defined(WIDE_DECIMAL_NAMESPACE)
using dec1001_t = WIDE_DECIMAL_NAMESPACE::math::wide_decimal::decwide_t<static_cast<std::int32_t>(INT32_C(1001))>;
#else
using dec1001_t = ::math::wide_decimal::decwide_t<static_cast<std::int32_t>(INT32_C(1001))>;
#endif
const dec1001_t control_base
{
"4."
"8158912082037439290859846198570189641070729092477641189851225392867818091922368962401331896527129054"
"2666982451156682861982679524527470166196254928165458284131035443816736211730436451521894844085101906"
"0636084466017164897727191862876944629168307811518342680833354831758201699290992192035440727861285590"
"8000194558288545181435034121332042612342464441366245658708127241516875453381972256430977100477994492"
"1474658096051794033615830363454630975474185840919830130502452555134885705490664737858885196366894765"
"6951094462223789705853191026178076148292450697704283854638855304180978799038854077251637293739120576"
"0521703221737573691529408083457723228782448834154143092019701535755380818666246605093528537268510024"
"5327578738401674847141550739942174920396318884548859759526811168922714652063318571945848520663995099"
"2043823208979562358069992851979820083605474605201139892476772979368945875254998083845420172215964725"
"7405172125930111781652611052213816835641831608143730061196534241354604509454101590453678456875749885"
"9"
};
dec1001_t x = dec1001_t(UINT32_C(123456789)) / UINT32_C(1000000);
const dec1001_t ln3 = log(dec1001_t(static_cast<unsigned>(UINT8_C(3))));
const auto tol = static_cast<dec1001_t>(std::numeric_limits<dec1001_t>::epsilon() * static_cast<std::uint32_t>(UINT8_C(10)));
bool result_is_ok = true;
const auto start = std::clock();
for(auto i = static_cast<unsigned>(0U); i < static_cast<unsigned>(UINT32_C(1000)); ++i)
{
const dec1001_t lg = log(x);
const dec1001_t control = control_base + (ln3 * i);
const dec1001_t closeness = fabs(1 - (lg / control));
result_is_ok &= (closeness < tol);
x *= 3U;
}
const auto stop = std::clock();
std::cout << "Time example006_logarithm() : "
<< static_cast<float>(stop - start) / static_cast<float>(CLOCKS_PER_SEC)
<< std::endl;
return result_is_ok;
}
// Enable this if you would like to activate this main() as a standalone example.
#if defined(WIDE_DECIMAL_STANDALONE_EXAMPLE006_LOGARITHM)
#include <iomanip>
#include <iostream>
auto main() -> int
{
const auto result_is_ok = ::math::wide_decimal::example006_logarithm();
std::cout << "result_is_ok: " << std::boolalpha << result_is_ok << std::endl;
}
#endif