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

Working with doubles. 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 "libtpcmisc.h"

Go to the source code of this file.

Functions

int doubleMatch (const double v1, const double v2, const double lim)
 
int doubleMatchRel (const double v1, const double v2, const double lim)
 
double doubleMachEps ()
 
void doubleCopy (double *t, double *s, const unsigned int n)
 
unsigned int doubleMaxIndex (double *a, const unsigned int n)
 
double doubleSum (double *a, const unsigned int n)
 
double doubleMean (double *a, const unsigned int n)
 
int doubleSpanPositives (double *a, const int n)
 
int doubleCSpanPositives (double *a, const int n)
 
double inverfc (double x)
 
void statSortDouble (double *data, unsigned int n, int order)
 
void statSortFloat (float *data, unsigned int n, int order)
 

Detailed Description

Working with doubles.

Author
Vesa Oikonen

Definition in file doubleutil.c.

Function Documentation

◆ doubleCopy()

void doubleCopy ( double * t,
double * s,
const unsigned int n )

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

Parameters
tTarget array
sSource array
nLength of arrays

Definition at line 93 of file doubleutil.c.

100 {
101 unsigned int i;
102 if(t==NULL || s==NULL || n<1) return;
103 for(i=0; i<n; i++) t[i]=s[i];
104}

◆ doubleCSpanPositives()

