1bd0ac8cb87dae0ab7c39bf357dc741e8ce10418
[pspp] / src / language / expressions / helpers.h
1 /*
2 PSPP - a program for statistical analysis.
3 Copyright (C) 2017 Free Software Foundation, Inc.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef EXPRESSIONS_HELPERS_H
20 #define EXPRESSIONS_HELPERS_H
21
22 #include <ctype.h>
23 #include <float.h>
24 #include <gsl/gsl_cdf.h>
25 #include <gsl/gsl_randist.h>
26 #include <gsl/gsl_sf.h>
27 #include <limits.h>
28 #include <math.h>
29 #include <stdbool.h>
30
31 #include "data/calendar.h"
32 #include "data/case.h"
33 #include "data/data-in.h"
34 #include "data/data-out.h"
35 #include "data/dataset.h"
36 #include "data/dictionary.h"
37 #include "data/settings.h"
38 #include "data/value.h"
39 #include "data/variable.h"
40 #include "data/vector.h"
41 #include "language/expressions/public.h"
42 #include "libpspp/compiler.h"
43 #include "libpspp/i18n.h"
44 #include "libpspp/message.h"
45 #include "libpspp/misc.h"
46 #include "libpspp/str.h"
47 #include "math/distributions.h"
48 #include "math/moments.h"
49 #include "math/random.h"
50
51 #include "gettext.h"
52 #define _(msgid) gettext (msgid)
53
54 static inline double check_errno (double x)
55 {
56   return errno == 0 ? x : SYSMIS;
57 }
58
59 #define check_errno(EXPRESSION) (errno = 0, check_errno (EXPRESSION))
60
61 #define DAY_S (60. * 60. * 24.)         /* Seconds per day. */
62 #define DAY_H 24.                       /* Hours per day. */
63 #define H_S (60 * 60.)                  /* Seconds per hour. */
64 #define H_MIN 60.                       /* Minutes per hour. */
65 #define MIN_S 60.                       /* Seconds per minute. */
66 #define WEEK_DAY 7.                     /* Days per week. */
67 #define WEEK_S (WEEK_DAY * DAY_S)       /* Seconds per week. */
68
69 extern const struct substring empty_string;
70
71 int compare_string_3way (const struct substring *, const struct substring *);
72
73 double expr_ymd_to_date (double year, double month, double day);
74 double expr_ymd_to_ofs (double year, double month, double day);
75 double expr_wkyr_to_date (double wk, double yr);
76 double expr_yrday_to_date (double yr, double day);
77 double expr_yrmoda (double year, double month, double day);
78 double expr_date_difference (double date1, double date2,
79                              struct substring unit);
80 double expr_date_sum (double date, double quantity, struct substring unit_name,
81                       struct substring method_name);
82
83 struct substring alloc_string (struct expression *, size_t length);
84 struct substring copy_string (struct expression *,
85                               const char *, size_t length);
86
87 static inline bool
88 is_valid (double d)
89 {
90   return isfinite (d) && d != SYSMIS;
91 }
92
93 size_t count_valid (double *, size_t);
94
95 double round_nearest (double x, double mult, double fuzzbits);
96 double round_zero (double x, double mult, double fuzzbits);
97
98 struct substring replace_string (struct expression *,
99                                  struct substring haystack,
100                                  struct substring needle,
101                                  struct substring replacement,
102                                  double n);
103
104 double median (double *, size_t n);
105
106 #endif /* expressions/helpers.h */