TPCCLIB
Toggle main menu visibility
Loading...
Searching...
No Matches
libtpcextensions
floatutil.c
Go to the documentation of this file.
1
5
/*****************************************************************************/
6
7
/*****************************************************************************/
8
#include "tpcclibConfig.h"
9
/*****************************************************************************/
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include <time.h>
13
#include <string.h>
14
#include <ctype.h>
15
#include <unistd.h>
16
#include <math.h>
17
#include "
tpcextensions.h
"
18
/*****************************************************************************/
19
20
/*****************************************************************************/
27
int
floatMatch
(
29
const
float
v1,
31
const
float
v2,
33
const
float
lim
34
) {
35
if
(isnan(v1) && isnan(v2))
return
1;
36
if
(isnan(v1) || isnan(v2))
return
0;
37
if
(v1==v2)
return
1;
38
if
(isnan(lim) || lim<0.0)
return
0;
39
if
(fabsf(v1-v2)<=lim)
return
1;
40
return
0;
41
}
42
/*****************************************************************************/
43
44
/*****************************************************************************/
54
int
floatMatchRel
(
56
const
float
v1,
58
const
float
v2,
60
const
float
lim
61
) {
62
if
(isnan(v1) && isnan(v2))
return
1;
63
if
(isnan(v1) || isnan(v2))
return
0;
64
if
(v1==v2)
return
1;
65
if
(isnan(lim))
return
0;
66
float
mean;
67
mean=0.5*(v1+v2);
if
(!isnormal(mean))
return
0;
68
if
(fabsf((v1-v2)/mean)<=lim)
return
1;
69
return
0;
70
}
71
/*****************************************************************************/
72
73
/*****************************************************************************/
82
float
floatMachEps
()
83
{
84
float
macheps=1.0;
85
do
{macheps/=2.0;}
while
((1.0+macheps/2.0)!=1.0);
86
return
(macheps);
87
}
88
/*****************************************************************************/
89
90
/*****************************************************************************/
94
void
floatCopy
(
96
float
*t,
98
float
*s,
100
const
unsigned
int
n
101
) {
102
unsigned
int
i;
103
if
(t==NULL || s==NULL || n<1)
return
;
104
for
(i=0; i<n; i++) t[i]=s[i];
105
}
106
/*****************************************************************************/
107
108
/*****************************************************************************/
113
unsigned
int
floatMaxIndex
(
115
float
*a,
117
const
unsigned
int
n
118
) {
119
if
(a==NULL || n<1)
return
(0);
120
unsigned
int
i, mi=0;
121
float
mv=nanf(
""
);
122
for
(i=0; i<n; i++)
if
(isnan(mv) || a[i]>mv) {mv=a[i]; mi=i;}
123
return
(mi);
124
}
125
/*****************************************************************************/
126
127
/*****************************************************************************/
132
float
floatSum
(
134
float
*a,
136
const
unsigned
int
n
137
) {
138
float
s=0.0;
139
if
(a==NULL || n<1)
return
(s);
140
for
(
unsigned
int
i=0; i<n; i++)
if
(!isnan(a[i])) s+=a[i];
141
return
(s);
142
}
143
/*****************************************************************************/
144
145
/*****************************************************************************/
150
float
floatMean
(
152
float
*a,
154
const
unsigned
int
n
155
) {
156
if
(a==NULL || n<1)
return
(nanf(
""
));
157
float
s=0.0;
158
unsigned
int
i, ci=0;
159
for
(i=0; i<n; i++)
if
(!isnan(a[i])) {ci++; s+=a[i];}
160
if
(ci<1)
return
(nanf(
""
));
161
return
(s/(
float
)ci);
162
}
163
/*****************************************************************************/
164
165
/*****************************************************************************/
174
int
floatGetWithUnit
(
177
const
char
*s,
179
float
*v,
181
int
*u
182
) {
183
if
(v!=NULL) *v=nanf(
""
);
184
if
(u!=NULL) *u=
UNIT_UNKNOWN
;
185
if
(s==NULL)
return
1;
186
int
n=
strTokenNr
(s,
" \t"
);
if
(n==0 || n>2)
return
1;
187
if
(n==1) {
// no unit
188
double
dv;
189
if
(
atofCheck
(s, &dv))
return
1;
190
*v=(float)dv;
return
0;
191
}
192
/* separate number and unit */
193
char
buf[64];
194
n=
strTokenNCpy
(s,
" \t"
, 1, buf, 64);
195
double
dv;
196
if
(
atofCheck
(buf, &dv))
return
1;
197
*v=(float)dv;
198
if
(u==NULL)
return
0;
199
n=
strTokenNCpy
(s,
" \t"
, 2, buf, 64);
200
*u=
unitIdentify
(buf);
201
return
0;
202
}
203
/*****************************************************************************/
204
205
/*****************************************************************************/
210
int
floatSpanPositives
(
212
float
*a,
214
const
int
n
215
) {
216
if
(a==NULL || n<1)
return
(0);
217
int
i=0;
218
for
(i=0; i<n; i++)
if
(!(a[i]>0.0))
break
;
219
return
(i);
220
}
221
/*****************************************************************************/
222
223
/*****************************************************************************/
228
int
floatCSpanPositives
(
230
float
*a,
232
const
int
n
233
) {
234
if
(a==NULL || n<1)
return
(0);
235
int
i=0;
236
for
(i=0; i<n; i++)
if
(a[i]>0.0)
break
;
237
return
(i);
238
}
239
/*****************************************************************************/
240
241
/*****************************************************************************/
247
unsigned
int
floatNonzeroes
(
249
float
*a,
251
const
unsigned
int
n
252
) {
253
if
(a==NULL || n<1)
return
(0);
254
unsigned
int
i, m=0;
255
for
(i=0; i<n; i++)
if
(fabsf(a[i])>0.0) m++;
256
return
(m);
257
}
258
/*****************************************************************************/
259
260
/*****************************************************************************/
atofCheck
int atofCheck(const char *s, double *v)
Definition
decpoint.c:94
floatMachEps
float floatMachEps()
Definition
floatutil.c:82
floatMean
float floatMean(float *a, const unsigned int n)
Definition
floatutil.c:150
floatNonzeroes
unsigned int floatNonzeroes(float *a, const unsigned int n)
Definition
floatutil.c:247
floatCopy
void floatCopy(float *t, float *s, const unsigned int n)
Definition
floatutil.c:94
floatSum
float floatSum(float *a, const unsigned int n)
Definition
floatutil.c:132
floatGetWithUnit
int floatGetWithUnit(const char *s, float *v, int *u)
Definition
floatutil.c:174
floatMatch
int floatMatch(const float v1, const float v2, const float lim)
Definition
floatutil.c:27
floatCSpanPositives
int floatCSpanPositives(float *a, const int n)
Definition
floatutil.c:228
floatMatchRel
int floatMatchRel(const float v1, const float v2, const float lim)
Definition
floatutil.c:54
floatMaxIndex
unsigned int floatMaxIndex(float *a, const unsigned int n)
Definition
floatutil.c:113
floatSpanPositives
int floatSpanPositives(float *a, const int n)
Definition
floatutil.c:210
strTokenNr
int strTokenNr(const char *s1, const char *s2)
Definition
stringext.c:25
strTokenNCpy
int strTokenNCpy(const char *s1, const char *s2, int i, char *s3, int count)
Definition
stringext.c:53
tpcextensions.h
Header file for library libtpcextensions.
UNIT_UNKNOWN
@ UNIT_UNKNOWN
Unknown unit.
Definition
tpcextensions.h:85
unitIdentify
int unitIdentify(const char *s)
Definition
units.c:162
Generated on
for TPCCLIB by
1.17.0