CHDK_DE Vorschauversion  Trunk Rev. 5830
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
gps_math.c-Dateireferenz
#include "camera_info.h"
#include "math.h"
#include "gps_math.h"
+ Include-Abhängigkeitsdiagramm für gps_math.c:

gehe zum Quellcode dieser Datei

Makrodefinitionen

#define __CHDK_GPS_MATH_H
 
#define MAXDIGITS   (18)
 
#define quad   long long
 
#define PIBY2   (3.141592653589793 / 2.0)
 

Funktionen

int fac (int n)
 
double arctan (double x, int n)
 
double arctan2 (double y, double x)
 
double floor (double arg)
 
double ceil (double arg)
 
double Round (double number, int places)
 
static int min (int a, int b)
 
const char * formatDouble (t_format_result result, double value, unsigned length, unsigned fractDigits)
 
void regressionInit (t_regression *rcb, int size, t_regression_xy buffer[])
 
void regressionReset (t_regression *rcb)
 
void regressionAdd (t_regression *rcb, double x, double y)
 
static void regressionCompute (t_regression *rcb)
 
double regressionActual (t_regression *rcb)
 
double regressionForecast (t_regression *rcb, double x)
 
double regressionReverse (t_regression *rcb, double y)
 
double regressionChange (t_regression *rcb)
 
double regressionQuality (t_regression *rcb)
 
double sqrt (double arg)
 
static double _sin (double arg)
 
static double _cos (double arg)
 
double sin (double phi)
 
double cos (double phi)
 

Variablen

static double doubleExpTable [MAXDIGITS+1]
 
static quad quadExpTable [MAXDIGITS+1]
 
static t_format_result staticBuffer
 

Makro-Dokumentation

#define __CHDK_GPS_MATH_H

Definiert in Zeile 2 der Datei gps_math.c.

#define MAXDIGITS   (18)

Definiert in Zeile 77 der Datei gps_math.c.

#define PIBY2   (3.141592653589793 / 2.0)

Definiert in Zeile 438 der Datei gps_math.c.

#define quad   long long

Definiert in Zeile 78 der Datei gps_math.c.

Dokumentation der Funktionen

static double _cos ( double  arg)
static

Definiert in Zeile 428 der Datei gps_math.c.

428  {
429  double s = _sin (arg);
430  return sqrt (1.0 - s*s);
431 }
static double _sin ( double  arg)
static

Definiert in Zeile 415 der Datei gps_math.c.

415  {
416  double sum = arg;
417  double msqarg = -arg*arg;
418  double term = arg;
419  int i;
420 
421  for (i=2; i<=14; i+=2) { // accuracy reverse order here!
422  term *= msqarg / (i*(i+1));
423  sum += term;
424  }
425  return sum;
426 }
double arctan ( double  x,
int  n 
)

Definiert in Zeile 16 der Datei gps_math.c.

16  {
17 
18  double result=0.0;
19  int sign;
20  int i;
21 
22  if (abs(x) < 1 )
23  {
24  result = x;
25  sign = 0;
26  i = 3;
27 
28  do
29  {
30  sign ^= 1;
31  result += ((pow(x, i)/i) * ((sign) ? -1.0 : +1.0));
32  i+=2;
33  } while( (--n) ); // as long as member number is not zero
34  }
35 
36  if (abs(x) >= 1 )
37  {
38  if (x<-1) {result = 3.141592653589793* -0.5; }
39  if (x>=1) {result = 3.141592653589793*0.5; }
40  sign = 0;
41  i = 1;
42 
43  do
44  {
45  sign ^= 1;
46  result += ((1/(i*pow(x,i))) * ((sign) ? -1.0 : +1.0));
47  i+=2;
48  } while( (--n) ); // as long as member number is not zero
49 
50  }
51  return result;
52 }
double arctan2 ( double  y,
double  x 
)

Definiert in Zeile 54 der Datei gps_math.c.

54  {
55  double result;
56  result = 2.0 * arctan((y / (sqrt(x*x+y*y)+x)),30);
57  return result;
58 }
double ceil ( double  arg)

Definiert in Zeile 72 der Datei gps_math.c.

72  {
73  long long trunc = (double) (long long) arg;
74  return arg<=0 || trunc==arg ? trunc : trunc + 1.0;
75 }
double cos ( double  phi)

Definiert in Zeile 459 der Datei gps_math.c.

459  {
460  long periode = floor (phi / PIBY2 + 0.125);
461  double reduced = phi - PIBY2 * periode;
462  switch (periode&3) {
463  default:
464  return _cos (reduced);
465  case 1:
466  return -_sin (reduced);
467  case 2:
468  return -_cos (reduced);
469  case 3:
470  return _sin (reduced);
471  }
472 }
int fac ( int  n)

Definiert in Zeile 8 der Datei gps_math.c.

8  {
9 
10  int i;
11  int f=1;
12  for (i=2; i<=n; i++) f*=i;
13  return(f);
14 }
double floor ( double  arg)

Definiert in Zeile 64 der Datei gps_math.c.

64  {
65  long long trunc = (double) (long long) arg;
66  return arg>=0 || trunc==arg ? trunc : trunc - 1.0;
67 }
const char* formatDouble ( t_format_result  result,
double  value,
unsigned  length,
unsigned  fractDigits 
)

Definiert in Zeile 136 der Datei gps_math.c.

136  {
137 
138  char* sign = "";
139  quad expanded;
140  quad integer, fract;
141  unsigned integerDigits;
142  char* p;
143  unsigned shift = 0;
144  int origLength = length;
145 
146 #ifndef USE_LLI
147  char fractbuffer [20];
148 #endif
149 
150  /*-----------------------
151  * default buffer
152  *----------------------*/
153 
154  if (!result) result = staticBuffer;
155 
156  /*-----------------------
157  * sanity: length
158  *----------------------*/
159 
160  if (length ==0 ) length = sizeof(t_format_result)-1;
161  if (length < 3 ) length = 3;
162  if (length > sizeof(t_format_result)-1) length = sizeof(t_format_result)-1;
163 
164  /*-----------------------
165  * sanity: fract
166  *----------------------*/
167 
168  if (fractDigits > MAXDIGITS) fractDigits = MAXDIGITS;
169  if (fractDigits > length-3 ) fractDigits = length - 3;
170  integerDigits = length - fractDigits - 1;
171 
172  /*-----------------------
173  * abs
174  *----------------------*/
175 
176  if (value < 0) {
177  value = -value;
178  sign = "-";
179  }
180 
181  /*-----------------------
182  * scale
183  *----------------------*/
184 
185  value = value * (double) quadExpTable[fractDigits] + 0.5;
186 
187  while (value >= 9223372036854775807.0 && shift < fractDigits) {
188  shift++;
189  value *= 0.1;
190  }
191 
192  /*-----------------------
193  * general overflow?
194  *----------------------*/
195 
196  if (value >= 9223372036854775807.0) {
197  sprintf (result, "%*s", length, "***");
198  return result;
199  }
200 
201  /*-----------------------
202  * int+fract
203  *----------------------*/
204 
205  expanded = (quad) value;
206  integer = expanded / quadExpTable[fractDigits-shift];
207  fract = expanded % quadExpTable[fractDigits-shift] * quadExpTable[shift];
208 
209  /*-----------------------
210  * fixed overflow
211  *----------------------*/
212 
213  if (integer >= quadExpTable[min(*sign ? integerDigits-1 : integerDigits, MAXDIGITS)]) {
214  sprintf (result, "%*s", length, "***");
215  return result;
216  }
217 
218  /*-----------------------
219  * format
220  *----------------------*/
221 
222 #ifdef USE_LLI
223  if (!origLength) {
224  sprintf (result, "%s%lli.%0*lli", sign, integer, fractDigits, fract);
225  return result;
226  }
227  sprintf (result, "%*lli.%0*lli", integerDigits, integer, fractDigits, fract);
228 #else
229  if (fractDigits >= 10) {
230  sprintf (fractbuffer, "%0*li%09li", fractDigits-9,
231  (long) (fract/1000000000),
232  (long) (fract%1000000000));
233  } else {
234  sprintf (fractbuffer, "%0*li", fractDigits, (long) fract);
235  }
236 
237  if (integer >= 1000000000ll) {
238  if (!origLength) {
239  sprintf (result, "%s%li%09li.%s", sign,
240  (long) (integer/1000000000),
241  (long) (integer%1000000000),
242  fractbuffer);
243  return result;
244  }
245  sprintf (result, "%*li%09li.%s", integerDigits-9,
246  (long) (integer/1000000000),
247  (long) (integer%1000000000),
248  fractbuffer);
249  } else {
250  if (!origLength) {
251  sprintf (result, "%s%li.%s", sign, (long) integer, fractbuffer);
252  return result;
253  }
254  sprintf (result, "%*li.%s", integerDigits, (long) integer, fractbuffer);
255  }
256 #endif
257 
258  if (*sign) {
259  p = result;
260  while (p[1]==' ') p++;
261  p[0] = *sign;
262  }
263 
264  return result;
265 }
static int min ( int  a,
int  b 
)
static

Definiert in Zeile 132 der Datei gps_math.c.

132  {
133  return a<b ? a : b;
134 }
double regressionActual ( t_regression rcb)

Definiert in Zeile 364 der Datei gps_math.c.

364  {
365  regressionCompute (rcb);
366  return rcb->last_x * rcb->s + rcb->t;
367 }
void regressionAdd ( t_regression rcb,
double  x,
double  y 
)

Definiert in Zeile 304 der Datei gps_math.c.

