5 * Copyright (C) 1999 - 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_log.c,v 1.29 2007/12/03 01:44:19 mike Exp $
24 * This file contains the LOG and LOG10 functions.
26 * $Log: mapm_log.c,v $
27 * Revision 1.29 2007/12/03 01:44:19 mike
30 * Revision 1.28 2003/07/21 20:18:06 mike
31 * Modify error messages to be in a consistent format.
33 * Revision 1.27 2003/06/02 17:22:46 mike
34 * put 'log_near_1' into it's own separate module
36 * Revision 1.26 2003/05/12 17:42:46 mike
37 * only check for 'near 1' if exponent is 0 or 1
39 * Revision 1.25 2003/05/04 21:08:25 mike
40 * *** empty log message ***
42 * Revision 1.24 2003/05/01 21:58:34 mike
45 * Revision 1.23 2003/05/01 21:39:09 mike
48 * Revision 1.22 2003/05/01 19:44:57 mike
49 * optimize log_near_1 by calculating fewer digits
50 * on subsequent iterations
52 * Revision 1.21 2003/03/31 22:00:56 mike
53 * call generic error handling function
55 * Revision 1.20 2003/03/30 22:57:13 mike
56 * call a new iterative log function which is cubically convergent
58 * Revision 1.19 2002/11/03 22:14:45 mike
59 * Updated function parameters to use the modern style
61 * Revision 1.18 2001/07/16 19:21:16 mike
62 * add function M_free_all_log
64 * Revision 1.17 2000/10/22 00:24:29 mike
67 * Revision 1.16 2000/10/21 16:22:50 mike
68 * use an improved log_near_1 algorithm
70 * Revision 1.15 2000/10/20 16:49:33 mike
71 * update algorithm for basic log function and add new
72 * function when input is close to '1'
74 * Revision 1.14 2000/09/23 19:48:21 mike
75 * change divide call to reciprocal
77 * Revision 1.13 2000/07/11 18:58:35 mike
78 * do it right this time
80 * Revision 1.12 2000/07/11 18:19:27 mike
81 * estimate a better initial precision
83 * Revision 1.11 2000/05/19 16:14:15 mike
84 * update some comments
86 * Revision 1.10 2000/05/17 23:47:35 mike
87 * recompute a local copy of log E base 10 on the fly
88 * if more precision is needed.
90 * Revision 1.9 2000/03/27 21:44:12 mike
91 * determine how many iterations should be required at
94 * Revision 1.8 1999/07/21 02:56:18 mike
97 * Revision 1.7 1999/07/19 00:28:51 mike
98 * adjust local precision again
100 * Revision 1.6 1999/07/19 00:10:34 mike
101 * adjust local precision during iterative loop
103 * Revision 1.5 1999/07/18 23:15:54 mike
104 * change local precision dynamically and change
105 * tolerance to integers for faster iterative routine.
107 * Revision 1.4 1999/06/19 21:08:32 mike
108 * changed local static variables to MAPM stack variables
110 * Revision 1.3 1999/05/15 01:34:50 mike
111 * add check for number of decimal places
113 * Revision 1.2 1999/05/10 21:42:32 mike
114 * added some comments
116 * Revision 1.1 1999/05/10 20:56:31 mike
120 #include "m_apm_lc.h"
122 /****************************************************************************/
124 Calls the LOG function. The formula used is :
126 log10(x) = A * log(x) where A = log (e) [0.43429448190325...]
129 void m_apm_log10(M_APM rr
, int places
, M_APM aa
)
134 tmp8
= M_get_stack_var();
135 tmp9
= M_get_stack_var();
137 dplaces
= places
+ 4;
138 M_check_log_places(dplaces
+ 45);
140 m_apm_log(tmp9
, dplaces
, aa
);
141 m_apm_multiply(tmp8
, tmp9
, MM_lc_log10R
);
142 m_apm_round(rr
, places
, tmp8
);
143 M_restore_stack(2); /* restore the 2 locals we used here */
145 /****************************************************************************/
146 void m_apm_log(M_APM r
, int places
, M_APM a
)
148 M_APM tmp0
, tmp1
, tmp2
;
151 if (a
->m_apm_sign
<= 0)
153 M_apm_log_error_msg(M_APM_RETURN
, "\'m_apm_log\', Negative argument");
158 tmp0
= M_get_stack_var();
159 tmp1
= M_get_stack_var();
160 tmp2
= M_get_stack_var();
162 dplaces
= places
+ 8;
165 * if the input is real close to 1, use the series expansion
166 * to compute the log.
168 * 0.9999 < a < 1.0001
171 mexp
= a
->m_apm_exponent
;
173 if (mexp
== 0 || mexp
== 1)
175 m_apm_subtract(tmp0
, a
, MM_One
);
177 if (tmp0
->m_apm_sign
== 0) /* is input exactly 1 ?? */
178 { /* if so, result is 0 */
184 if (tmp0
->m_apm_exponent
<= -4)
186 M_log_near_1(r
, places
, tmp0
);
192 /* make sure our log(10) is accurate enough for this calculation */
193 /* (and log(2) which is called from M_log_basic_iteration) */
195 M_check_log_places(dplaces
+ 25);
199 M_log_basic_iteration(r
, places
, a
);
204 * use log (x * y) = log(x) + log(y)
206 * here we use y = exponent of our base 10 number.
208 * let 'C' = log(10) = 2.3025850929940....
210 * then log(x * y) = log(x) + ( C * base_10_exponent )
215 mexp
= tmp2
->m_apm_exponent
- 2;
216 tmp2
->m_apm_exponent
= 2; /* force number between 10 & 100 */
218 M_log_basic_iteration(tmp0
, dplaces
, tmp2
);
220 m_apm_set_long(tmp1
, (long)mexp
);
221 m_apm_multiply(tmp2
, tmp1
, MM_lc_log10
);
222 m_apm_add(tmp1
, tmp2
, tmp0
);
224 m_apm_round(r
, places
, tmp1
);
227 M_restore_stack(3); /* restore the 3 locals we used here */
229 /****************************************************************************/