1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 #if TIME_WITH_SYS_TIME
42 #include "julcal/julcal.h"
54 expr_evaluate (const struct expression *e, const struct ccase *c, int case_num,
57 unsigned char *op = e->op;
59 unsigned char *str = e->str;
60 struct variable **vars = e->var;
64 union value *sp = e->stack;
74 if (sp[1].f == SYSMIS)
76 else if (sp[0].f != SYSMIS)
81 if (sp[1].f == SYSMIS)
83 else if (sp[0].f != SYSMIS)
88 if (sp[1].f == SYSMIS)
90 else if (sp[0].f != SYSMIS)
95 if (sp[1].f == SYSMIS || sp[1].f == 0.)
97 else if (sp[0].f != SYSMIS)
102 if (sp[0].f == SYSMIS)
107 else if (sp[1].f == SYSMIS)
114 else if (sp[0].f == 0.0 && sp[1].f <= 0.0)
117 sp->f = pow (sp[0].f, sp[1].f);
121 /* Note that booleans are always one of 0, 1, or SYSMIS.
123 Truth table (in order of detection):
135 1 and SYSMIS = SYSMIS
136 SYSMIS and SYSMIS = SYSMIS
140 SYSMIS and 1 = SYSMIS
144 if (sp[0].f == 0.0); /* 1 */
145 else if (sp[1].f == 0.0)
147 else if (sp[1].f == SYSMIS)
148 sp->f = SYSMIS; /* 3 */
151 /* Truth table (in order of detection):
164 SYSMIS or SYSMIS = SYSMIS
172 if (sp[0].f == 1.0); /* 1 */
173 else if (sp[1].f == 1.0)
175 else if (sp[1].f == SYSMIS)
176 sp->f = SYSMIS; /* 3 */
181 else if (sp[0].f == 1.0)
187 if (sp[0].f != SYSMIS)
189 if (sp[1].f == SYSMIS)
192 sp->f = sp[0].f == sp[1].f;
197 if (sp[0].f != SYSMIS)
199 if (sp[1].f == SYSMIS)
202 sp->f = sp[0].f >= sp[1].f;
207 if (sp[0].f != SYSMIS)
209 if (sp[1].f == SYSMIS)
212 sp->f = sp[0].f > sp[1].f;
217 if (sp[0].f != SYSMIS)
219 if (sp[1].f == SYSMIS)
222 sp->f = sp[0].f <= sp[1].f;
227 if (sp[0].f != SYSMIS)
229 if (sp[1].f == SYSMIS)
232 sp->f = sp[0].f < sp[1].f;
237 if (sp[0].f != SYSMIS)
239 if (sp[1].f == SYSMIS)
242 sp->f = sp[0].f != sp[1].f;
246 /* String operators. */
249 sp[0].f = st_compare_pad (&sp[0].c[1], sp[0].c[0],
250 &sp[1].c[1], sp[1].c[0]) == 0;
254 sp[0].f = st_compare_pad (&sp[0].c[1], sp[0].c[0],
255 &sp[1].c[1], sp[1].c[0]) >= 0;
259 sp[0].f = st_compare_pad (&sp[0].c[1], sp[0].c[0],
260 &sp[1].c[1], sp[1].c[0]) > 0;
264 sp[0].f = st_compare_pad (&sp[0].c[1], sp[0].c[0],
265 &sp[1].c[1], sp[1].c[0]) <= 0;
269 sp[0].f = st_compare_pad (&sp[0].c[1], sp[0].c[0],
270 &sp[1].c[1], sp[1].c[0]) < 0;
274 sp[0].f = st_compare_pad (&sp[0].c[1], sp[0].c[0],
275 &sp[1].c[1], sp[1].c[0]) != 0;
278 /* Unary functions. */
285 sp->f = fabs (sp->f);
291 sp->f = acos (sp->f);
300 sp->f = asin (sp->f);
307 sp->f = atan (sp->f);
326 sp->f = log10 (sp->f);
342 sp->f = fmod (sp->f, 10);
348 sp->f = floor (sp->f + 0.5);
350 sp->f = -floor (-sp->f + 0.5);
361 sp->f = sqrt (sp->f);
379 sp->f = floor (sp->f);
381 sp->f = -floor (-sp->f);
385 /* N-ary numeric functions. */
394 for (i = 1; i < n_args; i++)
395 if (sp[0].f == sp[i].f)
400 else if (sp[i].f != SYSMIS)
402 sp->f = sysmis ? SYSMIS : 0.0;
411 for (i = 1; i < n_args; i++)
412 if (!st_compare_pad (&sp[0].c[1], sp[0].c[0],
413 &sp[i].c[1], sp[i].c[0]))
424 double weight, mean, variance;
428 moments_of_values (sp, n_args,
429 &weight, &mean, &variance, NULL, NULL);
431 if (weight < *op++ || mean == SYSMIS
432 || mean == 0 || variance == SYSMIS)
435 sp->f = sqrt (variance) / mean;
442 double max = -DBL_MAX;
445 for (i = 0; i < n_args; i++)
446 if (sp[i].f != SYSMIS)
464 for (i = 1; i < n_args; i++)
465 if (st_compare_pad (&sp[i].c[1], sp[i].c[0],
466 &sp[max_idx].c[1], sp[max_idx].c[0]) > 0)
468 sp[0].c = sp[max_idx].c;
478 moments_of_values (sp, n_args,
479 &weight, &mean, NULL, NULL, NULL);
480 sp->f = weight < *op++ ? SYSMIS : mean;
487 double min = DBL_MAX;
490 for (i = 0; i < n_args; i++)
491 if (sp[i].f != SYSMIS)
509 for (i = 1; i < n_args; i++)
510 if (st_compare_pad (&sp[i].c[1], sp[i].c[0],
511 &sp[min_idx].c[1], sp[min_idx].c[0]) < 0)
513 sp[0].c = sp[min_idx].c;
522 for (i = 0; i < n_args; i++)
523 if (sp[i].f == SYSMIS)
534 for (i = 0; i < n_args; i++)
535 if (sp[i].f != SYSMIS)
548 for (i = 1; i < n_args; i += 2)
549 if (sp[i].f == SYSMIS || sp[i + 1].f == SYSMIS)
551 else if (sp[0].f >= sp[i].f && sp[0].f <= sp[i + 1].f)
558 sp->f = sysmis ? SYSMIS : 0.0;
561 case OP_RANGE_STRING:
566 for (i = 1; i < n_args; i += 2)
567 if (st_compare_pad (&sp[0].c[1], sp[0].c[0],
568 &sp[i].c[1], sp[i].c[0]) >= 0
569 && st_compare_pad (&sp[0].c[1], sp[0].c[0],
570 &sp[i + 1].c[1], sp[i + 1].c[0]) <= 0)
581 double weight, variance;
584 moments_of_values (sp, n_args,
585 &weight, NULL, &variance, NULL, NULL);
586 sp->f = weight < *op++ ? SYSMIS : sqrt (variance);
596 for (i = 0; i < n_args; i++)
597 if (sp[i].f != SYSMIS)
611 double weight, variance;
614 moments_of_values (sp, n_args,
615 &weight, NULL, &variance, NULL, NULL);
616 sp->f = weight < *op++ ? SYSMIS : variance;
620 /* Time construction function. */
623 if (sp[0].f == SYSMIS || sp[1].f == SYSMIS || sp[2].f == SYSMIS)
628 min = min (sp[0].f, min (sp[1].f, sp[2].f));
629 max = max (sp[0].f, max (sp[1].f, sp[2].f));
630 if (min < 0. && max > 0.)
632 msg (SW, _("TIME.HMS cannot mix positive and negative "
633 "in its arguments."));
637 sp->f = 60. * (60. * sp[0].f + sp[1].f) + sp[2].f;
642 sp->f /= 60. * 60. * 24.;
648 case OP_CTIME_MINUTES:
654 sp->f *= 60. * 60. * 24.;
656 case OP_CTIME_SECONDS:
660 /* Date construction functions. */
663 sp->f = yrmoda (sp[2].f, sp[1].f, sp[0].f);
665 sp->f *= 60. * 60. * 24.;
669 sp->f = yrmoda (sp[2].f, sp[0].f, sp[1].f);
671 sp->f *= 60. * 60. * 24.;
675 sp->f = yrmoda (sp[1].f, sp[0].f, 1);
677 sp->f *= 60. * 60. * 24.;
681 if (sp[0].f == SYSMIS)
685 sp->f = yrmoda (sp[1].f, sp[0].f * 3 - 2, 1);
687 sp->f *= 60. * 60. * 24.;
692 if (sp[0].f == SYSMIS || sp[1].f == SYSMIS)
694 else if (sp[0].f < 0. || sp[0].f > 53.)
696 msg (SW, _("Week argument to WKYR must be in range 0 to 53."));
701 double result = yrmoda (sp[1].f, 1, 1);
702 if (result != SYSMIS)
703 result = 60. * 60. * 24. * (result + 7. * (sp->f - 1.));
709 if (sp[1].f == SYSMIS)
713 sp->f = yrmoda (sp[0].f, 1, 1);
715 sp->f = 60. * 60. * 24. * (sp->f + floor (sp[1].f) - 1);
720 sp->f = yrmoda (sp[0].f, sp[1].f, sp[2].f);
723 /* Date extraction functions. */
726 sp->f = floor (sp->f / 60. / 60. / 24.) * 60. * 60. * 24.;
730 sp->f = fmod (floor (sp->f / 60. / 60.), 24.);
734 sp->f = 86400. * julian_to_jday (sp->f / 86400.);
740 julian_to_calendar (sp->f / 86400., NULL, NULL, &day);
744 case OP_XDATE_MINUTE:
746 sp->f = fmod (floor (sp->f / 60.), 60.);
752 julian_to_calendar (sp->f / 86400., NULL, &month, NULL);
756 case OP_XDATE_QUARTER:
760 julian_to_calendar (sp->f / 86400., NULL, &month, NULL);
761 sp->f = (month - 1) / 3 + 1;
764 case OP_XDATE_SECOND:
766 sp->f = fmod (sp->f, 60.);
770 sp->f = floor (sp->f / 60. / 60. / 24.);
774 sp->f -= floor (sp->f / 60. / 60. / 24.) * 60. * 60. * 24.;
778 sp->f = (julian_to_jday (sp->f / 86400.) - 1) / 7 + 1;
782 sp->f = julian_to_wday (sp->f / 86400.);
788 julian_to_calendar (sp->f / 86400., &year, NULL, NULL);
793 /* String functions. */
799 dest = pool_alloc (e->pool, 256);
803 for (i = 0; i < n_args; i++)
806 if (sp[i].c[0] + dest[0] < 255)
808 memcpy (&dest[dest[0] + 1], &sp[i].c[1], sp[i].c[0]);
809 dest[0] += sp[i].c[0];
813 memcpy (&dest[dest[0] + 1], &sp[i].c[1], 255 - dest[0]);
827 int last = sp[0].c[0] - sp[1].c[0];
829 for (i = 0; i <= last; i++)
830 if (!memcmp (&sp[0].c[i + 1], &sp[1].c[1], sp[1].c[0]))
845 else if (sp[2].f == SYSMIS)
847 msg (SW, _("Argument 3 of RINDEX may not be system-missing."));
852 int part_len = sp[2].f;
854 if (part_len < 0 || part_len > sp[1].c[0]
855 || sp[1].c[0] % part_len != 0)
857 msg (SW, _("Argument 3 of RINDEX must be between 1 and "
858 "the length of argument 2, and it must "
859 "evenly divide the length of argument 2."));
865 int last = sp[0].c[0] - part_len;
866 for (i = 0; i <= last; i++)
867 for (j = 0; j < sp[1].c[0]; j += part_len)
868 if (!memcmp (&sp[0].c[i + 1], &sp[1].c[j + 1], part_len))
885 for (i = sp[0].c[0] - sp[1].c[0]; i >= 0; i--)
886 if (!memcmp (&sp[0].c[i + 1], &sp[1].c[1], sp[1].c[0]))
901 else if (sp[2].f == SYSMIS)
903 msg (SW, _("Argument 3 of RINDEX may not be system-missing."));
908 int part_len = sp[2].f;
910 if (part_len < 0 || part_len > sp[1].c[0]
911 || sp[1].c[0] % part_len != 0)
913 msg (SW, _("Argument 3 of RINDEX must be between 1 and "
914 "the length of argument 2, and it must "
915 "evenly divide the length of argument 2."));
920 for (i = sp[0].c[0] - part_len; i >= 0; i--)
921 for (j = 0; j < sp[1].c[0]; j += part_len)
922 if (!memcmp (&sp[0].c[i + 1], &sp[1].c[j + 1], part_len))
936 for (i = sp[0].c[0]; i >= 1; i--)
937 sp[0].c[i] = tolower ((unsigned char) (sp[0].c[i]));
940 for (i = sp[0].c[0]; i >= 1; i--)
941 sp[0].c[i] = toupper ((unsigned char) (sp[0].c[i]));
948 if (sp[1].f == SYSMIS || len < 0 || len > 255 || sp[2].c[0] != 1)
950 else if (len > sp[0].c[0])
954 dest = pool_alloc (e->pool, len + 1);
956 memset (&dest[1], sp[2].c[1], len - sp->c[0]);
957 memcpy (&dest[len - sp->c[0] + 1], &sp->c[1], sp->c[0]);
967 if (len < 0 || len > 255 || sp[2].c[0] != 1)
969 else if (len > sp[0].c[0])
973 dest = pool_alloc (e->pool, len + 1);
975 memcpy (&dest[1], &sp->c[1], sp->c[0]);
976 memset (&dest[sp->c[0] + 1], sp[2].c[1], len - sp->c[0]);
988 int len = sp[0].c[0];
989 int cmp = sp[1].c[1];
992 while (i <= len && sp[0].c[i] == cmp)
996 sp[0].c[i] = sp[0].c[0] - i;
1004 if (sp[1].c[0] != 1)
1008 int cmp = sp[1].c[1];
1009 while (sp[0].c[0] > 0 && sp[0].c[sp[0].c[0]] == cmp)
1021 di.format.type = *op++;
1022 di.format.w = *op++;
1023 di.format.d = *op++;
1024 di.e = &sp->c[1] + min (sp->c[0], di.format.w);
1032 unsigned char *dest;
1038 dest = pool_alloc (e->pool, f.w + 1);
1041 assert ((formats[f.type].cat & FCAT_STRING) == 0);
1042 data_out (&dest[1], &f, sp);
1052 if (index < 1 || index > sp[0].c[0])
1057 sp->c[index] = sp->c[0] - index;
1070 if (sp[1].f == SYSMIS || sp[2].f == SYSMIS || index < 1
1071 || index > sp[0].c[0] || n < 1)
1078 sp->c[index] = sp->c[0] - index;
1089 if (sp->f != SYSMIS)
1092 case OP_NUM_TO_BOOL:
1095 else if (sp->f == 1.0)
1097 else if (sp->f != SYSMIS)
1099 msg (SE, _("A number being treated as a Boolean in an "
1100 "expression was found to have a value other than "
1101 "0 (false), 1 (true), or the system-missing value. "
1102 "The result was forced to 0."));
1110 if (sp[0].f != SYSMIS)
1112 if (sp[1].f == SYSMIS)
1118 sp->f = fmod (sp[0].f, sp[1].f);
1122 if (sp->f != SYSMIS)
1123 sp->f *= rng_get_double_normal (pspp_rng ());
1126 if (sp->f != SYSMIS)
1127 sp->f *= rng_get_double (pspp_rng ());
1130 sp->f = sp->f == SYSMIS || !finite (sp->f);
1132 case OP_VEC_ELEM_NUM:
1134 int rindx = sp[0].f + EPSILON;
1135 const struct vector *v = dict_get_vector (default_dict, *op++);
1137 if (sp[0].f == SYSMIS || rindx < 1 || rindx > v->cnt)
1139 if (sp[0].f == SYSMIS)
1140 msg (SE, _("SYSMIS is not a valid index value for vector "
1141 "%s. The result will be set to SYSMIS."),
1144 msg (SE, _("%g is not a valid index value for vector %s. "
1145 "The result will be set to SYSMIS."),
1151 sp->f = c->data[v->var[rindx - 1]->fv].f;
1154 case OP_VEC_ELEM_STR:
1156 int rindx = sp[0].f + EPSILON;
1157 const struct vector *vect = dict_get_vector (default_dict, *op++);
1160 if (sp[0].f == SYSMIS || rindx < 1 || rindx > vect->cnt)
1162 if (sp[0].f == SYSMIS)
1163 msg (SE, _("SYSMIS is not a valid index value for vector "
1164 "%s. The result will be set to the empty "
1168 msg (SE, _("%g is not a valid index value for vector %s. "
1169 "The result will be set to the empty string."),
1170 sp[0].f, vect->name);
1171 sp->c = pool_alloc (e->pool, 1);
1176 v = vect->var[rindx - 1];
1177 sp->c = pool_alloc (e->pool, v->width + 1);
1178 sp->c[0] = v->width;
1180 memcpy (&sp->c[1], c->data[v->fv].s, v->width);
1191 sp->c = pool_alloc (e->pool, *str + 1);
1192 memcpy (sp->c, str, *str + 1);
1198 sp->f = c->data[(*vars)->fv].f;
1199 if (is_num_user_missing (sp->f, *vars))
1205 int width = (*vars)->width;
1208 sp->c = pool_alloc (e->pool, width + 1);
1211 memcpy (&sp->c[1], &c->data[(*vars)->fv], width);
1217 struct ccase *c = lagged_case (*op++);
1224 sp->f = c->data[(*vars)->fv].f;
1225 if (is_num_user_missing (sp->f, *vars))
1233 struct ccase *c = lagged_case (*op++);
1234 int width = (*vars)->width;
1237 sp->c = pool_alloc (e->pool, width + 1);
1241 memset (sp->c, ' ', width);
1243 memcpy (&sp->c[1], &c->data[(*vars)->fv], width);
1251 sp->f = c->data[*op++].f == SYSMIS;
1256 sp->f = c->data[*op++].f;
1273 if (e->type != EXPR_STRING)
1275 double value = sp->f;
1276 if (!finite (value))