File: cohen_acceleration.cpp

package info (click to toggle)
scipy 1.17.0-1exp2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 235,340 kB
  • sloc: cpp: 506,914; python: 357,038; ansic: 215,028; javascript: 89,566; fortran: 19,308; cs: 3,081; f90: 1,150; sh: 860; makefile: 519; pascal: 284; lisp: 134; xml: 56; perl: 51
file content (46 lines) | stat: -rw-r--r-- 1,352 bytes parent folder | download | duplicates (13)
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
//  (C) Copyright Nick Thompson 2020.
//  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)
#include <iostream>
#include <iomanip>
#include <boost/math/tools/cohen_acceleration.hpp>
#include <boost/math/constants/constants.hpp>

using boost::math::tools::cohen_acceleration;
using boost::math::constants::pi;

template<typename Real>
class G {
public:
    G(){
        k_ = 0;
    }
    
    Real operator()() {
        k_ += 1;
        return 1/(k_*k_);
    }

private:
    Real k_;
};

int main() {
    using Real = float;
    auto g = G<Real>();
    Real computed = cohen_acceleration(g);
    Real expected = pi<Real>()*pi<Real>()/12;
    std::cout << std::setprecision(std::numeric_limits<Real>::max_digits10);

    std::cout << "Computed = " << computed << " = " << std::hexfloat << computed <<  "\n";
    std::cout << std::defaultfloat;
    std::cout << "Expected = " << expected << " = " << std::hexfloat << expected << "\n";

    // Compute with a specified number of terms:
    // Make sure to reset g:
    g = G<Real>();
    computed = cohen_acceleration(g, 5);
    std::cout << std::defaultfloat;
    std::cout << "Computed = " << computed << " = " << std::hexfloat << computed << " using 5 terms.\n";
}