-
Notifications
You must be signed in to change notification settings - Fork 0
/
manysum.cpp
160 lines (149 loc) · 3.32 KB
/
manysum.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/******************************************************/
/* */
/* manysum.cpp - add many numbers */
/* */
/******************************************************/
/* Copyright 2018,2020 Pierre Abbat.
* This file is part of the Quadlods program.
*
* The Quadlods program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Quadlods is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License and Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* and Lesser General Public License along with Quadlods. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <cfloat>
#include <iostream>
#include <cmath>
#include <vector>
#include "manysum.h"
using namespace std;
int manysum::cnt=0;
void manysum::clear()
{
bucket.clear();
}
double manysum::total()
{
map<int,double>::iterator i;
double t;
for (t=0,i=bucket.begin();i!=bucket.end();++i)
t+=i->second;
return t;
}
void manysum::dump()
{
map<int,double>::iterator i;
for (i=bucket.begin();i!=bucket.end();++i)
cout<<i->first<<' '<<i->second<<endl;
}
void manysum::prune()
{
vector<int> delenda;
int j;
map<int,double>::iterator i;
for (i=bucket.begin();i!=bucket.end();++i)
if (i->second==0)
delenda.push_back(i->first);
for (j=0;j<delenda.size();j++)
bucket.erase(delenda[j]);
}
manysum& manysum::operator+=(double x)
{
int i=DBL_MAX_EXP+3,j=DBL_MAX_EXP+3;
double d;
while (x!=0)
{
/* frexp(NAN) on Linux sets i to 0. On DragonFly BSD,
* it leaves i unchanged. This causes the program to hang
* if j>i. Setting it to DBL_MAX_EXP+5 insures that NAN
* uses a bucket separate from finite numbers.
*/
if (std::isfinite(x))
frexp(x,&i);
else
i=DBL_MAX_EXP+5;
bucket[i]+=x;
frexp(d=bucket[i],&j);
if (j>i)
{
x=d;
bucket[i]=0;
if (((++cnt)&0xff)==0)
prune();
}
else
x=0;
}
return *this;
}
manysum& manysum::operator-=(double x)
{
int i=DBL_MAX_EXP+3,j=DBL_MAX_EXP+3;
double d;
x=-x;
while (x!=0)
{
if (std::isfinite(x))
frexp(x,&i);
else
i=DBL_MAX_EXP+5;
bucket[i]+=x;
frexp(d=bucket[i],&j);
if (j>i)
{
x=d;
bucket[i]=0;
if (((++cnt)&0xff)==0)
prune();
}
else
x=0;
}
return *this;
}
double pairwisesum(double *a,unsigned n)
// a is clobbered.
{
unsigned i,j;
if (n)
{
for (i=1;i<n;i*=2)
for (j=0;j+i<n;j+=2*i)
a[j]+=a[j+i];
return a[0];
}
else
return 0;
}
long double pairwisesum(long double *a,unsigned n)
// a is clobbered.
{
unsigned i,j;
if (n)
{
for (i=1;i<n;i*=2)
for (j=0;j+i<n;j+=2*i)
a[j]+=a[j+i];
return a[0];
}
else
return 0;
}
double pairwisesum(vector<double> &a)
{
return pairwisesum(&a[0],a.size());
}
long double pairwisesum(vector<long double> &a)
{
return pairwisesum(&a[0],a.size());
}