5 * Copyright (C) 2000 - 2007 Michael C. Ring
7 * Permission to use, copy, and distribute this software and its
8 * documentation for any purpose with or without fee is hereby granted,
9 * provided that the above copyright notice appear in all copies and
10 * that both that copyright notice and this permission notice appear
11 * in supporting documentation.
13 * Permission to modify the software is granted. Permission to distribute
14 * the modified code is granted. Modifications are to be distributed by
15 * using the file 'license.txt' as a template to modify the file header.
16 * 'license.txt' is available in the official MAPM distribution.
18 * This software is provided "as is" without express or implied warranty.
22 * $Id: mapm_pow.c,v 1.10 2007/12/03 01:46:07 mike Exp $
24 * This file contains the POW function.
26 * $Log: mapm_pow.c,v $
27 * Revision 1.10 2007/12/03 01:46:07 mike
30 * Revision 1.9 2002/11/05 23:39:42 mike
31 * use new set_to_zero call
33 * Revision 1.8 2002/11/03 22:20:59 mike
34 * Updated function parameters to use the modern style
36 * Revision 1.7 2001/07/16 19:24:26 mike
37 * add function M_free_all_pow
39 * Revision 1.6 2000/09/05 22:15:03 mike
42 * Revision 1.5 2000/08/22 21:22:29 mike
43 * if parameter yy is an integer, call the more
44 * efficient _integer_pow function
46 * Revision 1.4 2000/08/22 20:42:08 mike
47 * compute more digits in the log calculation
49 * Revision 1.3 2000/05/24 20:08:21 mike
50 * update some comments
52 * Revision 1.2 2000/05/23 23:20:11 mike
53 * return 1 when input is 0^0.
55 * Revision 1.1 2000/05/18 22:10:43 mike
61 static M_APM M_last_xx_input
;
62 static M_APM M_last_xx_log
;
63 static int M_last_log_digits
;
64 static int M_size_flag
= 0;
66 /****************************************************************************/
71 m_apm_free(M_last_xx_input
);
72 m_apm_free(M_last_xx_log
);
76 /****************************************************************************/
78 Calculate the POW function by calling EXP :
81 X = e where A = Y * log(X)
83 void m_apm_pow(M_APM rr
, int places
, M_APM xx
, M_APM yy
)
89 /* if yy == 0, return 1 */
91 if (yy
->m_apm_sign
== 0)
93 m_apm_copy(rr
, MM_One
);
97 /* if xx == 0, return 0 */
99 if (xx
->m_apm_sign
== 0)
105 if (M_size_flag
== 0) /* init locals on first call */
107 M_size_flag
= M_get_sizeof_int();
108 M_last_log_digits
= 0;
109 M_last_xx_input
= m_apm_init();
110 M_last_xx_log
= m_apm_init();
114 * if 'yy' is a small enough integer, call the more
115 * efficient _integer_pow function.
118 if (m_apm_is_integer(yy
))
122 if (M_size_flag
== 2) /* 16 bit compilers */
124 if (yy
->m_apm_exponent
<= 4)
127 else /* >= 32 bit compilers */
129 if (yy
->m_apm_exponent
<= 7)
135 m_apm_to_integer_string(sbuf
, yy
);
136 m_apm_integer_pow(rr
, places
, xx
, atoi(sbuf
));
141 tmp8
= M_get_stack_var();
142 tmp9
= M_get_stack_var();
145 * If parameter 'X' is the same this call as it
146 * was the previous call, re-use the saved log
147 * calculation from last time.
152 if (M_last_log_digits
>= places
)
154 if (m_apm_compare(xx
, M_last_xx_input
) == 0)
160 m_apm_round(tmp9
, (places
+ 8), M_last_xx_log
);
164 m_apm_log(tmp9
, (places
+ 8), xx
);
166 M_last_log_digits
= places
+ 2;
168 /* save the 'X' input value and the log calculation */
170 m_apm_copy(M_last_xx_input
, xx
);
171 m_apm_copy(M_last_xx_log
, tmp9
);
174 m_apm_multiply(tmp8
, tmp9
, yy
);
175 m_apm_exp(rr
, places
, tmp8
);
176 M_restore_stack(2); /* restore the 2 locals we used here */
178 /****************************************************************************/