mirror of
https://github.com/aicodix/dsp.git
synced 2026-04-27 14:30:36 +00:00
test double and single float in Kahan summation
This commit is contained in:
parent
37537e2f23
commit
6c1064ad37
1 changed files with 18 additions and 7 deletions
|
|
@ -4,16 +4,27 @@ Test for the Kahan summation algorithm
|
|||
Copyright 2018 Ahmet Inan <inan@aicodix.de>
|
||||
*/
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include "const.hh"
|
||||
#include "kahan.hh"
|
||||
|
||||
template <typename T>
|
||||
bool Kahan_summation_algorithm_test()
|
||||
{
|
||||
DSP::Kahan<T> sum(3);
|
||||
for (T i = T(2); i < T(181423); i += T(4)) {
|
||||
sum(T(4) / (i * (i + T(1)) * (i + T(2))));
|
||||
sum(T(-4) / ((i + T(2)) * (i + T(3)) * (i + T(4))));
|
||||
}
|
||||
return sum() == DSP::Const<T>::Pi();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
const double pi = 3.14159265358979323846;
|
||||
DSP::Kahan<double> kahan(3.0);
|
||||
for (double i = 2.0; i < 181423.0; i += 4.0) {
|
||||
kahan(4.0 / (i * (i + 1.0) * (i + 2.0)));
|
||||
kahan(-4.0 / ((i + 2.0) * (i + 3.0) * (i + 4.0)));
|
||||
}
|
||||
return kahan() != pi;
|
||||
assert(Kahan_summation_algorithm_test<float>());
|
||||
assert(Kahan_summation_algorithm_test<double>());
|
||||
std::cerr << "Kahan summation algorithm test passed!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue