1 /****************************************************************************
2 * RRDtool 1.4.3 Copyright by Tobi Oetiker, 1997-2010
3 ****************************************************************************
4 * rrd_rpncalc.c RPN calculator functions
5 ****************************************************************************/
12 #include "rrd_rpncalc.h"
13 // #include "rrd_graph.h"
20 unsigned short *offset);
22 time_t); /* used to implement LTIME */
32 /* count the number of rpn nodes */
33 while (rpnp[*count].op != OP_END)
35 if (++(*count) > DS_CDEF_MAX_RPN_NODES) {
36 rrd_set_error("Maximum %d RPN nodes permitted. Got %d RPN nodes at present.",
37 DS_CDEF_MAX_RPN_NODES-1,(*count)-1);
42 *rpnc = (rpn_cdefds_t *) calloc(*count, sizeof(rpn_cdefds_t));
43 for (i = 0; rpnp[i].op != OP_END; i++) {
44 (*rpnc)[i].op = (char) rpnp[i].op;
45 if (rpnp[i].op == OP_NUMBER) {
46 /* rpnp.val is a double, rpnc.val is a short */
47 double temp = floor(rpnp[i].val);
49 if (temp < SHRT_MIN || temp > SHRT_MAX) {
51 ("constants must be integers in the interval (%d, %d)",
56 (*rpnc)[i].val = (short) temp;
57 } else if (rpnp[i].op == OP_VARIABLE || rpnp[i].op == OP_PREV_OTHER) {
58 (*rpnc)[i].val = (short) rpnp[i].ptr;
61 /* terminate the sequence */
62 (*rpnc)[(*count) - 1].op = OP_END;
72 /* DS_CDEF_MAX_RPN_NODES is small, so at the expense of some wasted
73 * memory we avoid any reallocs */
74 rpnp = (rpnp_t *) calloc(DS_CDEF_MAX_RPN_NODES, sizeof(rpnp_t));
76 rrd_set_error("failed allocating rpnp array");
79 for (i = 0; rpnc[i].op != OP_END; ++i) {
80 rpnp[i].op = (enum op_en)rpnc[i].op;
81 if (rpnp[i].op == OP_NUMBER) {
82 rpnp[i].val = (double) rpnc[i].val;
83 } else if (rpnp[i].op == OP_VARIABLE || rpnp[i].op == OP_PREV_OTHER) {
84 rpnp[i].ptr = (long) rpnc[i].val;
87 /* terminate the sequence */
92 /* rpn_compact2str: convert a compact sequence of RPN operator nodes back
93 * into a CDEF string. This function is used by rrd_dump.
95 * rpnc: an array of compact RPN operator nodes
96 * ds_def: a pointer to the data source definition section of an RRD header
97 * for lookup of data source names by index
98 * str: out string, memory is allocated by the function, must be freed by the
100 void rpn_compact2str(
105 unsigned short i, offset = 0;
106 char buffer[7]; /* short as a string */
108 for (i = 0; rpnc[i].op != OP_END; i++) {
110 (*str)[offset++] = ',';
112 #define add_op(VV,VVV) \
113 if (addop2str((enum op_en)(rpnc[i].op), VV, VVV, str, &offset) == 1) continue;
115 if (rpnc[i].op == OP_NUMBER) {
116 /* convert a short into a string */
117 #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__)
118 _itoa(rpnc[i].val, buffer, 10);
120 snprintf(buffer, sizeof buffer, "%d", rpnc[i].val);
122 add_op(OP_NUMBER, buffer)
125 if (rpnc[i].op == OP_VARIABLE) {
126 char *ds_name = ds_def[rpnc[i].val].ds_nam;
128 add_op(OP_VARIABLE, ds_name)
131 if (rpnc[i].op == OP_PREV_OTHER) {
132 char *ds_name = ds_def[rpnc[i].val].ds_nam;
134 add_op(OP_VARIABLE, ds_name)
138 #define add_op(VV,VVV) \
139 if (addop2str((enum op_en)rpnc[i].op, VV, #VVV, str, &offset) == 1) continue;
149 add_op(OP_FLOOR, FLOOR)
150 add_op(OP_CEIL, CEIL)
163 add_op(OP_LIMIT, LIMIT)
164 add_op(OP_UNKN, UNKN)
166 add_op(OP_NEGINF, NEGINF)
168 add_op(OP_PREV, PREV)
170 add_op(OP_ISINF, ISINF)
172 add_op(OP_LTIME, LTIME)
173 add_op(OP_TIME, TIME)
174 add_op(OP_ATAN2, ATAN2)
175 add_op(OP_ATAN, ATAN)
176 add_op(OP_SQRT, SQRT)
177 add_op(OP_SORT, SORT)
179 add_op(OP_TREND, TREND)
180 add_op(OP_TRENDNAN, TRENDNAN)
181 add_op(OP_PREDICT, PREDICT)
182 add_op(OP_PREDICTSIGMA, PREDICTSIGMA)
183 add_op(OP_RAD2DEG, RAD2DEG)
184 add_op(OP_DEG2RAD, DEG2RAD)
187 add_op(OP_ADDNAN, ADDNAN)
190 (*str)[offset] = '\0';
199 unsigned short *offset)
204 op_len = strlen(op_str);
205 *result_str = (char *) rrd_realloc(*result_str,
207 *offset) * sizeof(char));
208 if (*result_str == NULL) {
209 rrd_set_error("failed to alloc memory in addop2str");
212 strncpy(&((*result_str)[*offset]), op_str, op_len);
225 rpn_cdefds_t *rpnc = NULL;
228 rpnp = rpn_parse((void *) rrd, def, &lookup_DS);
230 rrd_set_error("failed to parse computed data source");
233 /* Check for OP nodes not permitted in COMPUTE DS.
234 * Moved this check from within rpn_compact() because it really is
235 * COMPUTE DS specific. This is less efficient, but creation doesn't
236 * occur too often. */
237 for (i = 0; rpnp[i].op != OP_END; i++) {
238 if (rpnp[i].op == OP_TIME || rpnp[i].op == OP_LTIME ||
239 rpnp[i].op == OP_PREV || rpnp[i].op == OP_COUNT ||
240 rpnp[i].op == OP_TREND || rpnp[i].op == OP_TRENDNAN ||
241 rpnp[i].op == OP_PREDICT || rpnp[i].op == OP_PREDICTSIGMA ) {
243 ("operators TIME, LTIME, PREV COUNT TREND TRENDNAN PREDICT PREDICTSIGMA are not supported with DS COMPUTE");
248 if (rpn_compact(rpnp, &rpnc, &count) == -1) {
252 /* copy the compact rpn representation over the ds_def par array */
253 memcpy((void *) &(rrd->ds_def[ds_idx].par[DS_cdef]),
254 (void *) rpnc, count * sizeof(rpn_cdefds_t));
259 /* lookup a data source name in the rrd struct and return the index,
260 * should use ds_match() here except:
261 * (1) need a void * pointer to the rrd
262 * (2) error handling is left to the caller
271 rrd = (rrd_t *) rrd_vptr;
273 for (i = 0; i < rrd->stat_head->ds_cnt; ++i) {
274 if (strcmp(ds_name, rrd->ds_def[i].ds_nam) == 0)
277 /* the caller handles a bad data source name in the rpn string */
281 /* rpn_parse : parse a string and generate a rpnp array; modified
282 * str2rpn() originally included in rrd_graph.c
284 * key_hash: a transparent argument passed to lookup(); conceptually this
285 * is a hash object for lookup of a numeric key given a variable name
286 * expr: the string RPN expression, including variable names
287 * lookup(): a function that retrieves a numeric key given a variable name
291 const char *const expr_const,
292 long (*lookup) (void *,
299 char vname[MAX_VNAME_LEN + 10];
302 old_locale = setlocale(LC_NUMERIC, NULL);
303 setlocale(LC_NUMERIC, "C");
306 expr = (char *) expr_const;
309 if ((rpnp = (rpnp_t *) rrd_realloc(rpnp, (++steps + 2) *
310 sizeof(rpnp_t))) == NULL) {
311 setlocale(LC_NUMERIC, old_locale);
315 else if ((sscanf(expr, "%lf%n", &rpnp[steps].val, &pos) == 1)
316 && (expr[pos] == ',')) {
317 rpnp[steps].op = OP_NUMBER;
320 #define match_op(VV,VVV) \
321 else if (strncmp(expr, #VVV, strlen(#VVV))==0 && ( expr[strlen(#VVV)] == ',' || expr[strlen(#VVV)] == '\0' )){ \
322 rpnp[steps].op = VV; \
323 expr+=strlen(#VVV); \
326 #define match_op_param(VV,VVV) \
327 else if (sscanf(expr, #VVV "(" DEF_NAM_FMT ")",vname) == 1) { \
329 if ((length = strlen(#VVV)+strlen(vname)+2, \
330 expr[length] == ',' || expr[length] == '\0') ) { \
331 rpnp[steps].op = VV; \
332 rpnp[steps].ptr = (*lookup)(key_hash,vname); \
333 if (rpnp[steps].ptr < 0) { \
336 } else expr+=length; \
345 match_op(OP_SIN, SIN)
346 match_op(OP_COS, COS)
347 match_op(OP_LOG, LOG)
348 match_op(OP_FLOOR, FLOOR)
349 match_op(OP_CEIL, CEIL)
350 match_op(OP_EXP, EXP)
351 match_op(OP_DUP, DUP)
352 match_op(OP_EXC, EXC)
353 match_op(OP_POP, POP)
354 match_op(OP_LTIME, LTIME)
361 match_op(OP_MIN, MIN)
362 match_op(OP_MAX, MAX)
363 match_op(OP_LIMIT, LIMIT)
364 /* order is important here ! .. match longest first */
365 match_op(OP_UNKN, UNKN)
367 match_op(OP_NEGINF, NEGINF)
369 match_op(OP_COUNT, COUNT)
370 match_op_param(OP_PREV_OTHER, PREV)
371 match_op(OP_PREV, PREV)
372 match_op(OP_INF, INF)
373 match_op(OP_ISINF, ISINF)
374 match_op(OP_NOW, NOW)
375 match_op(OP_TIME, TIME)
376 match_op(OP_ATAN2, ATAN2)
377 match_op(OP_ATAN, ATAN)
378 match_op(OP_SQRT, SQRT)
379 match_op(OP_SORT, SORT)
380 match_op(OP_REV, REV)
381 match_op(OP_TREND, TREND)
382 match_op(OP_TRENDNAN, TRENDNAN)
383 match_op(OP_PREDICT, PREDICT)
384 match_op(OP_PREDICTSIGMA, PREDICTSIGMA)
385 match_op(OP_RAD2DEG, RAD2DEG)
386 match_op(OP_DEG2RAD, DEG2RAD)
387 match_op(OP_AVG, AVG)
388 match_op(OP_ABS, ABS)
389 match_op(OP_ADDNAN, ADDNAN)
390 match_op(OP_MEDIAN, MEDIAN)
392 else if ((sscanf(expr, DEF_NAM_FMT "%n", vname, &pos) == 1)
393 && ((rpnp[steps].ptr = (*lookup) (key_hash, vname)) !=
395 rpnp[steps].op = OP_VARIABLE;
400 setlocale(LC_NUMERIC, old_locale);
410 setlocale(LC_NUMERIC, old_locale);
415 rpnp[steps + 1].op = OP_END;
416 setlocale(LC_NUMERIC, old_locale);
421 rpnstack_t *rpnstack)
424 rpnstack->dc_stacksize = 0;
425 rpnstack->dc_stackblock = 100;
429 rpnstack_t *rpnstack)
431 if (rpnstack->s != NULL)
433 rpnstack->dc_stacksize = 0;
436 static int rpn_compare_double(
440 double diff = *((const double *) x) - *((const double *) y);
442 return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
445 /* rpn_calc: run the RPN calculator; also performs variable substitution;
446 * moved and modified from data_calc() originally included in rrd_graph.c
448 * rpnp : an array of RPN operators (including variable references)
449 * rpnstack : the initialized stack
450 * data_idx : when data_idx is a multiple of rpnp.step, the rpnp.data pointer
451 * is advanced by rpnp.ds_cnt; used only for variable substitution
452 * output : an array of output values; OP_PREV assumes this array contains
453 * the "previous" value at index position output_idx-1; the definition of
454 * "previous" depends on the calling environment
455 * output_idx : an index into the output array in which to store the output
456 * of the RPN calculator
457 * returns: -1 if the computation failed (also calls rrd_set_error)
462 rpnstack_t *rpnstack,
470 /* process each op from the rpn in turn */
471 for (rpi = 0; rpnp[rpi].op != OP_END; rpi++) {
472 /* allocate or grow the stack */
473 if (stptr + 5 > rpnstack->dc_stacksize) {
474 /* could move this to a separate function */
475 rpnstack->dc_stacksize += rpnstack->dc_stackblock;
476 rpnstack->s = (double*)rrd_realloc(rpnstack->s,
477 (rpnstack->dc_stacksize) *
478 sizeof(*(rpnstack->s)));
479 if (rpnstack->s == NULL) {
480 rrd_set_error("RPN stack overflow");
484 #define stackunderflow(MINSIZE) \
486 rrd_set_error("RPN stack underflow"); \
490 switch (rpnp[rpi].op) {
492 rpnstack->s[++stptr] = rpnp[rpi].val;
496 /* Sanity check: VDEFs shouldn't make it here */
497 if (rpnp[rpi].ds_cnt == 0) {
498 rrd_set_error("VDEF made it into rpn_calc... aborting");
501 /* make sure we pull the correct value from
502 * the *.data array. Adjust the pointer into
503 * the array acordingly. Advance the ptr one
504 * row in the rra (skip over non-relevant
507 if (rpnp[rpi].op == OP_VARIABLE) {
508 rpnstack->s[++stptr] = *(rpnp[rpi].data);
510 if ((output_idx) <= 0) {
511 rpnstack->s[++stptr] = DNAN;
513 rpnstack->s[++stptr] =
514 *(rpnp[rpi].data - rpnp[rpi].ds_cnt);
518 if (data_idx % rpnp[rpi].step == 0) {
519 rpnp[rpi].data += rpnp[rpi].ds_cnt;
524 rpnstack->s[++stptr] = (output_idx + 1); /* Note: Counter starts at 1 */
527 if ((output_idx) <= 0) {
528 rpnstack->s[++stptr] = DNAN;
530 rpnstack->s[++stptr] = output[output_idx - 1];
534 rpnstack->s[++stptr] = DNAN;
537 rpnstack->s[++stptr] = DINF;
540 rpnstack->s[++stptr] = -DINF;
543 rpnstack->s[++stptr] = (double) time(NULL);
546 /* HACK: this relies on the data_idx being the time,
547 ** which the within-function scope is unaware of */
548 rpnstack->s[++stptr] = (double) data_idx;
551 rpnstack->s[++stptr] =
552 (double) tzoffset(data_idx) + (double) data_idx;
556 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
557 + rpnstack->s[stptr];
562 if (isnan(rpnstack->s[stptr - 1])) {
563 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
564 } else if (isnan(rpnstack->s[stptr])) {
566 /* rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]; */
568 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
569 + rpnstack->s[stptr];
576 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
577 - rpnstack->s[stptr];
582 rpnstack->s[stptr - 1] = (rpnstack->s[stptr - 1])
583 * (rpnstack->s[stptr]);
588 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1]
589 / rpnstack->s[stptr];
594 rpnstack->s[stptr - 1] = fmod(rpnstack->s[stptr - 1]
595 , rpnstack->s[stptr]);
600 rpnstack->s[stptr] = sin(rpnstack->s[stptr]);
604 rpnstack->s[stptr] = atan(rpnstack->s[stptr]);
608 rpnstack->s[stptr] = 57.29577951 * rpnstack->s[stptr];
612 rpnstack->s[stptr] = 0.0174532952 * rpnstack->s[stptr];
616 rpnstack->s[stptr - 1] = atan2(rpnstack->s[stptr - 1],
622 rpnstack->s[stptr] = cos(rpnstack->s[stptr]);
626 rpnstack->s[stptr] = ceil(rpnstack->s[stptr]);
630 rpnstack->s[stptr] = floor(rpnstack->s[stptr]);
634 rpnstack->s[stptr] = log(rpnstack->s[stptr]);
638 rpnstack->s[stptr + 1] = rpnstack->s[stptr];
650 dummy = rpnstack->s[stptr];
651 rpnstack->s[stptr] = rpnstack->s[stptr - 1];
652 rpnstack->s[stptr - 1] = dummy;
657 rpnstack->s[stptr] = exp(rpnstack->s[stptr]);
661 if (isnan(rpnstack->s[stptr - 1]));
662 else if (isnan(rpnstack->s[stptr]))
663 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
665 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <
666 rpnstack->s[stptr] ? 1.0 : 0.0;
671 if (isnan(rpnstack->s[stptr - 1]));
672 else if (isnan(rpnstack->s[stptr]))
673 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
675 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] <=
676 rpnstack->s[stptr] ? 1.0 : 0.0;
681 if (isnan(rpnstack->s[stptr - 1]));
682 else if (isnan(rpnstack->s[stptr]))
683 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
685 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >
686 rpnstack->s[stptr] ? 1.0 : 0.0;
691 if (isnan(rpnstack->s[stptr - 1]));
692 else if (isnan(rpnstack->s[stptr]))
693 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
695 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] >=
696 rpnstack->s[stptr] ? 1.0 : 0.0;
701 if (isnan(rpnstack->s[stptr - 1]));
702 else if (isnan(rpnstack->s[stptr]))
703 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
705 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
706 rpnstack->s[stptr] ? 0.0 : 1.0;
711 if (isnan(rpnstack->s[stptr - 1]));
712 else if (isnan(rpnstack->s[stptr]))
713 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
715 rpnstack->s[stptr - 1] = rpnstack->s[stptr - 1] ==
716 rpnstack->s[stptr] ? 1.0 : 0.0;
721 rpnstack->s[stptr - 2] = (isnan(rpnstack->s[stptr - 2])
722 || rpnstack->s[stptr - 2] ==
723 0.0) ? rpnstack->s[stptr] : rpnstack->
730 if (isnan(rpnstack->s[stptr - 1]));
731 else if (isnan(rpnstack->s[stptr]))
732 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
733 else if (rpnstack->s[stptr - 1] > rpnstack->s[stptr])
734 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
739 if (isnan(rpnstack->s[stptr - 1]));
740 else if (isnan(rpnstack->s[stptr]))
741 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
742 else if (rpnstack->s[stptr - 1] < rpnstack->s[stptr])
743 rpnstack->s[stptr - 1] = rpnstack->s[stptr];
748 if (isnan(rpnstack->s[stptr - 2]));
749 else if (isnan(rpnstack->s[stptr - 1]))
750 rpnstack->s[stptr - 2] = rpnstack->s[stptr - 1];
751 else if (isnan(rpnstack->s[stptr]))
752 rpnstack->s[stptr - 2] = rpnstack->s[stptr];
753 else if (rpnstack->s[stptr - 2] < rpnstack->s[stptr - 1])
754 rpnstack->s[stptr - 2] = DNAN;
755 else if (rpnstack->s[stptr - 2] > rpnstack->s[stptr])
756 rpnstack->s[stptr - 2] = DNAN;
761 rpnstack->s[stptr] = isnan(rpnstack->s[stptr]) ? 1.0 : 0.0;
765 rpnstack->s[stptr] = isinf(rpnstack->s[stptr]) ? 1.0 : 0.0;
769 rpnstack->s[stptr] = sqrt(rpnstack->s[stptr]);
774 int spn = (int) rpnstack->s[stptr--];
776 stackunderflow(spn - 1);
777 qsort(rpnstack->s + stptr - spn + 1, spn, sizeof(double),
784 int spn = (int) rpnstack->s[stptr--];
787 stackunderflow(spn - 1);
789 p = rpnstack->s + stptr - spn + 1;
790 q = rpnstack->s + stptr;
800 case OP_PREDICTSIGMA:
803 /* the local averaging window (similar to trend, but better here, as we get better statistics thru numbers)*/
804 int locstepsize = rpnstack->s[--stptr];
805 /* the number of shifts and range-checking*/
806 int shifts = rpnstack->s[--stptr];
807 stackunderflow(shifts);
808 // handle negative shifts special
814 /* the real calculation */
816 /* the info on the datasource */
817 time_t dsstep = (time_t) rpnp[rpi - 1].step;
818 int dscount = rpnp[rpi - 1].ds_cnt;
819 int locstep = (int)ceil((float)locstepsize/(float)dsstep);
825 /* now loop for each position */
827 if (shifts<0) { doshifts=-shifts; }
828 for(int loop=0;loop<doshifts;loop++) {
829 /* calculate shift step */
832 shiftstep = loop*rpnstack->s[stptr];
834 shiftstep = rpnstack->s[stptr+loop];
837 rrd_set_error("negative shift step not allowed: %i",shiftstep);
840 shiftstep=(int)ceil((float)shiftstep/(float)dsstep);
841 /* loop all local shifts */
842 for(int i=0;i<=locstep;i++) {
843 /* now calculate offset into data-array - relative to output_idx*/
844 int offset=shiftstep+i;
845 /* and process if we have index 0 of above */
846 if ((offset>=0)&&(offset<output_idx)) {
848 val =rpnp[rpi - 1].data[-dscount * offset];
849 /* and handle the non NAN case only*/
858 /* do the final calculations */
860 if (rpnp[rpi].op == OP_PREDICT) { /* the average */
862 val = sum/(double)count;
865 if (count>1) { /* the sigma case */
866 val=count*sum2-sum*sum;
870 val=sqrt(val/((float)count*((float)count-1.0)));
874 rpnstack->s[stptr] = val;
880 if ((rpi < 2) || (rpnp[rpi - 2].op != OP_VARIABLE)) {
881 rrd_set_error("malformed trend arguments");
884 time_t dur = (time_t) rpnstack->s[stptr];
885 time_t step = (time_t) rpnp[rpi - 2].step;
887 if (output_idx + 1 >= (int) ceil((float) dur / (float) step)) {
888 int ignorenan = (rpnp[rpi].op == OP_TREND);
895 rpnp[rpi - 2].data[rpnp[rpi - 2].ds_cnt * i--];
896 if (ignorenan || !isnan(val)) {
904 rpnstack->s[--stptr] =
905 (count == 0) ? DNAN : (accum / count);
907 rpnstack->s[--stptr] = DNAN;
913 int i = (int) rpnstack->s[stptr--];
917 stackunderflow(i - 1);
919 double val = rpnstack->s[stptr--];
928 /* now push the result back on stack */
930 rpnstack->s[++stptr] = sum / count;
932 rpnstack->s[++stptr] = DNAN;
938 rpnstack->s[stptr] = fabs(rpnstack->s[stptr]);
943 int elements = (int) rpnstack->s[stptr--];
944 int final_elements = elements;
945 double *element_ptr = rpnstack->s + stptr - elements + 1;
946 double *goodvals = element_ptr;
947 double *badvals = element_ptr + elements - 1;
949 stackunderflow(elements - 1);
951 /* move values to consolidate the non-NANs for sorting, keeping
952 * track of how many NANs we encounter. */
953 while (goodvals < badvals) {
954 if (isnan(*goodvals)) {
955 *goodvals = *badvals--;
963 if (!final_elements) {
964 /* no non-NAN elements; push NAN */
965 rpnstack->s[++stptr] = DNAN;
967 /* when goodvals and badvals meet, they might have met on a
968 * NAN, which wouldn't decrease final_elements. so, check
970 if (isnan(*goodvals)) --final_elements;
971 /* and finally, take the median of the remaining non-NAN
973 qsort(element_ptr, final_elements, sizeof(double),
975 if (final_elements % 2 == 1){
976 rpnstack->s[++stptr] = element_ptr[ final_elements / 2 ];
979 rpnstack->s[++stptr] = 0.5 * ( element_ptr[ final_elements / 2 ] + element_ptr[ final_elements / 2 - 1 ] );
988 #undef stackunderflow
991 rrd_set_error("RPN final stack size != 1");
995 output[output_idx] = rpnstack->s[0];
999 /* figure out what the local timezone offset for any point in
1000 time was. Return it in seconds */
1004 int gm_sec, gm_min, gm_hour, gm_yday, gm_year,
1005 l_sec, l_min, l_hour, l_yday, l_year;
1012 gm_hour = t.tm_hour;
1013 gm_yday = t.tm_yday;
1014 gm_year = t.tm_year;
1015 localtime_r(&now, &t);
1022 (l_sec - gm_sec) + (l_min - gm_min) * 60 + (l_hour - gm_hour) * 3600;
1023 if (l_yday > gm_yday || l_year > gm_year) {
1025 } else if (l_yday < gm_yday || l_year < gm_year) {