TPCCLIB
Loading...
Searching...
No Matches
floatutil.c File Reference

Working with floats. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <math.h>
#include "tpcextensions.h"

Go to the source code of this file.

Functions

int floatMatch (const float v1, const float v2, const float lim)
 
int floatMatchRel (const float v1, const float v2, const float lim)
 
float floatMachEps ()
 
void floatCopy (float *t, float *s, const unsigned int n)
 
unsigned int floatMaxIndex (float *a, const unsigned int n)
 
float floatSum (float *a, const unsigned int n)
 
float floatMean (float *a, const unsigned int n)
 
int floatGetWithUnit (const char *s, float *v, int *u)
 
int floatSpanPositives (float *a, const int n)
 
int floatCSpanPositives (float *a, const int n)
 
unsigned int floatNonzeroes (float *a, const unsigned int n)
 

Detailed Description

Working with floats.

Author
Vesa Oikonen

Definition in file floatutil.c.

Function Documentation

◆ floatCopy()

void floatCopy ( float * t,
float * s,
const unsigned int n )

Copy double values from the 2nd array to the first.

See also
doubleCopy
Parameters
tTarget array.
sSource array.
nLength of arrays.

Definition at line 94 of file floatutil.c.

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}

◆ floatCSpanPositives()

int floatCSpanPositives ( float * a,
const int n )

Returns the length of array consisting of non-positive (<=0 and NaN) values.

Returns
Returns the index of first positive value in the given array.
See also
doubleCSpanPositives, floatSpanPositives, atofList
Parameters
aPointer to the array.
nLength of the array.

Definition at line 228 of file floatutil.c.

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}

◆ floatGetWithUnit()

int floatGetWithUnit ( const char * s,
float * v,
int * u )

Reads numerical value and its unit from argument string.

Both decimal point and comma are accepted. Optional result float value is set to NaN if string was not valid value. Optional unit is set to UNIT_UNKNOWN, if not valid or not found.

Returns
0 if numerical value successfully read, and 1 in case of an error.
See also
doubleGetWithUnit, atofVerified, atofCheck
Parameters
sString from where float and unit is read; string must not contain any extra space characters.
vPointer to the float; enter NULL, if not needed.
uPointer to int for unit code; enter NULL, if not needed.

Definition at line 174 of file floatutil.c.

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}
int atofCheck(const char *s, double *v)
Definition decpoint.c:94
int strTokenNr(const char *s1, const char *s2)
Definition stringext.c:25
int strTokenNCpy(const char *s1, const char *s2, int i, char *s3, int count)
Definition stringext.c:53
@ UNIT_UNKNOWN
Unknown unit.
int unitIdentify(const char *s)
Definition units.c:162

◆ floatMachEps()

float floatMachEps ( )

Estimates the machine epsilon, the upper bound on the relative error due to rounding in floating point arithmetic, within one order of magnitude of the true machine epsilon.

Standard C library should also have FLT_EPSILON and DBL_EPSILON in float.h.

Returns
Estimate of machine epsilon.
Author
Vesa Oikonen
See also
doubleMachEps, floatMatchRel, floatMatch

Definition at line 82 of file floatutil.c.

83{
84 float macheps=1.0;
85 do {macheps/=2.0;} while((1.0+macheps/2.0)!=1.0);
86 return(macheps);
87}

◆ floatMatch()

int floatMatch ( const float v1,
const float v2,
const float lim )

Verifies that given two floats have the same value inside given limits. Values are considered to match also if both are NaNs.

Returns
1 if match is found, and 0 if not.
Author
Vesa Oikonen
See also
doubleMatch, floatMatchRel, floatMachEps, atofCheck
Parameters
v1First value
v2Second value
limLimit for absolute difference (if <0 then test will fail every time)

Definition at line 27 of file floatutil.c.

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}

Referenced by imgCompareConc(), imgCompareTimes(), and imgReadDICOM().

◆ floatMatchRel()

int floatMatchRel ( const float v1,
const float v2,
const float lim )

Verifies that given two floats have the same value inside given relative limit |2*(v1-v2)/(v1+v2)|.

Values are considered to match also if both are NaNs, but not if mean is zero (unless both are exactly zero).

Returns
1 if match is found, and 0 if not.
Author
Vesa Oikonen
See also
doubleMatchRel, floatMatch, floatMachEps
Parameters
v1First value.
v2Second value.
limLimit for relative difference (if <0 then test will fail every time).

Definition at line 54 of file floatutil.c.

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}

Referenced by imgCompareConc(), and imgCompareTimes().

◆ floatMaxIndex()

unsigned int floatMaxIndex ( float * a,
const unsigned int n )

Find the maximum value in given float array.

Returns
The index [0..n-1] of maximum value in array; 0 is returned also in case of errors.
See also
doubleMaxIndex, floatSpanPositives, floatCSpanPositives
Parameters
aPointer to float array.
nLength of array.

Definition at line 113 of file floatutil.c.

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}

◆ floatMean()

float floatMean ( float * a,
const unsigned int n )

Calculate the mean of values in given float array.

Returns
The mean of array values, or NaN in case of an error.
See also
doubleMean, floatSum, floatMaxIndex, fstatMeanSD
Parameters
aPointer to float array.
nLength of array.

Definition at line 150 of file floatutil.c.

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}

◆ floatNonzeroes()

unsigned int floatNonzeroes ( float * a,
const unsigned int n )

Find the number of non-zero values in given float array. Negative values are counted, but NaNs are not.

Returns
The number of non-zero values in array; 0 is returned also in case of errors.
See also
doubleNonzeroes, floatSpanPositives
Parameters
aPointer to float array.
nLength of array.

Definition at line 247 of file floatutil.c.

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}

Referenced by imgContents().

◆ floatSpanPositives()

int floatSpanPositives ( float * a,
const int n )

Returns the length of array consisting of only positive (>0 and not NaN) values.

Returns
Returns the index of first nonpositive value in the given array.
See also
doubleSpanPositives, floatCSpanPositives, atofList, floatNonzeroes
Parameters
aPointer to the array.
nLength of the array.

Definition at line 210 of file floatutil.c.

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}

◆ floatSum()

float floatSum ( float * a,
const unsigned int n )

Calculate the sum of values in given float array.

Returns
The sum of array values.
See also
doubleSum, floatMean, floatMaxIndex
Parameters
aPointer to float array.
nLength of array.

Definition at line 132 of file floatutil.c.

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}