304  {
305 
306  /* remove old value */
307 
308  if (rcb->n >= rcb->size) {
309  double old_x = rcb->values[rcb->index].x;
310  double old_y = rcb->values[rcb->index].y;
311  rcb->values[rcb->index].x = 0;
312  rcb->values[rcb->index].y = 0;
313  rcb->n -= 1;
314  rcb->sx -= old_x;
315  rcb->sy -= old_y;
316  rcb->sxx -= old_x*old_x;
317  rcb->sxy -= old_x*old_y;
318  rcb->valid = 0;
319  rcb->s = 0.0;
320  rcb->t = 0.0;
321  }
322 
323  if (rcb->n < rcb->size) {
324  rcb->n += 1;
325  rcb->sx += x;
326  rcb->sy += y;
327  rcb->sxx += x*x;
328  rcb->sxy += x*y;
329  rcb->valid = 0;
330  rcb->s = 0.0;
331  rcb->t = 0.0;
332  rcb->last_x = x;
333  rcb->values[rcb->index].x = x;
334  rcb->values[rcb->index].y = y;
335  rcb->index = (rcb->index+1) % rcb->size;
336  }
337 }
double regressionChange ( t_regression rcb)

Definiert in Zeile 391 der Datei gps_math.c.

391  {
392  regressionCompute (rcb);
393  return rcb->s;
394 }
static void regressionCompute ( t_regression rcb)
static

Definiert in Zeile 343 der Datei gps_math.c.

343  {
344 
345  double det;
346 
347  if (rcb->valid) return;
348  rcb->valid = -1;
349 
350  det = rcb->n*rcb->sxx - rcb->sx*rcb->sx;
351 
352  /* TODO: possibly epsilon to abs (det) */
353  if (det==0.0) return;
354 
355  rcb->s = (rcb->n * rcb->sxy - rcb->sx * rcb->sy ) / det;
356  rcb->t = (rcb->sxx*rcb->sy - rcb->sx * rcb->sxy) / det;
357  rcb->valid = 1;
358 }
double regressionForecast ( t_regression rcb,
double  x 
)

Definiert in Zeile 373 der Datei gps_math.c.

373  {
374  regressionCompute (rcb);
375  return x * rcb->s + rcb->t;
376 }
void regressionInit ( t_regression rcb,
int  size,
t_regression_xy  buffer[] 
)

Definiert in Zeile 272 der Datei gps_math.c.

272  {
273  rcb->size = size;
274  rcb->values = buffer;
275  regressionReset (rcb);
276 }
double regressionQuality ( t_regression rcb)

Definiert in Zeile 402 der Datei gps_math.c.

402  {
403  regressionCompute (rcb);
404  return rcb->valid==1 ? 1.0 : 0.0;
405 }
void regressionReset ( t_regression rcb)

Definiert in Zeile 282 der Datei gps_math.c.

282  {
283  int i;
284  rcb->n = 0;
285  rcb->sx = 0.0;
286  rcb->sy = 0.0;
287  rcb->sxx = 0.0;
288  rcb->sxy = 0.0;
289  rcb->last_x = 0.0;
290  rcb->valid = 0;
291  rcb->s = 0.0;
292  rcb->t = 0.0;
293  rcb->index = 0;
294  for (i=0; i<rcb->size; i++) {
295  rcb->values[i].x = 0;
296  rcb->values[i].y = 0;
297  }
298 }
double regressionReverse ( t_regression rcb,
double  y 
)

Definiert in Zeile 382 der Datei gps_math.c.

382  {
383  regressionCompute (rcb);
384  return rcb->s != 0.0 ? (y - rcb->t) / rcb->s : 1e9;
385 }
double Round ( double  number,
int  places 
)

Definiert in Zeile 104 der Datei gps_math.c.

104  {
105  return floor(number * doubleExpTable[places] + 0.5) / doubleExpTable[places];
106 }
double sin ( double  phi)

Definiert in Zeile 444 der Datei gps_math.c.

444  {
445  long periode = (long) floor (phi / PIBY2 + 0.5);
446  double reduced = phi - PIBY2 * periode;
447  switch (periode&3) {
448  default:
449  return _sin (reduced);
450  case 1:
451  return _cos (reduced);
452  case 2:
453  return -_sin (reduced);
454  case 3:
455  return -_cos (reduced);
456  }
457 }
double sqrt ( double  arg)

Definiert in Zeile 1014 der Datei wrappers.c.

1014  {
1015  return __sqrt(x);
1016 }

Variablen-Dokumentation

double doubleExpTable[MAXDIGITS+1]
static
Initialisierung:
= {
1.0,
10.0,
100.0,
1000.0,
10000.0,
100000.0,
1000000.0,
10000000.0,
100000000.0,
1000000000.0,
10000000000.0,
100000000000.0,
1000000000000.0,
10000000000000.0,
100000000000000.0,
1000000000000000.0,
10000000000000000.0,
100000000000000000.0,
1000000000000000000.0
}

Definiert in Zeile 82 der Datei gps_math.c.

quad quadExpTable[MAXDIGITS+1]
static
Initialisierung:
= {
1LL,
10LL,
100LL,
1000LL,
10000LL,
100000LL,
1000000LL,
10000000LL,
100000000LL,
1000000000LL,
10000000000LL,
100000000000LL,
1000000000000LL,
10000000000000LL,
100000000000000LL,
1000000000000000LL,
10000000000000000LL,
100000000000000000LL,
1000000000000000000LL
}

Definiert in Zeile 108 der Datei gps_math.c.

t_format_result staticBuffer
static

Definiert in Zeile 130 der Datei gps_math.c.