int doubleCSpanPositives ( double * 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
doubleSpanPositives
Parameters
aPointer to the array.
nLength of the array.

Definition at line 186 of file doubleutil.c.

191 {
192 if(a==NULL || n<1) return(0);
193 int i=0;
194 for(i=0; i<n; i++) if(a[i]>0.0) break;
195 return(i);
196}

◆ doubleMachEps()

double doubleMachEps ( )

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 DBL_EPSILON in float.h.

Returns
Estimate of machine epsilon.
Author
Vesa Oikonen
See also
doubleMatchRel, doubleMatch

Definition at line 83 of file doubleutil.c.

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

Referenced by householder_transform().

◆ doubleMatch()

int doubleMatch ( const double v1,
const double v2,
const double lim )

Verifies that given two doubles 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
doubleMatchRel, doubleMachEps
Parameters
v1First value
v2Second value
limLimit for absolute difference (if <0 then test will fail every time)

Definition at line 28 of file doubleutil.c.

35 {
36 if(isnan(v1) && isnan(v2)) return 1;
37 if(isnan(v1) || isnan(v2)) return 0;
38 if(v1==v2) return 1;
39 if(isnan(lim) || lim<0.0) return 0;
40 if(fabs(v1-v2)<=lim) return 1;
41 return 0;
42}

◆ doubleMatchRel()

int doubleMatchRel ( const double v1,
const double v2,
const double lim )

Verifies that given two doubles 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
doubleMatch, doubleMachEps
Parameters
v1First value
v2Second value
limLimit for relative difference (if <0 then test will fail every time)

Definition at line 55 of file doubleutil.c.

62 {
63 if(isnan(v1) && isnan(v2)) return 1;
64 if(isnan(v1) || isnan(v2)) return 0;
65 if(v1==v2) return 1;
66 if(isnan(lim)) return 0;
67 double mean;
68 mean=0.5*(v1+v2); if(!isnormal(mean)) return 0;
69 if(fabs((v1-v2)/mean)<=lim) return 1;
70 return 0;
71}
int mean(double *x, double *y, int nr, double *xmean, double *xsd, double *ymean, double *ysd)
Definition pearson.c:341

◆ doubleMaxIndex()

unsigned int doubleMaxIndex ( double * a,
const unsigned int n )

Find the maximum value in given double array.

Returns
The index [0..n-1] of maximum value in array; 0 is returned also in case of errors.
Parameters
aPointer to double array.
nLength of array.

Definition at line 111 of file doubleutil.c.

116 {
117 if(a==NULL || n<1) return(0);
118 unsigned int i, mi=0;
119 double mv=nan("");
120 for(i=0; i<n; i++) if(isnan(mv) || a[i]>mv) {mv=a[i]; mi=i;}
121 return(mi);
122}

◆ doubleMean()

double doubleMean ( double * a,
const unsigned int n )

Calculate the mean of values in given double array.

Returns
The mean of array values, or NaN in case of an error.
See also
doubleSum
Parameters
aPointer to double array.
nLength of array.

Definition at line 148 of file doubleutil.c.

153 {
154 if(a==NULL || n<1) return(nan(""));
155 double s=0.0;
156 unsigned int i, ci=0;
157 for(i=0; i<n; i++) if(!isnan(a[i])) {ci++; s+=a[i];}
158 if(ci<1) return(nan(""));
159 return(s/(double)ci);
160}

◆ doubleSpanPositives()

int doubleSpanPositives ( double * 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
doubleCSpanPositives
Parameters
aPointer to the array.
nLength of the array.

Definition at line 168 of file doubleutil.c.

173 {
174 if(a==NULL || n<1) return(0);
175 int i=0;
176 for(i=0; i<n; i++) if(!(a[i]>0.0)) break;
177 return(i);
178}

◆ doubleSum()

double doubleSum ( double * a,
const unsigned int n )

Calculate the sum of values in given double array.

Returns
The sum of array values.
See also
doubleMean
Parameters
aPointer to double array.
nLength of array.

Definition at line 130 of file doubleutil.c.

135 {
136 double s=0.0;
137 if(a==NULL || n<1) return(s);
138 for(unsigned int i=0; i<n; i++) if(!isnan(a[i])) s+=a[i];
139 return(s);
140}

◆ inverfc()

double inverfc ( double x)

Inverse complementary error function erfc^{-1}(x).

Based on the code by Acklam PJ, 2010.

Returns
Returns an approximate value of erfc^{-1}(x).
Parameters
xParameter for the inverse complementary error function.

Definition at line 205 of file doubleutil.c.

208 {
209 static const double a[] = {
210 -3.969683028665376E+01, 2.209460984245205E+02, -2.759285104469687E+02,
211 1.383577518672690E+02, -3.066479806614716E+01, 2.506628277459239
212 };
213 static const double b[] = {
214 -5.447609879822406E+01, 1.615858368580409E+02, -1.556989798598866E+02,
215 6.680131188771972E+01, -1.328068155288572E+01
216 };
217 static const double c[] = {
218 -7.784894002430293E-03, -3.223964580411365E-01, -2.400758277161838,
219 -2.549732539343734, 4.374664141464968, 2.938163982698783
220 };
221 static const double d[] = {
222 7.784695709041462E-03, 3.224671290700398E-01, 2.445134137142996, 3.754408661907416
223 };
224 double y, e, u;
225 x/=2.0;
226 if(x<0.02425) {
227 double q=sqrt(-2.0 * log(x));
228 y = (((((c[0]*q + c[1])*q + c[2])*q + c[3])*q + c[4])*q + c[5])
229 / ((((d[0]*q + d[1])*q + d[2])*q + d[3])*q + 1.0);
230 } else if(x<=0.97575) {
231 double q=x-0.5;
232 double r=q*q;
233 y = (((((a[0]*r + a[1])*r + a[2])*r + a[3])*r + a[4])*r + a[5])*q
234 / (((((b[0]*r + b[1])*r + b[2])*r + b[3])*r + b[4])*r + 1.0);
235 } else {
236 double q=sqrt(-2.0 * log(1.0-x));
237 y = -(((((c[0]*q + c[1])*q + c[2])*q + c[3])*q + c[4])*q + c[5])
238 / ((((d[0]*q + d[1])*q + d[2])*q + d[3])*q + 1.0);
239 }
240 e=0.5*erfc(-y/M_SQRT2)-x;
241 u=(e)*M_SQRT2*M_PI*exp(0.5*y*y);
242 y-=u/(1.0 + 0.5*y*u);
243 return(fabs(y/M_SQRT2));
244}

Referenced by imgGaussianFIRFilter().

◆ statSortDouble()

void statSortDouble ( double * data,
unsigned int n,
int order )

Sort the given double array into ascending or descending order.

Author
Vesa Oikonen
Parameters
dataPointer to data array of size n
nLength of data array
orderAscending (0) or descending (<>0) order

Definition at line 267 of file doubleutil.c.

274 {
275 if(n<2 || data==NULL) return;
276 if(order==0) qsort(data, n, sizeof(double), statDoubleCompAsc);
277 else qsort(data, n, sizeof(float), statDoubleCompDesc);
278}

◆ statSortFloat()

void statSortFloat ( float * data,
unsigned int n,
int order )

Sort the given float array into ascending or descending order.

Author
Vesa Oikonen
Parameters
dataPointer to data array of size n
nLength of data array
orderAscending (0) or descending (<>0) order

Definition at line 301 of file doubleutil.c.

308 {
309 if(n<2 || data==NULL) return;
310 if(order==0) qsort(data, n, sizeof(float), statFloatCompAsc);
311 else qsort(data, n, sizeof(float), statFloatCompDesc);
312}