CHDK_DE Vorschauversion  Trunk Rev. 5294
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
gps_math.c-Dateireferenz
#include "camera_info.h"
#include "stdlib.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 78 der Datei gps_math.c.

#define PIBY2   (3.141592653589793 / 2.0)

Definiert in Zeile 439 der Datei gps_math.c.

#define quad   long long

Definiert in Zeile 79 der Datei gps_math.c.

Dokumentation der Funktionen

static double _cos ( double  arg)
static

Definiert in Zeile 429 der Datei gps_math.c.

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

Definiert in Zeile 416 der Datei gps_math.c.

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

Definiert in Zeile 17 der Datei gps_math.c.

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

Definiert in Zeile 55 der Datei gps_math.c.

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

Definiert in Zeile 73 der Datei gps_math.c.

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

Definiert in Zeile 460 der Datei gps_math.c.

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

Definiert in Zeile 9 der Datei gps_math.c.

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

Definiert in Zeile 65 der Datei gps_math.c.

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

Definiert in Zeile 137 der Datei gps_math.c.

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

Definiert in Zeile 133 der Datei gps_math.c.

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

Definiert in Zeile 365 der Datei gps_math.c.

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

Definiert in Zeile 305 der Datei gps_math.c.

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

Definiert in Zeile 392 der Datei gps_math.c.

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

Definiert in Zeile 344 der Datei gps_math.c.

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

Definiert in Zeile 374 der Datei gps_math.c.

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

Definiert in Zeile 273 der Datei gps_math.c.

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

Definiert in Zeile 403 der Datei gps_math.c.

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

Definiert in Zeile 283 der Datei gps_math.c.

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

Definiert in Zeile 383 der Datei gps_math.c.

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

Definiert in Zeile 105 der Datei gps_math.c.

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

Definiert in Zeile 445 der Datei gps_math.c.

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

Definiert in Zeile 10 der Datei wrapper.c.

10 { return d2d( _sqrt( d2d(x) ) ); }

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 83 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 109 der Datei gps_math.c.

t_format_result staticBuffer
static

Definiert in Zeile 131 der Datei gps_math.c.