2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: functions.c /main/6 1996/09/25 09:36:28 mustafa $ */
26 * Contains the many of the functions (i.e. do_*) which actually do *
27 * (at least start) the calculations. *
29 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
30 * (c) Copyright 1993, 1994 International Business Machines Corp. *
31 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
32 * (c) Copyright 1993, 1994 Novell, Inc. *
45 extern char *base_str[] ; /* Strings for each base value. */
46 extern char *dtype_str[] ; /* Strings for each display mode value. */
47 extern char *mode_str[] ; /* Strings for each mode value. */
48 extern char *ttype_str[] ; /* Strings for each trig type value. */
49 extern char *vstrs[] ; /* Various strings. */
51 extern struct button buttons[] ; /* Calculator button values. */
52 extern struct button mode_buttons[] ; /* Special "mode" buttons. */
53 extern struct menu_entry menu_entries[] ; /* All the menu strings. */
55 extern Vars v ; /* Calctool variables and options. */
57 double mods[] = { 1.0, 1.0e-1, 1.0e-2, 1.0e-3, 1.0e-4,
58 1.0e-5, 1.0e-6, 1.0e-7, 1.0e-8, 1.0e-9,
59 1.0e-10, 1.0e-11, 1.0e-12, 1.0e-13, 1.0e-14,
60 1.0e-15, 1.0e-16, 1.0e-17, 1.0e-18, 1.0e-19 };
62 static void compute_i(double *target);
63 static int count_sign_changes(double *cf, int count);
67 do_accuracy(void) /* Set display accuracy. */
71 for (i = ACC_START; i <= ACC_END; i++)
72 if (v->current == menu_entries[i].val)
74 v->accuracy = char_val(v->current) ;
83 do_ascii(void) /* Convert ASCII value. */
90 do_base(void) /* Change the current base setting. */
92 if (v->current == BASE_BIN) v->base = BIN ;
93 else if (v->current == BASE_OCT) v->base = OCT ;
94 else if (v->current == BASE_DEC) v->base = DEC ;
95 else if (v->current == BASE_HEX) v->base = HEX ;
102 set_base(enum base_type base)
104 grey_buttons(v->base) ;
105 show_display(v->MPdisp_val) ;
106 set_option_menu((int) BASEITEM, (int)v->base);
108 if (v->rstate) make_registers(MEM) ;
109 if (v->frstate) make_registers(FIN) ;
113 do_business(void) /* Perform special business mode calculations. */
115 Boolean need_show = TRUE;
116 char *display_number = NULL;
117 int MPbv[MP_SIZE], MP1[MP_SIZE], MP2[MP_SIZE], MP3[MP_SIZE], MP4[MP_SIZE] ;
118 int i, len, val, accSav ;
121 if (IS_KEY(v->current, KEY_CTRM))
123 /* Cterm - FMEM1 = int (periodic interest rate).
124 * FMEM2 = Pv (present value).
125 * FMEM4 = Fv (future value).
127 * RESULT = log(FMEM4 / FMEM2) / log(1 + FMEM1)
129 if(v->MPfvals[1] == 0.0 || v->MPfvals[2] == 0.0 || v->MPfvals[4] == 0.0)
131 char *errorMsg, *tmp;
133 /* want to undraw the button first */
134 draw_button(19, 0, 4, 3, FALSE);
135 errorMsg = GETMESSAGE(5, 7, "Can't calculate 'Compound Term'\nwithout a non zero interest rate,\na non zero Present value, and\na non zero Future value.\n");
136 tmp = XtNewString(errorMsg);
143 result = log(v->MPfvals[4] / v->MPfvals[2]) / log(1.0 + (v->MPfvals[1] / 1200));
146 mpcdm(&result, v->MPdisp_val) ;
147 make_registers(FIN) ;
152 else if (IS_KEY(v->current, KEY_DDB))
155 /* Ddb - MEM0 = cost (amount paid for asset).
156 * MEM1 = salvage (value of asset at end of its life).
157 * MEM2 = life (useful life of the asset).
158 * MEM3 = period (time period for depreciation allowance).
161 * for (i = 0; i < MEM3; i++)
163 * VAL = ((MEM0 - bv) * 2) / MEM2
171 mpcmi(v->MPmvals[3], &len) ;
172 for (i = 0; i < len; i++)
174 mpsub(v->MPmvals[0], MPbv, MP1) ;
176 mpmuli(MP1, &val, MP2) ;
177 mpdiv(MP2, v->MPmvals[2], v->MPdisp_val) ;
179 mpadd(MP1, v->MPdisp_val, MPbv) ;
182 else if (IS_KEY(v->current, KEY_FV))
185 /* Fv - FMEM3 = pmt (periodic payment).
186 * FMEM1 = int (periodic interest rate).
187 * FMEM2 = Pv (present value).
188 * FMEM0 = n (number of periods).
191 if(v->MPfvals[0] == 0.0 || v->MPfvals[1] == 0.0 ||
192 (v->MPfvals[2] == 0.0 && v->MPfvals[3] == 0.0) || v->funstate == 0)
197 doerr(GETMESSAGE(5, 2, "ERROR: No Solution"));
201 /* set FV register */
202 mpcmd(v->MPdisp_val, &(v->MPfvals[4]));
206 if ((w = 1.0 + v->MPfvals[1] / (v->MPfvals[5] * 100.0)) == 1.0)
207 result = -(v->MPfvals[2] + v->MPfvals[0] * v->MPfvals[3]);
209 result = -(v->MPfvals[2] * pow(w, v->MPfvals[0]) +
210 v->MPfvals[3] * (pow(w, v->MPfvals[0]) - 1.0) *
211 pow(w, 0.0) / (w - 1.0));
212 mpcdm(&result, v->MPdisp_val) ;
214 if(strcmp(v->display, GETMESSAGE(3, 364, "Error")) != 0)
215 mpcmd(v->MPdisp_val, &(v->MPfvals[4]));
216 make_registers(FIN) ;
219 else if (IS_KEY(v->current, KEY_PMT))
222 /* Pmt - FMEM0 = prin (principal).
223 * FMEM1 = int (periodic interest rate).
226 * RESULT = FMEM0 * (FMEM1 / (1 - pow(FMEM1 + 1, -1 * FMEM2)))
229 if(v->MPfvals[0] == 0.0 || v->MPfvals[1] == 0.0 ||
230 (v->MPfvals[2] == 0.0 && v->MPfvals[4] == 0.0) || v->funstate == 0)
235 doerr(GETMESSAGE(5, 2, "ERROR: No Solution"));
239 /* set Payment register */
240 mpcmd(v->MPdisp_val, &(v->MPfvals[3]));
245 if ((w = 1.0 + v->MPfvals[1] / (v->MPfvals[5] * 100.0)) == 1.0)
246 result = -(v->MPfvals[4] + v->MPfvals[2]) / v->MPfvals[0];
248 result = -(v->MPfvals[2] * pow(w, v->MPfvals[0]) +
249 v->MPfvals[4]) * (w - 1.0) /
250 ((pow(w, v->MPfvals[0]) - 1.0) * pow(w, 0.0));
251 mpcdm(&result, v->MPdisp_val) ;
253 if(strcmp(v->display, GETMESSAGE(3, 364, "Error")) != 0)
254 mpcmd(v->MPdisp_val, &(v->MPfvals[3]));
255 make_registers(FIN) ;
258 else if (IS_KEY(v->current, KEY_PV))
261 /* Pv - FMEM0 = pmt (periodic payment).
262 * FMEM1 = int (periodic interest rate).
265 * RESULT = FMEM0 * (1 - pow(1 + FMEM1, -1 * FMEM2)) / FMEM1
268 if(v->MPfvals[0] == 0.0 || v->MPfvals[1] == 0.0 || v->MPfvals[3] == 0.0 || v->funstate == 0)
273 doerr(GETMESSAGE(5, 2, "ERROR: No Solution"));
277 /* set PV register */
278 mpcmd(v->MPdisp_val, &(v->MPfvals[2]));
282 if ((w = 1.0 + v->MPfvals[1] / (v->MPfvals[5] * 100.0)) == 1.0)
283 result = -(v->MPfvals[4] + v->MPfvals[0] * v->MPfvals[3]);
285 result = -(v->MPfvals[4] / pow(w, v->MPfvals[0]) +
286 v->MPfvals[3] * (pow(w, v->MPfvals[0]) - 1.0) *
287 pow(w, 0.0 - v->MPfvals[0]) / (w - 1.0));
288 mpcdm(&result, v->MPdisp_val) ;
290 if(strcmp(v->display, GETMESSAGE(3, 364, "Error")) != 0)
291 mpcmd(v->MPdisp_val, &(v->MPfvals[2]));
292 make_registers(FIN) ;
295 else if (IS_KEY(v->current, KEY_RATE))
297 /* Rate - MEM0 = fv (future value).
298 * MEM1 = pv (present value).
301 * RESULT = pow(MEM0 / MEM1, 1 / MEM2) - 1
303 if(v->MPfvals[0] == 0.0 || (v->MPfvals[2] == 0.0 && v->MPfvals[3] == 0.0)
304 || (v->MPfvals[3] == 0.0 && v->MPfvals[4] == 0.0)
310 doerr(GETMESSAGE(5, 2, "ERROR: No Solution"));
315 accSav = v->accuracy;
317 display_number = make_number(v->MPdisp_val, FALSE);
318 MPstr_to_num(display_number, DEC, v->MPdisp_val);
319 /* set RATE register */
320 mpcmd(v->MPdisp_val, &(v->MPfvals[1]));
321 v->accuracy = accSav;
326 compute_i(&(v->MPfvals[1]));
327 mpcdm(&(v->MPfvals[1]), v->MPdisp_val);
328 accSav = v->accuracy;
330 display_number = make_number(v->MPdisp_val, FALSE);
331 MPstr_to_num(display_number, DEC, v->MPdisp_val);
332 v->accuracy = accSav;
336 make_registers(FIN) ;
339 STRCPY(v->display, display_number);
340 set_item(DISPLAYITEM, v->display);
343 else if (IS_KEY(v->current, KEY_SLN))
346 /* Sln - MEM0 = cost (cost of the asset).
347 * MEM1 = salvage (salvage value of the asset).
348 * MEM2 = life (useful life of the asset).
350 * RESULT = (MEM0 - MEM1) / MEM2
353 mpsub(v->MPmvals[0], v->MPmvals[1], MP1) ;
354 mpdiv(MP1, v->MPmvals[2], v->MPdisp_val) ;
356 else if (IS_KEY(v->current, KEY_SYD))
359 /* Syd - MEM0 = cost (cost of the asset).
360 * MEM1 = salvage (salvage value of the asset).
361 * MEM2 = life (useful life of the asset).
362 * MEM3 = period (period for which depreciation is computed).
364 * RESULT = ((MEM0 - MEM1) * (MEM2 - MEM3 + 1)) /
365 * (MEM2 * (MEM2 + 1) / 2)
368 mpsub(v->MPmvals[2], v->MPmvals[3], MP2) ;
370 mpaddi(MP2, &val, MP3) ;
371 mpaddi(v->MPmvals[2], &val, MP2) ;
372 mpmul(v->MPmvals[2], MP2, MP4) ;
375 mpdiv(MP4, MP2, MP1) ;
376 mpdiv(MP3, MP1, MP2) ;
377 mpsub(v->MPmvals[0], v->MPmvals[1], MP1) ;
378 mpmul(MP1, MP2, v->MPdisp_val) ;
380 else if (IS_KEY(v->current, KEY_TERM))
383 /* Term - FMEM0 = pmt (periodic payment).
384 * FMEM1 = fv (future value).
385 * FMEM2 = int (periodic interest rate).
387 * RESULT = log(1 + (FMEM1 * FMEM2 / FMEM0)) / log(1 + FMEM2)
390 if(v->MPfvals[1] == 0.0 || (v->MPfvals[2] == 0.0 && v->MPfvals[4] == 0)
391 || v->MPfvals[3] == 0.0 || v->funstate == 0)
396 doerr(GETMESSAGE(5, 2, "ERROR: No Solution"));
400 /* set Term register */
401 mpcmd(v->MPdisp_val, &(v->MPfvals[0]));
405 if ((w = 1.0 + v->MPfvals[1] / (v->MPfvals[5] * 100.0)) == 1.0)
406 result = -(v->MPfvals[4] + v->MPfvals[2]) / v->MPfvals[3];
409 double wdb = pow(w, 0.0);
411 result = log((v->MPfvals[3] * wdb / (w - 1.0) - v->MPfvals[4]) /
412 (v->MPfvals[2] * pow(w, 0.0) + v->MPfvals[3] * wdb /
413 (w - 1.0))) / log(w);
416 if(strcmp(v->display, GETMESSAGE(3, 364, "Error")) != 0)
417 mpcdm(&result, v->MPdisp_val) ;
419 if(strcmp(v->display, GETMESSAGE(3, 364, "Error")) != 0)
420 mpcmd(v->MPdisp_val, &(v->MPfvals[0]));
421 make_registers(FIN) ;
424 else if (IS_KEY(v->current, KEY_PYR))
426 mpcmd(v->MPdisp_val, &(v->MPfvals[5]));
427 result = do_round(v->MPfvals[5], 0);
431 v->MPfvals[5] = result;
432 make_registers(FIN) ;
435 else if (IS_KEY(v->current, KEY_FCLR))
441 /* clear Term register */
442 mpcmd(MP1, &(v->MPfvals[0])) ;
444 /* clear %/YR register */
445 mpcmd(MP1, &(v->MPfvals[1])) ;
447 /* clear PV register */
448 mpcmd(MP1, &(v->MPfvals[2])) ;
450 /* clear Payment register */
451 mpcmd(MP1, &(v->MPfvals[3])) ;
453 /* clear FV register */
454 mpcmd(MP1, &(v->MPfvals[4])) ;
458 mpcmd(MP1, &(v->MPfvals[5])) ;
463 if (need_show == TRUE)
464 show_display(v->MPdisp_val) ;
471 do_calc(void) /* Perform arithmetic calculation and display result. */
476 /* the financial state is false - last key was not a fin. key */
479 if (!(v->opsptr && !v->show_paren)) { /* Don't do if processing parens. */
480 if (IS_KEY(v->current, KEY_EQ) && IS_KEY(v->old_cal_value, KEY_EQ)) {
482 mpstr(v->MPdisp_val, v->MPresult) ;
484 mpstr(v->MPlast_input, v->MPdisp_val) ;
489 if (!IS_KEY(v->current, KEY_EQ) && IS_KEY(v->old_cal_value, KEY_EQ))
492 if (IS_KEY(v->cur_op, KEY_COS) || /* Cos */
493 IS_KEY(v->cur_op, KEY_SIN) || /* Sin */
494 IS_KEY(v->cur_op, KEY_TAN) || /* Tan */
495 v->cur_op == '?') /* Undefined */
496 mpstr(v->MPdisp_val, v->MPresult) ;
498 else if (IS_KEY(v->cur_op, KEY_ADD)) /* Addition */
499 mpadd(v->MPresult, v->MPdisp_val, v->MPresult) ;
501 else if (IS_KEY(v->cur_op, KEY_SUB)) /* Subtraction. */
502 mpsub(v->MPresult, v->MPdisp_val, v->MPresult) ;
504 else if (v->cur_op == '*' ||
505 IS_KEY(v->cur_op, KEY_MUL)) /* Multiplication */
506 mpmul(v->MPresult, v->MPdisp_val, v->MPresult) ;
508 else if (IS_KEY(v->cur_op, KEY_DIV)) /* Division. */
509 mpdiv(v->MPresult, v->MPdisp_val, v->MPresult) ;
511 else if (IS_KEY(v->cur_op, KEY_PER)) /* % */
513 mpmul(v->MPresult, v->MPdisp_val, v->MPresult) ;
514 MPstr_to_num("0.01", DEC, MP1) ;
515 mpmul(v->MPresult, MP1, v->MPresult) ;
518 else if (IS_KEY(v->cur_op, KEY_YTOX)) /* y^x */
519 mppwr2(v->MPresult, v->MPdisp_val, v->MPresult) ;
521 else if (IS_KEY(v->cur_op, KEY_AND)) /* And */
523 mpcmd(v->MPresult, &dres) ;
524 mpcmd(v->MPdisp_val, &dval) ;
525 dres = setbool((BOOLEAN)(ibool(dres) & ibool(dval))) ;
526 mpcdm(&dres, v->MPresult) ;
529 else if (IS_KEY(v->cur_op, KEY_OR)) /* Or */
531 mpcmd(v->MPresult, &dres) ;
532 mpcmd(v->MPdisp_val, &dval) ;
533 dres = setbool((BOOLEAN)(ibool(dres) | ibool(dval))) ;
534 mpcdm(&dres, v->MPresult) ;
537 else if (IS_KEY(v->cur_op, KEY_XOR)) /* Xor */
539 mpcmd(v->MPresult, &dres) ;
540 mpcmd(v->MPdisp_val, &dval) ;
541 dres = setbool((BOOLEAN)(ibool(dres) ^ ibool(dval))) ;
542 mpcdm(&dres, v->MPresult) ;
545 else if (IS_KEY(v->cur_op, KEY_XNOR)) /* Xnor */
547 mpcmd(v->MPresult, &dres) ;
548 mpcmd(v->MPdisp_val, &dval) ;
549 dres = setbool((BOOLEAN)(~ibool(dres) ^ ibool(dval))) ;
550 mpcdm(&dres, v->MPresult) ;
553 else if (IS_KEY(v->cur_op, KEY_EQ)) /* do nothing. */ ; /* Equals */
555 show_display(v->MPresult) ;
557 if (!(IS_KEY(v->current, KEY_EQ) && IS_KEY(v->old_cal_value, KEY_EQ)))
558 mpstr(v->MPdisp_val, v->MPlast_input) ;
560 mpstr(v->MPresult, v->MPdisp_val) ;
562 v->cur_op = v->current ;
564 v->old_cal_value = v->current ;
565 v->new_input = v->key_exp = 0 ;
570 do_clear(void) /* Clear the calculator display and re-initialize. */
573 if (v->error) set_item(DISPLAYITEM, "") ;
581 if (v->current >= '0' && v->current <= '9')
583 mpstr(v->MPcon_vals[char_val(v->current)], v->MPdisp_val) ;
584 show_display(v->MPdisp_val) ;
590 do_delete(void) /* Remove the last numeric character typed. */
592 if (strlen(v->display))
593 v->display[strlen(v->display)-1] = '\0' ;
595 /* If we were entering a scientific number, and we have backspaced over
596 * the exponent sign, then this reverts to entering a fixed point number.
599 if (v->key_exp && !(strchr(v->display, '+')))
602 v->display[strlen(v->display)-1] = '\0' ;
603 set_item(OPITEM, "") ;
606 /* If we've backspaced over the numeric point, clear the pointed flag. */
608 if (v->pointed && !(strchr(v->display, '.'))) v->pointed = 0 ;
610 if(strcmp(v->display, "") == 0)
613 set_item(DISPLAYITEM, v->display) ;
614 MPstr_to_num(v->display, v->base, v->MPdisp_val) ;
619 do_exchange(void) /* Exchange display with memory register. */
621 int i, MPtemp[MP_SIZE] ;
623 for (i = MEM_START; i <= MEM_END; i++)
624 if (v->current == menu_entries[i].val)
626 mpstr(v->MPdisp_val, MPtemp) ;
627 mpstr(v->MPmvals[char_val(v->current)], v->MPdisp_val) ;
628 mpstr(MPtemp, v->MPmvals[char_val(v->current)]) ;
629 make_registers(MEM) ;
636 do_expno(void) /* Get exponential number. */
638 /* the financial state is false - last key was not a fin. key */
641 v->pointed = (strchr(v->display, '.') != NULL) ;
644 STRCPY(v->display, "1.0 +") ;
645 v->new_input = v->pointed = 1 ;
647 else if (!v->pointed)
649 STRNCAT(v->display, ". +", 3) ;
652 else if (!v->key_exp) STRNCAT(v->display, " +", 2) ;
655 v->exp_posn = strchr(v->display, '+') ;
656 set_item(DISPLAYITEM, v->display) ;
657 MPstr_to_num(v->display, v->base, v->MPdisp_val) ;
662 do_factorial(int *MPval, int *MPres) /* Calculate the factorial of MPval. */
665 int i, MPa[MP_SIZE], MP1[MP_SIZE], MP2[MP_SIZE] ;
667 /* NOTE: do_factorial, on each iteration of the loop, will attempt to
668 * convert the current result to a double. If v->error is set,
669 * then we've overflowed. This is to provide the same look&feel
672 * XXX: Needs to be improved. Shouldn't need to convert to a double in
673 * order to check this.
680 if (mpeq(MPval, MP1) && mpge(MPval, MP2)) /* Only positive integers. */
683 if (mpeq(MP1, MP2)) /* Special case for 0! */
690 if (!i) matherr((struct exception *) NULL) ;
694 mpmuli(MPa, &i, MPa) ;
696 if (v->error) break ;
700 else matherr((struct exception *) NULL) ;
706 do_frame(void) /* Exit dtcalc. */
712 do_function(void) /* Perform a user defined function. */
714 enum fcp_type scurwin ;
715 int fno, scolumn, srow ;
718 scolumn = v->column ;
719 scurwin = v->curwin ;
721 if (v->current >= '0' && v->current <= '9')
723 fno = char_val(v->current) ;
724 if(strcmp(v->fun_vals[fno], "") != 0)
725 process_str(v->fun_vals[fno], M_FUN) ;
727 v->curwin = scurwin ;
729 v->column = scolumn ;
737 int i, MP1[MP_SIZE], MP2[MP_SIZE] ;
739 /* the financial state is false - last key was not a fin. key */
742 if (IS_KEY(v->current, KEY_HYP)) /* Hyp */
744 v->hyperbolic = !v->hyperbolic ;
745 set_item(HYPITEM, (v->hyperbolic) ? vstrs[(int) V_HYP]
749 else if (IS_KEY(v->current, KEY_INV)) /* Inv */
751 v->inverse = !v->inverse ;
752 set_item(INVITEM, (v->inverse) ? vstrs[(int) V_INV]
756 else if (IS_KEY(v->current, KEY_32)) /* &32 */
758 mpcmd(v->MPdisp_val, &dval) ;
759 dval2 = ibool2(dval);
761 doerr(GETMESSAGE(5, 6, "ERR:Num too large for operation"));
764 dval = setbool((BOOLEAN)dval2) ;
765 mpcdm(&dval, v->MPdisp_val) ;
769 else if (IS_KEY(v->current, KEY_16)) /* &16 */
771 mpcmd(v->MPdisp_val, &dval) ;
772 dval2 = ibool2(dval);
774 doerr(GETMESSAGE(5, 6, "ERR:Num too large for operation"));
777 dval = setbool((BOOLEAN)(ibool(dval2) & 0xffff)) ;
778 mpcdm(&dval, v->MPdisp_val) ;
782 else if (IS_KEY(v->current, KEY_ETOX)) /* e^x */
784 mpstr(v->MPdisp_val, MP1) ;
785 mpexp(MP1, v->MPdisp_val) ;
788 else if (IS_KEY(v->current, KEY_TTOX)) /* 10^x */
792 mppwr2(MP1, v->MPdisp_val, v->MPdisp_val) ;
795 else if (IS_KEY(v->current, KEY_LN)) /* Ln */
797 mpstr(v->MPdisp_val, MP1) ;
798 mpln(MP1, v->MPdisp_val) ;
801 else if (IS_KEY(v->current, KEY_LOG)) /* Log */
803 mplog10(v->MPdisp_val, v->MPdisp_val) ;
806 else if (IS_KEY(v->current, KEY_RAND)) /* Rand */
809 mpcdm(&dval, v->MPdisp_val) ;
812 else if (IS_KEY(v->current, KEY_SQRT)) /* Sqrt */
814 mpstr(v->MPdisp_val, MP1) ;
815 mpsqrt(MP1, v->MPdisp_val) ;
818 else if (IS_KEY(v->current, KEY_NOT)) /* Not */
820 mpcmd(v->MPdisp_val, &dval) ;
821 dval = setbool((BOOLEAN)~ibool(dval)) ;
822 mpcdm(&dval, v->MPdisp_val) ;
825 else if (IS_KEY(v->current, KEY_REC)) /* 1/x */
829 mpstr(v->MPdisp_val, MP2) ;
830 mpdiv(MP1, MP2, v->MPdisp_val) ;
832 else if (IS_KEY(v->current, KEY_FACT)) /* x! */
834 do_factorial(v->MPdisp_val, MP1) ;
835 mpstr(MP1, v->MPdisp_val) ;
837 else if (IS_KEY(v->current, KEY_SQR)) /* x^2 */
839 mpstr(v->MPdisp_val, MP1) ;
840 mpmul(MP1, MP1, v->MPdisp_val) ;
843 else if (IS_KEY(v->current, KEY_CHS)) /* +/- */
847 if (*v->exp_posn == '+') *v->exp_posn = '-' ;
848 else *v->exp_posn = '+' ;
849 set_item(DISPLAYITEM, v->display) ;
850 MPstr_to_num(v->display, v->base, v->MPdisp_val) ;
855 mpneg(v->MPdisp_val, v->MPdisp_val) ;
856 mpstr(v->MPdisp_val, v->MPlast_input) ;
859 show_display(v->MPdisp_val) ;
864 do_keys(void) /* Display/undisplay the dtcalc key values. */
866 v->tstate = !v->tstate ;
871 do_mode(void) /* Set special calculator mode. */
873 if (v->current == MODE_FIN) v->modetype = FINANCIAL ;
874 else if (v->current == MODE_LOG) v->modetype = LOGICAL ;
875 else if (v->current == MODE_SCI) v->modetype = SCIENTIFIC ;
878 v->curwin = FCP_KEY ;
883 do_none(void) /* Null routine for empty buttons. */
893 static int maxvals[4] = { 1, 7, 9, 15 } ;
895 /* the financial state is false - last key was not a fin. key */
898 nextchar = v->current ;
899 n = v->current - '0' ;
900 if (v->base == HEX && v->current >= 'a' && v->current <= 'f')
902 nextchar -= 32 ; /* Convert to uppercase hex digit. */
903 n = v->current - 'a' + 10 ;
905 if (n > maxvals[(int) v->base])
913 SPRINTF(v->display, "%c", nextchar) ;
918 len = strlen(v->display) ;
919 if (len < MAX_DIGITS)
921 v->display[len] = nextchar ;
922 v->display[len+1] = '\0' ;
927 set_item(DISPLAYITEM, v->display) ;
928 MPstr_to_num(v->display, v->base, v->MPdisp_val) ;
934 do_numtype(void) /* Set number type (engineering, fixed or scientific). */
936 if (v->current == DISP_ENG) v->dtype = ENG ;
937 else if (v->current == DISP_FIX) v->dtype = FIX ;
938 else if (v->current == DISP_SCI) v->dtype = SCI ;
941 set_numtype(v->dtype);
945 set_numtype(enum num_type dtype)
948 show_display(v->MPdisp_val) ;
949 set_option_menu((int) NUMITEM, (int)v->dtype);
950 if (v->rstate) make_registers(MEM) ;
951 if (v->frstate) make_registers(FIN) ;
960 /* the financial state is false - last key was not a fin. key */
963 /* Check to see if this is the first outstanding parenthesis. If so, and
964 * their is a current operation already defined, then add the current
965 * operation to the parenthesis expression being displayed.
966 * Increment parentheses count, and add the open paren to the expression.
969 if (IS_KEY(v->current, KEY_LPAR))
971 if (v->noparens == 0)
973 /* if not in default state, put the operand between the display
974 value and the paren, else just put the paren */
977 /* there is no paren, and there is no current operand ... Let's
978 make the current operand into a "x" */
985 /* if the current op is an '=' and the result in the display is
986 zero, we want to ignore the display */
989 mpcmd(v->MPdisp_val, &tmpdb);
993 STRCPY(v->display, "") ;
994 set_item(DISPLAYITEM, v->display) ;
1001 paren_disp(v->cur_op) ;
1007 paren_disp(v->cur_op) ;
1012 STRCPY(v->display, "") ;
1013 set_item(DISPLAYITEM, v->display) ;
1018 int len = strlen(v->display);
1020 if(v->display[len - 1] >= '0' && v->display[len - 1] <= '9')
1021 paren_disp(v->cur_op) ;
1024 v->pending = v->current ;
1028 /* If we haven't had any left brackets yet, and this is a right bracket,
1029 * then just ignore it.
1030 * Decrement the bracket count. If the count is zero, then process the
1031 * parenthesis expression.
1034 else if (IS_KEY(v->current, KEY_RPAR))
1036 if (!v->noparens) return ;
1041 paren_disp(v->current) ;
1043 while (*ptr != '(') ptr++ ;
1044 while (*ptr != '\0') process_parens(*ptr++) ;
1048 paren_disp(v->current) ;
1055 /* the financial state is false - last key was not a fin. key */
1058 /* Certain pending operations which are half completed, force the numeric
1059 * keypad to be reshown (assuming they already aren't).
1061 * Con, Exch, Fun, Sto, Rcl and Acc show buttons 0 - 9.
1062 * < and > show buttons 0 - f.
1067 if (IS_KEY(v->current, KEY_CON) || /* Con. */
1068 IS_KEY(v->current, KEY_EXCH) || /* Exch. */
1069 IS_KEY(v->current, KEY_FUN) || /* Fun. */
1070 IS_KEY(v->current, KEY_STO) || /* Sto. */
1071 IS_KEY(v->current, KEY_RCL) || /* Rcl. */
1072 IS_KEY(v->current, KEY_ACC)) /* Acc. */
1074 if (IS_KEY(v->current, KEY_LSFT) ||
1075 IS_KEY(v->current, KEY_RSFT))
1079 if (IS_KEY(v->pending, KEY_BASE)) do_base() ; /* Base */
1080 else if (IS_KEY(v->pending, KEY_DISP)) do_numtype() ; /* Disp */
1081 else if (IS_KEY(v->pending, KEY_TRIG)) do_trigtype() ; /* Trig */
1082 else if (IS_KEY(v->pending, KEY_CON)) do_constant() ; /* Con */
1083 else if (IS_KEY(v->pending, KEY_EXCH)) do_exchange() ; /* Exch */
1084 else if (IS_KEY(v->pending, KEY_FUN)) do_function() ; /* Fun */
1085 else if (IS_KEY(v->pending, KEY_STO) || /* Sto */
1086 IS_KEY(v->pending, KEY_RCL)) /* Rcl */
1089 if (IS_KEY(v->pending_op, KEY_ADD) ||
1090 IS_KEY(v->pending_op, KEY_SUB) ||
1091 IS_KEY(v->pending_op, KEY_MUL) ||
1092 IS_KEY(v->pending_op, KEY_DIV)) return ;
1094 else if (IS_KEY(v->pending, KEY_LSFT) || /* < */
1095 IS_KEY(v->pending, KEY_RSFT)) do_shift() ; /* > */
1096 else if (IS_KEY(v->pending, KEY_ACC)) do_accuracy() ; /* Acc */
1097 else if (IS_KEY(v->pending, KEY_MODE)) do_mode() ; /* Mode */
1098 else if (IS_KEY(v->pending, KEY_LPAR)) /* ( */
1103 else if (!v->pending)
1105 save_pending_values(v->current) ;
1106 v->pending_op = KEY_EQ ;
1110 show_display(v->MPdisp_val) ;
1111 if (v->error) set_item(OPITEM, vstrs[(int) V_CLR]) ;
1112 else set_item(OPITEM, "") ; /* Redisplay pending op. (if any). */
1116 grey_buttons(v->base) ; /* Just show numeric keys for current base. */
1121 do_point(void) /* Handle numeric point. */
1123 /* the financial state is false - last key was not a fin. key */
1130 STRCPY(v->display, ".") ;
1133 else STRNCAT(v->display, ".", 1) ;
1138 set_item(DISPLAYITEM, v->display) ;
1139 MPstr_to_num(v->display, v->base, v->MPdisp_val) ;
1148 /* the financial state is false - last key was not a fin. key */
1151 if (IS_KEY(v->current, KEY_ABS)) /* Abs */
1153 mpstr(v->MPdisp_val, MP1) ;
1154 mpabs(MP1, v->MPdisp_val) ;
1156 else if (IS_KEY(v->current, KEY_FRAC)) /* Frac */
1158 mpstr(v->MPdisp_val, MP1) ;
1159 mpcmf(MP1, v->MPdisp_val) ;
1161 else if (IS_KEY(v->current, KEY_INT)) /* Int */
1163 mpstr(v->MPdisp_val, MP1) ;
1164 mpcmim(MP1, v->MPdisp_val) ;
1166 show_display(v->MPdisp_val) ;
1171 do_shift(void) /* Perform bitwise shift on display value. */
1173 int MPtemp[MP_SIZE], shift ;
1177 shift = char_val(v->current) ;
1178 if(strcmp(v->snum, v->display) != 0)
1180 MPstr_to_num(v->display, v->base, MPtemp) ;
1181 mpcmd(MPtemp, &dval) ;
1184 mpcmd(v->MPdisp_val, &dval) ;
1185 temp = ibool(dval) ;
1187 if (IS_KEY(v->pending, KEY_LSFT)) temp = temp << shift ;
1188 else if (IS_KEY(v->pending, KEY_RSFT)) temp = temp >> shift ;
1190 dval = setbool((BOOLEAN)temp) ;
1191 mpcdm(&dval, v->MPdisp_val) ;
1192 show_display(v->MPdisp_val) ;
1193 mpstr(v->MPdisp_val, v->MPlast_input) ;
1199 do_sto_rcl(void) /* Save/restore value to/from memory register. */
1201 int i, MPn[MP_SIZE], n ;
1203 for (i = MEM_START; i <= MEM_END; i++)
1204 if (v->current == menu_entries[i].val)
1206 if (IS_KEY(v->pending, KEY_RCL)) /* Rcl */
1208 mpstr(v->MPmvals[char_val(v->current)], v->MPdisp_val) ;
1211 else if (IS_KEY(v->pending, KEY_STO)) /* Sto */
1213 n = char_val(v->current) ;
1215 if (IS_KEY(v->pending_op, KEY_ADD)) /* + */
1217 mpstr(v->MPmvals[n], MPn) ;
1218 mpadd(MPn, v->MPdisp_val, v->MPmvals[n]) ;
1220 else if (IS_KEY(v->pending_op, KEY_SUB)) /* - */
1222 mpstr(v->MPmvals[n], MPn) ;
1223 mpsub(MPn, v->MPdisp_val, v->MPmvals[n]) ;
1225 else if (IS_KEY(v->pending_op, KEY_MUL)) /* x */
1227 mpstr(v->MPmvals[n], MPn) ;
1228 mpmul(MPn, v->MPdisp_val, v->MPmvals[n]) ;
1230 else if (IS_KEY(v->pending_op, KEY_DIV)) /* / */
1232 mpstr(v->MPmvals[n], MPn) ;
1233 mpdiv(MPn, v->MPdisp_val, v->MPmvals[n]) ;
1235 else mpstr(v->MPdisp_val, v->MPmvals[n]) ;
1238 make_registers(MEM) ;
1243 if (IS_KEY(v->current, KEY_ADD) || IS_KEY(v->current, KEY_SUB) ||
1244 IS_KEY(v->current, KEY_MUL) || IS_KEY(v->current, KEY_DIV))
1245 v->pending_op = v->current ;
1250 do_trig(void) /* Perform all trigonometric functions. */
1252 int i, MPtemp[MP_SIZE], MP1[MP_SIZE], MP2[MP_SIZE] ;
1254 int MPcos[MP_SIZE], MPsin[MP_SIZE] ;
1260 if (v->ttype == DEG)
1263 mpmul(v->MPdisp_val, MP1, MP2) ;
1266 mpdiv(MP2, MP1, MPtemp) ;
1268 else if (v->ttype == GRAD)
1271 mpmul(v->MPdisp_val, MP1, MP2) ;
1274 mpdiv(MP2, MP1, MPtemp) ;
1276 else mpstr(v->MPdisp_val, MPtemp) ;
1278 else mpstr(v->MPdisp_val, MPtemp) ;
1282 if (IS_KEY(v->current, KEY_COS)) /* Cos */
1283 mpcos(MPtemp, v->MPtresults[(int) RAD]) ;
1284 else if (IS_KEY(v->current, KEY_SIN)) /* Sin */
1285 mpsin(MPtemp, v->MPtresults[(int) RAD]) ;
1286 else if (IS_KEY(v->current, KEY_TAN)) /* Tan */
1288 mpsin(MPtemp, MPsin) ;
1289 mpcos(MPtemp, MPcos) ;
1290 mpcmd(MPcos, &cval) ;
1291 if (cval == 0.0) doerr(vstrs[(int) V_ERROR]) ;
1292 mpdiv(MPsin, MPcos, v->MPtresults[(int) RAD]) ;
1297 if (IS_KEY(v->current, KEY_COS)) /* Cosh */
1298 mpcosh(MPtemp, v->MPtresults[(int) RAD]) ;
1299 else if (IS_KEY(v->current, KEY_SIN)) /* Sinh */
1300 mpsinh(MPtemp, v->MPtresults[(int) RAD]) ;
1301 else if (IS_KEY(v->current, KEY_TAN)) /* Tanh */
1302 mptanh(MPtemp, v->MPtresults[(int) RAD]) ;
1305 mpstr(v->MPtresults[(int) RAD], v->MPtresults[(int) DEG]) ;
1306 mpstr(v->MPtresults[(int) RAD], v->MPtresults[(int) GRAD]) ;
1312 if (IS_KEY(v->current, KEY_COS)) /* Acos */
1313 mpacos(v->MPdisp_val, v->MPdisp_val) ;
1314 else if (IS_KEY(v->current, KEY_SIN)) /* Asin */
1315 mpasin(v->MPdisp_val, v->MPdisp_val) ;
1316 else if (IS_KEY(v->current, KEY_TAN)) /* Atan */
1317 mpatan(v->MPdisp_val, v->MPdisp_val) ;
1321 if (IS_KEY(v->current, KEY_COS)) /* Acosh */
1322 mpacosh(v->MPdisp_val, v->MPdisp_val) ;
1323 else if (IS_KEY(v->current, KEY_SIN)) /* Asinh */
1324 mpasinh(v->MPdisp_val, v->MPdisp_val) ;
1325 else if (IS_KEY(v->current, KEY_TAN)) /* Atanh */
1326 mpatanh(v->MPdisp_val, v->MPdisp_val) ;
1333 mpmul(v->MPdisp_val, MP1, MP2) ;
1335 mpdiv(MP2, MP1, v->MPtresults[(int) DEG]) ;
1339 mpmul(v->MPdisp_val, MP1, MP2) ;
1341 mpdiv(MP2, MP1, v->MPtresults[(int) GRAD]) ;
1345 mpstr(v->MPdisp_val, v->MPtresults[(int) DEG]) ;
1346 mpstr(v->MPdisp_val, v->MPtresults[(int) GRAD]) ;
1349 mpstr(v->MPdisp_val, v->MPtresults[(int) RAD]) ;
1352 show_display(v->MPtresults[(int) v->ttype]) ;
1353 mpstr(v->MPtresults[(int) v->ttype], v->MPdisp_val) ;
1359 do_trigtype(void) /* Change the current trigonometric type. */
1361 if (v->current == TRIG_DEG) v->ttype = DEG ;
1362 else if (v->current == TRIG_GRA) v->ttype = GRAD ;
1363 else if (v->current == TRIG_RAD) v->ttype = RAD ;
1366 if (IS_KEY(v->cur_op, KEY_COS) ||
1367 IS_KEY(v->cur_op, KEY_SIN) ||
1368 IS_KEY(v->cur_op, KEY_TAN))
1370 mpstr(v->MPtresults[(int) v->ttype], v->MPdisp_val) ;
1371 show_display(v->MPtresults[(int) v->ttype]) ;
1373 set_option_menu((int) TTYPEITEM, (int)v->ttype);
1383 if (x > 68719476736.00) return(0) ;
1384 else if (x < -68719476736.00) return(0) ;
1387 while (x < 0.0) x += 4294967296.00 ;
1388 while (x >= 4294967296.00) x -= 4294967296.00 ;
1399 if (x > 9007199254740991.00 || x < -9007199254740991.00)
1405 while (x < 0.0) x += 4294967296.00 ;
1406 while (x >= 4294967296.00) x -= 4294967296.00 ;
1413 /* The following MP routines were not in the Brent FORTRAN package. They are
1414 * derived here, in terms of the existing routines.
1417 /* MP precision arc cosine.
1419 * 1. If (x < -1.0 or x > 1.0) then report DOMAIN error and return 0.0.
1421 * 2. If (x = 0.0) then acos(x) = PI/2.
1423 * 3. If (x = 1.0) then acos(x) = 0.0
1425 * 4. If (x = -1.0) then acos(x) = PI.
1427 * 5. If (0.0 < x < 1.0) then acos(x) = atan(sqrt(1-(x**2)) / x)
1429 * 6. If (-1.0 < x < 0.0) then acos(x) = atan(sqrt(1-(x**2)) / x) + PI
1433 mpacos(int *MPx, int *MPretval)
1435 int MP0[MP_SIZE], MP1[MP_SIZE], MP2[MP_SIZE] ;
1436 int MPn1[MP_SIZE], MPpi[MP_SIZE], MPy[MP_SIZE], val ;
1446 if (mpgt(MPx, MP1) || mplt(MPx, MPn1))
1448 doerr("acos DOMAIN error") ;
1449 mpstr(MP0, MPretval) ;
1451 else if (mpeq(MPx, MP0))
1454 mpdivi(MPpi, &val, MPretval) ;
1456 else if (mpeq(MPx, MP1)) mpstr(MP0, MPretval) ;
1457 else if (mpeq(MPx, MPn1)) mpstr(MPpi, MPretval) ;
1460 mpmul(MPx, MPx, MP2) ;
1461 mpsub(MP1, MP2, MP2) ;
1463 mpdiv(MP2, MPx, MP2) ;
1465 if (mpgt(MPx, MP0)) mpstr(MPy, MPretval) ;
1466 else mpadd(MPy, MPpi, MPretval) ;
1471 /* MP precision hyperbolic arc cosine.
1473 * 1. If (x < 1.0) then report DOMAIN error and return 0.0.
1475 * 2. acosh(x) = log(x + sqrt(x**2 - 1))
1479 mpacosh(int *MPx, int *MPretval)
1481 int MP1[MP_SIZE], val ;
1487 doerr("acosh DOMAIN error") ;
1489 mpcim(&val, MPretval) ;
1493 mpmul(MPx, MPx, MP1) ;
1495 mpaddi(MP1, &val, MP1) ;
1497 mpadd(MPx, MP1, MP1) ;
1498 mpln(MP1, MPretval) ;
1503 /* MP precision hyperbolic arc sine.
1505 * 1. asinh(x) = log(x + sqrt(x**2 + 1))
1509 mpasinh(int *MPx, int *MPretval)
1511 int MP1[MP_SIZE], val ;
1513 mpmul(MPx, MPx, MP1) ;
1515 mpaddi(MP1, &val, MP1) ;
1517 mpadd(MPx, MP1, MP1) ;
1518 mpln(MP1, MPretval) ;
1522 /* MP precision hyperbolic arc tangent.
1524 * 1. If (x <= -1.0 or x >= 1.0) then report a DOMAIn error and return 0.0.
1526 * 2. atanh(x) = 0.5 * log((1 + x) / (1 - x))
1530 mpatanh(int *MPx, int *MPretval)
1532 int MP0[MP_SIZE], MP1[MP_SIZE], MP2[MP_SIZE] ;
1533 int MP3[MP_SIZE], MPn1[MP_SIZE], val ;
1542 if (mpge(MPx, MP1) || mple(MPx, MPn1))
1544 doerr("atanh DOMAIN error") ;
1545 mpstr(MP0, MPretval) ;
1549 mpadd(MP1, MPx, MP2) ;
1550 mpsub(MP1, MPx, MP3) ;
1551 mpdiv(MP2, MP3, MP3) ;
1553 MPstr_to_num("0.5", DEC, MP1) ;
1554 mpmul(MP1, MP3, MPretval) ;
1559 /* MP precision common log.
1561 * 1. log10(x) = log10(e) * log(x)
1565 mplog10(int *MPx, int *MPretval)
1567 int MP1[MP_SIZE], MP2[MP_SIZE], n ;
1573 mpdiv(MP2, MP1, MPretval) ;
1578 process_parens(char current)
1581 int last_lpar ; /* Position in stack of last left paren. */
1582 int last_num ; /* Position is numeric stack to start processing. */
1584 /* Check to see if this is the first outstanding parenthesis. If so, and
1585 * their is a current operation already defined, then push the current
1586 * result on the numeric stack, and note it on the op stack, with a -1,
1587 * which has this special significance.
1588 * Zeroise current display value (in case of invalid operands inside the
1590 * Add the current pending operation to the opstack.
1591 * Increment parentheses count.
1594 if (IS_KEY(current, KEY_LPAR))
1596 if (!v->noparens && v->cur_op != '?')
1598 push_num(v->MPresult) ;
1601 mpcim(&i, v->MPdisp_val) ;
1602 push_op(v->cur_op) ;
1604 v->noparens++ ; /* Count of left brackets outstanding. */
1605 save_pending_values(current) ;
1608 /* If we haven't had any left brackets yet, and this is a right bracket,
1609 * then just ignore it.
1610 * Decrement the bracket count.
1611 * Add a equals to the op stack, to force a calculation to be performed
1612 * for two op operands. This is ignored if the preceding element of the
1613 * op stack was an immediate operation.
1614 * Work out where the preceding left bracket is in the stack, and then
1615 * process the stack from that point until this end, pushing the result
1616 * on the numeric stack, and setting the new op stack pointer appropriately.
1617 * If there are no brackets left unmatched, then clear the pending flag,
1618 * clear the stack pointers and current operation, and show the display.
1621 else if (IS_KEY(current, KEY_RPAR))
1625 last_lpar = v->opsptr - 1 ;
1626 last_num = v->numsptr ;
1627 while (!IS_KEY(v->opstack[last_lpar], KEY_LPAR))
1629 if (v->opstack[last_lpar] == -1) last_num-- ;
1632 process_stack(last_lpar + 1, last_num, v->opsptr - last_lpar - 1) ;
1638 process_stack(0, 0, v->opsptr) ;
1640 v->pending = v->opsptr = v->numsptr = 0 ;
1642 set_item(OPITEM, "") ;
1645 set_item(DISPLAYITEM, vstrs[(int) V_ERROR]) ;
1646 set_item(OPITEM, vstrs[(int) V_CLR]) ;
1647 STRCPY(v->display, vstrs[(int) V_ERROR]) ;
1651 show_display(v->MPdisp_val) ;
1652 mpstr(v->MPdisp_val, v->MPlast_input) ;
1662 push_num(int *MPval) /* Try to push value onto the numeric stack. */
1664 if (v->numsptr < 0) return ;
1665 if (v->numsptr >= MAXSTACK)
1667 STRCPY(v->display, vstrs[(int) V_NUMSTACK]) ;
1668 set_item(DISPLAYITEM, v->display) ;
1671 set_item(OPITEM, vstrs[(int) V_CLR]) ;
1675 if (v->MPnumstack[v->numsptr] == NULL)
1676 v->MPnumstack[v->numsptr] =
1677 (int *) LINT_CAST(calloc(1, sizeof(int) * MP_SIZE)) ;
1678 mpstr(MPval, v->MPnumstack[v->numsptr++]) ;
1684 push_op(int val) /* Try to push value onto the operand stack. */
1686 if (v->opsptr < 0) return ;
1687 if (v->opsptr >= MAXSTACK)
1689 STRCPY(v->display, vstrs[(int) V_OPSTACK]) ;
1690 set_item(DISPLAYITEM, v->display) ;
1692 set_item(OPITEM, vstrs[(int) V_CLR]) ;
1694 else v->opstack[v->opsptr++] = val ;
1699 save_pending_values(int val)
1704 for (n = 0; n < TITEMS; n++)
1706 if (val == buttons[n].value)
1709 v->pending_win = v->curwin ;
1710 if (v->pending_win == FCP_MODE)
1711 v->pending_mode = v->modetype ;
1721 q = p & 0x80000000 ;
1724 if (q) val += 2147483648.0 ;
1729 do_round(double result, int ndigits)
1731 char buf2[40], buffer[100];
1734 if (isnan(result)) return result;
1735 #if defined(_AIX) || defined(__aix) || defined(sun)
1736 temp = finite(result);
1738 return (temp > 0) ? HUGE : -HUGE;
1740 if ((temp = isinf(result))) return (temp > 0) ? HUGE : -HUGE;
1741 #endif /* _AIX or sun */
1743 if (ndigits >= 0 && ndigits < MAX_DIGITS)
1745 result += 0.5 * (result > 0 ? mods[ndigits] : -mods[ndigits]);
1746 result -= fmod(result, mods[ndigits]);
1749 sprintf(buf2, "%%.%dlg", MAX_DIGITS);
1750 sprintf(buffer, buf2, result);
1751 return atof(buffer);
1755 try_compute_i(double guess, double *result, int method)
1757 double sum_pos, sum_pos_prime, sum_neg, sum_neg_prime, w = guess;
1763 double term, term_prime, f, f_prime, lsp, lsn;
1765 sum_pos = sum_pos_prime = sum_neg = sum_neg_prime = 0;
1767 if (v->MPfvals[2] != 0.0)
1772 term_prime = v->MPfvals[0];
1776 term = pow(w, v->MPfvals[0]);
1777 term_prime = (v->MPfvals[0]) * pow(w, v->MPfvals[0] - 1.0);
1779 if (v->MPfvals[2] > 0.0)
1781 sum_pos += v->MPfvals[2] * term;
1782 sum_pos_prime += v->MPfvals[2] * term_prime;
1786 sum_neg -= v->MPfvals[2] * term;
1787 sum_neg_prime -= v->MPfvals[2] * term_prime;
1790 if (v->MPfvals[3] != 0.0)
1794 term = v->MPfvals[0];
1796 term_prime = v->MPfvals[0] * (v->MPfvals[0] - 1) / 2.0 +
1797 v->MPfvals[0] * (0.0);
1801 double wn = pow(w, v->MPfvals[0]);
1802 double wdb = pow(w, 0.0);
1804 term = (wn - 1.0) * wdb / (w - 1.0);
1806 term_prime = (v->MPfvals[0] * pow(w,(0.0 + v->MPfvals[0] - .01))
1807 + (wn - 1.0) * (0.0) * pow(w, (0.0 - 1.0))) /
1808 (w - 1.0) - (wn - 1.0) * wdb /
1809 ((w - 1.0) * (w - 1.0));
1812 if (v->MPfvals[3] > 0.0)
1814 sum_pos += v->MPfvals[3] * term;
1815 sum_pos_prime += v->MPfvals[3] * term_prime;
1819 sum_neg -= v->MPfvals[3] * term;
1820 sum_neg_prime -= v->MPfvals[3] * term_prime;
1823 if (v->MPfvals[4] != 0.0)
1825 if (v->MPfvals[4] > 0.0) sum_pos += v->MPfvals[4];
1826 else sum_neg -= v->MPfvals[4];
1837 f_prime = sum_pos_prime / sum_pos - sum_neg_prime / sum_neg;
1840 f = lsp / lsn - 1.0;
1841 f_prime = (lsn * sum_pos_prime / sum_pos -
1842 lsp * sum_neg_prime / sum_neg) /
1847 new_w = w - f / f_prime;
1849 #if defined(_AIX) || defined(__aix) || defined(sun)
1850 if (!(!isnan(new_w) && finite(new_w)))
1853 if (!(!isnan(new_w) && !isinf(new_w)))
1855 #endif /* _AIX or sun */
1857 if (new_w == w || (w != 0.0 && fabs((new_w - w) / w) < FIN_EPSILON))
1862 if (niter++ >= MAX_FIN_ITER)
1866 *result = do_round((new_w - 1.0) * 100.0 * v->MPfvals[5], -1);
1871 compute_i(double *target)
1874 double first_period, last_period;
1881 if (first_period < 0.0 || last_period < 0.0)
1883 doerr(GETMESSAGE(5, 5, "ERROR:Invalid odd period values"));
1887 p[0] = v->MPfvals[2] + (first_period == 0.0 ? v->MPfvals[3] : 0);
1888 p[1] = v->MPfvals[3];
1889 p[2] = v->MPfvals[4] + (last_period == 0.0 ? v->MPfvals[3] : 0);
1891 nsc = count_sign_changes(p, 3);
1895 int MP1[MP_SIZE], MP2[MP_SIZE], MP3[MP_SIZE], MP4[MP_SIZE];
1900 temp = v->MPfvals[4]/v->MPfvals[2];
1904 mpcdm(&(v->MPfvals[0]), MP4);
1905 mpdiv(MP2, MP4, MP3) ;
1906 mppwr2(MP1, MP3, MP5) ;
1908 mpaddi(MP5, &val, MP1) ;
1910 mpmuli(MP1, &val, v->MPdisp_val) ;
1911 mpcmd(v->MPdisp_val, target);
1916 doerr(GETMESSAGE(5, 3, "ERROR: Multiple Solutions"));
1919 else if (v->MPfvals[0] <= 0)
1921 doerr(GETMESSAGE(5, 4, "ERROR: Term <= 0"));
1925 success = try_compute_i((double)1.0, target, 1);
1926 success = success || try_compute_i((double)1.0e-12, target, 1);
1927 success = success || try_compute_i((double)1.0, target, 2);
1928 success = success || try_compute_i((double)1.0e-12, target, 2);
1931 doerr(GETMESSAGE(5, 1, "ERROR: Computation Failed"));
1935 count_sign_changes(double *cf, int count)
1937 int i, curr_sign = 0, result = 0;
1939 for (i = 0; i < count; i++)
1941 if (cf[i] == 0.0) continue;
1945 if (cf[i] > 0.0) continue;
1949 else if (curr_sign == -1)
1951 if (cf[i] < 0.0) continue;
1957 if (cf[i] > 0.0) curr_sign = 1;
1958 else curr_sign = -1;