Loading [MathJax]/extensions/TeX/AMSsymbols.js
LAL 7.7.0.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
MultiplyGPSTest.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Kipp Cannon
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with with program; see the file COPYING. If not, write to the Free
16 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
18 */
19
20
21#include <math.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <time.h>
25
26#include <lal/LALStdlib.h>
27#include <lal/Date.h>
28
29
30/* caution: not thread safe; each call returns a pointer to one of 4
31 * internal buffers that are used cyclically, so the 5th call overwrites
32 * the contents of the first buffer; be aware of this when using for
33 * multiple arguments in a printf() call. */
34static const char *print_LIGOTimeGPS(const LIGOTimeGPS *gps)
35{
36 static int i = 0;
37 static char buffer[4][512];
38 return XLALGPSToStr(buffer[i++ & 3], gps);
39}
40
41
42static LIGOTimeGPS compute_with_bc(const char *expr)
43{
44 LIGOTimeGPS gps;
45 char cmd[535];
46 char res[512];
47 FILE *stream;
48
49 snprintf(cmd, sizeof(cmd), "echo 'scale=17 ; %s' | bc", expr);
50 stream = popen(cmd, "r");
51 fgets(res, 512, stream);
52 pclose(stream);
53 XLALStrToGPS(&gps, res, NULL);
54
55 return gps;
56}
57
58
60{
61 LIGOTimeGPS gps;
62
63 gps.gpsSeconds = floor((rand() / (double) RAND_MAX - 0.5) * 200000000);
64 gps.gpsNanoSeconds = floor(rand() / ((double) RAND_MAX + 1) * 1000000000);
65
66 return gps;
67}
68
69
70int main(void)
71{
72 double tolerance = 1e-9;
73 int i;
74
75 srand(time(NULL));
76
77 for(i = 0; i < 6000; i++) {
78 char expr[512];
79 LIGOTimeGPS gps, prod, correct;
80 double x;
81
82 /* multiplicands */
83 gps = random_LIGOTimeGPS();
84 gps.gpsSeconds /= 100;
85 x = pow(100., (rand() / (double) RAND_MAX) * 2. - 1.);
86
87 /* compute product with lal */
88 prod = gps;
89 XLALGPSMultiply(&prod, x);
90
91 /* compute product with bc */
92 sprintf(expr, "%s * %.17g", print_LIGOTimeGPS(&gps), x);
93 correct = compute_with_bc(expr);
94
95 /* compare */
96 if(fabs(XLALGPSDiff(&prod, &correct)) <= tolerance)
97 continue;
98
99 /* didn't match */
100 printf("LAL said %s * %.17g = %s, but bc says = %s\n", print_LIGOTimeGPS(&gps), x, print_LIGOTimeGPS(&prod), print_LIGOTimeGPS(&correct));
101 return 1;
102 }
103
104 return 0;
105}
int XLALStrToGPS(LIGOTimeGPS *t, const char *nptr, char **endptr)
Parse an ASCII string into a LIGOTimeGPS structure.
Definition: StrToGPS.c:91
static LIGOTimeGPS compute_with_bc(const char *expr)
static LIGOTimeGPS random_LIGOTimeGPS(void)
int main(void)
static const char * print_LIGOTimeGPS(const LIGOTimeGPS *gps)
char * XLALGPSToStr(char *, const LIGOTimeGPS *t)
Return a string containing the ASCII base 10 representation of a LIGOTimeGPS.
Definition: StrToGPS.c:274
REAL8 XLALGPSDiff(const LIGOTimeGPS *t1, const LIGOTimeGPS *t0)
Difference between two GPS times as double.
Definition: XLALTime.c:157
LIGOTimeGPS * XLALGPSMultiply(LIGOTimeGPS *gps, REAL8 x)
Multiply a GPS time by a number.
Definition: XLALTime.c:228
Epoch relative to GPS epoch, see LIGOTimeGPS type for more details.
Definition: LALDatatypes.h:458
INT4 gpsSeconds
Seconds since 0h UTC 6 Jan 1980.
Definition: LALDatatypes.h:459
INT4 gpsNanoSeconds
Residual nanoseconds.
Definition: LALDatatypes.h:460