numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
util/kahan_adder.cpp

Example to use kahan_adder class.

/*
* Copyright 2021 MusicScience37 (Kenta Kabashima)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cmath>
#include <cstddef>
#include <iomanip>
#include <iostream>
auto main() -> int {
// calculate zeta(4) using kahan_adder
constexpr std::size_t terms = 100000;
for (std::size_t i = 1; i <= terms; ++i) {
const auto i_d = static_cast<double>(i);
const double term = 1.0 / (i_d * i_d * i_d * i_d);
sum += term;
}
const double val = sum;
const double reference = std::pow(num_collect::constants::pid, 4) / 90.0;
std::cout << std::setprecision(15); // NOLINT
std::cout << "Value: " << val << std::endl;
std::cout << "Reference: " << reference << std::endl;
return 0;
}
Class to add numbers using Kahan summation kahan1965.
Definition kahan_adder.h:32
Definition of kahan_adder class.
constexpr double pid
Value of pi.
Definition pi.h:45
Definition of pi.