1*59d799daSIngo Weinhold
2*59d799daSIngo Weinhold /*
3*59d799daSIngo Weinhold * M_APM - mapm_log.c
4*59d799daSIngo Weinhold *
5*59d799daSIngo Weinhold * Copyright (C) 1999 - 2007 Michael C. Ring
6*59d799daSIngo Weinhold *
7*59d799daSIngo Weinhold * Permission to use, copy, and distribute this software and its
8*59d799daSIngo Weinhold * documentation for any purpose with or without fee is hereby granted,
9*59d799daSIngo Weinhold * provided that the above copyright notice appear in all copies and
10*59d799daSIngo Weinhold * that both that copyright notice and this permission notice appear
11*59d799daSIngo Weinhold * in supporting documentation.
12*59d799daSIngo Weinhold *
13*59d799daSIngo Weinhold * Permission to modify the software is granted. Permission to distribute
14*59d799daSIngo Weinhold * the modified code is granted. Modifications are to be distributed by
15*59d799daSIngo Weinhold * using the file 'license.txt' as a template to modify the file header.
16*59d799daSIngo Weinhold * 'license.txt' is available in the official MAPM distribution.
17*59d799daSIngo Weinhold *
18*59d799daSIngo Weinhold * This software is provided "as is" without express or implied warranty.
19*59d799daSIngo Weinhold */
20*59d799daSIngo Weinhold
21*59d799daSIngo Weinhold /*
22*59d799daSIngo Weinhold * $Id: mapm_log.c,v 1.29 2007/12/03 01:44:19 mike Exp $
23*59d799daSIngo Weinhold *
24*59d799daSIngo Weinhold * This file contains the LOG and LOG10 functions.
25*59d799daSIngo Weinhold *
26*59d799daSIngo Weinhold * $Log: mapm_log.c,v $
27*59d799daSIngo Weinhold * Revision 1.29 2007/12/03 01:44:19 mike
28*59d799daSIngo Weinhold * Update license
29*59d799daSIngo Weinhold *
30*59d799daSIngo Weinhold * Revision 1.28 2003/07/21 20:18:06 mike
31*59d799daSIngo Weinhold * Modify error messages to be in a consistent format.
32*59d799daSIngo Weinhold *
33*59d799daSIngo Weinhold * Revision 1.27 2003/06/02 17:22:46 mike
34*59d799daSIngo Weinhold * put 'log_near_1' into it's own separate module
35*59d799daSIngo Weinhold *
36*59d799daSIngo Weinhold * Revision 1.26 2003/05/12 17:42:46 mike
37*59d799daSIngo Weinhold * only check for 'near 1' if exponent is 0 or 1
38*59d799daSIngo Weinhold *
39*59d799daSIngo Weinhold * Revision 1.25 2003/05/04 21:08:25 mike
40*59d799daSIngo Weinhold * *** empty log message ***
41*59d799daSIngo Weinhold *
42*59d799daSIngo Weinhold * Revision 1.24 2003/05/01 21:58:34 mike
43*59d799daSIngo Weinhold * remove math.h
44*59d799daSIngo Weinhold *
45*59d799daSIngo Weinhold * Revision 1.23 2003/05/01 21:39:09 mike
46*59d799daSIngo Weinhold * use 'abs' call
47*59d799daSIngo Weinhold *
48*59d799daSIngo Weinhold * Revision 1.22 2003/05/01 19:44:57 mike
49*59d799daSIngo Weinhold * optimize log_near_1 by calculating fewer digits
50*59d799daSIngo Weinhold * on subsequent iterations
51*59d799daSIngo Weinhold *
52*59d799daSIngo Weinhold * Revision 1.21 2003/03/31 22:00:56 mike
53*59d799daSIngo Weinhold * call generic error handling function
54*59d799daSIngo Weinhold *
55*59d799daSIngo Weinhold * Revision 1.20 2003/03/30 22:57:13 mike
56*59d799daSIngo Weinhold * call a new iterative log function which is cubically convergent
57*59d799daSIngo Weinhold *
58*59d799daSIngo Weinhold * Revision 1.19 2002/11/03 22:14:45 mike
59*59d799daSIngo Weinhold * Updated function parameters to use the modern style
60*59d799daSIngo Weinhold *
61*59d799daSIngo Weinhold * Revision 1.18 2001/07/16 19:21:16 mike
62*59d799daSIngo Weinhold * add function M_free_all_log
63*59d799daSIngo Weinhold *
64*59d799daSIngo Weinhold * Revision 1.17 2000/10/22 00:24:29 mike
65*59d799daSIngo Weinhold * minor optimization
66*59d799daSIngo Weinhold *
67*59d799daSIngo Weinhold * Revision 1.16 2000/10/21 16:22:50 mike
68*59d799daSIngo Weinhold * use an improved log_near_1 algorithm
69*59d799daSIngo Weinhold *
70*59d799daSIngo Weinhold * Revision 1.15 2000/10/20 16:49:33 mike
71*59d799daSIngo Weinhold * update algorithm for basic log function and add new
72*59d799daSIngo Weinhold * function when input is close to '1'
73*59d799daSIngo Weinhold *
74*59d799daSIngo Weinhold * Revision 1.14 2000/09/23 19:48:21 mike
75*59d799daSIngo Weinhold * change divide call to reciprocal
76*59d799daSIngo Weinhold *
77*59d799daSIngo Weinhold * Revision 1.13 2000/07/11 18:58:35 mike
78*59d799daSIngo Weinhold * do it right this time
79*59d799daSIngo Weinhold *
80*59d799daSIngo Weinhold * Revision 1.12 2000/07/11 18:19:27 mike
81*59d799daSIngo Weinhold * estimate a better initial precision
82*59d799daSIngo Weinhold *
83*59d799daSIngo Weinhold * Revision 1.11 2000/05/19 16:14:15 mike
84*59d799daSIngo Weinhold * update some comments
85*59d799daSIngo Weinhold *
86*59d799daSIngo Weinhold * Revision 1.10 2000/05/17 23:47:35 mike
87*59d799daSIngo Weinhold * recompute a local copy of log E base 10 on the fly
88*59d799daSIngo Weinhold * if more precision is needed.
89*59d799daSIngo Weinhold *
90*59d799daSIngo Weinhold * Revision 1.9 2000/03/27 21:44:12 mike
91*59d799daSIngo Weinhold * determine how many iterations should be required at
92*59d799daSIngo Weinhold * run time for log
93*59d799daSIngo Weinhold *
94*59d799daSIngo Weinhold * Revision 1.8 1999/07/21 02:56:18 mike
95*59d799daSIngo Weinhold * added some comments
96*59d799daSIngo Weinhold *
97*59d799daSIngo Weinhold * Revision 1.7 1999/07/19 00:28:51 mike
98*59d799daSIngo Weinhold * adjust local precision again
99*59d799daSIngo Weinhold *
100*59d799daSIngo Weinhold * Revision 1.6 1999/07/19 00:10:34 mike
101*59d799daSIngo Weinhold * adjust local precision during iterative loop
102*59d799daSIngo Weinhold *
103*59d799daSIngo Weinhold * Revision 1.5 1999/07/18 23:15:54 mike
104*59d799daSIngo Weinhold * change local precision dynamically and change
105*59d799daSIngo Weinhold * tolerance to integers for faster iterative routine.
106*59d799daSIngo Weinhold *
107*59d799daSIngo Weinhold * Revision 1.4 1999/06/19 21:08:32 mike
108*59d799daSIngo Weinhold * changed local static variables to MAPM stack variables
109*59d799daSIngo Weinhold *
110*59d799daSIngo Weinhold * Revision 1.3 1999/05/15 01:34:50 mike
111*59d799daSIngo Weinhold * add check for number of decimal places
112*59d799daSIngo Weinhold *
113*59d799daSIngo Weinhold * Revision 1.2 1999/05/10 21:42:32 mike
114*59d799daSIngo Weinhold * added some comments
115*59d799daSIngo Weinhold *
116*59d799daSIngo Weinhold * Revision 1.1 1999/05/10 20:56:31 mike
117*59d799daSIngo Weinhold * Initial revision
118*59d799daSIngo Weinhold */
119*59d799daSIngo Weinhold
120*59d799daSIngo Weinhold #include "m_apm_lc.h"
121*59d799daSIngo Weinhold
122*59d799daSIngo Weinhold /****************************************************************************/
123*59d799daSIngo Weinhold /*
124*59d799daSIngo Weinhold Calls the LOG function. The formula used is :
125*59d799daSIngo Weinhold
126*59d799daSIngo Weinhold log10(x) = A * log(x) where A = log (e) [0.43429448190325...]
127*59d799daSIngo Weinhold 10
128*59d799daSIngo Weinhold */
m_apm_log10(M_APM rr,int places,M_APM aa)129*59d799daSIngo Weinhold void m_apm_log10(M_APM rr, int places, M_APM aa)
130*59d799daSIngo Weinhold {
131*59d799daSIngo Weinhold int dplaces;
132*59d799daSIngo Weinhold M_APM tmp8, tmp9;
133*59d799daSIngo Weinhold
134*59d799daSIngo Weinhold tmp8 = M_get_stack_var();
135*59d799daSIngo Weinhold tmp9 = M_get_stack_var();
136*59d799daSIngo Weinhold
137*59d799daSIngo Weinhold dplaces = places + 4;
138*59d799daSIngo Weinhold M_check_log_places(dplaces + 45);
139*59d799daSIngo Weinhold
140*59d799daSIngo Weinhold m_apm_log(tmp9, dplaces, aa);
141*59d799daSIngo Weinhold m_apm_multiply(tmp8, tmp9, MM_lc_log10R);
142*59d799daSIngo Weinhold m_apm_round(rr, places, tmp8);
143*59d799daSIngo Weinhold M_restore_stack(2); /* restore the 2 locals we used here */
144*59d799daSIngo Weinhold }
145*59d799daSIngo Weinhold /****************************************************************************/
m_apm_log(M_APM r,int places,M_APM a)146*59d799daSIngo Weinhold void m_apm_log(M_APM r, int places, M_APM a)
147*59d799daSIngo Weinhold {
148*59d799daSIngo Weinhold M_APM tmp0, tmp1, tmp2;
149*59d799daSIngo Weinhold int mexp, dplaces;
150*59d799daSIngo Weinhold
151*59d799daSIngo Weinhold if (a->m_apm_sign <= 0)
152*59d799daSIngo Weinhold {
153*59d799daSIngo Weinhold M_apm_log_error_msg(M_APM_RETURN, "\'m_apm_log\', Negative argument");
154*59d799daSIngo Weinhold M_set_to_zero(r);
155*59d799daSIngo Weinhold return;
156*59d799daSIngo Weinhold }
157*59d799daSIngo Weinhold
158*59d799daSIngo Weinhold tmp0 = M_get_stack_var();
159*59d799daSIngo Weinhold tmp1 = M_get_stack_var();
160*59d799daSIngo Weinhold tmp2 = M_get_stack_var();
161*59d799daSIngo Weinhold
162*59d799daSIngo Weinhold dplaces = places + 8;
163*59d799daSIngo Weinhold
164*59d799daSIngo Weinhold /*
165*59d799daSIngo Weinhold * if the input is real close to 1, use the series expansion
166*59d799daSIngo Weinhold * to compute the log.
167*59d799daSIngo Weinhold *
168*59d799daSIngo Weinhold * 0.9999 < a < 1.0001
169*59d799daSIngo Weinhold */
170*59d799daSIngo Weinhold
171*59d799daSIngo Weinhold mexp = a->m_apm_exponent;
172*59d799daSIngo Weinhold
173*59d799daSIngo Weinhold if (mexp == 0 || mexp == 1)
174*59d799daSIngo Weinhold {
175*59d799daSIngo Weinhold m_apm_subtract(tmp0, a, MM_One);
176*59d799daSIngo Weinhold
177*59d799daSIngo Weinhold if (tmp0->m_apm_sign == 0) /* is input exactly 1 ?? */
178*59d799daSIngo Weinhold { /* if so, result is 0 */
179*59d799daSIngo Weinhold M_set_to_zero(r);
180*59d799daSIngo Weinhold M_restore_stack(3);
181*59d799daSIngo Weinhold return;
182*59d799daSIngo Weinhold }
183*59d799daSIngo Weinhold
184*59d799daSIngo Weinhold if (tmp0->m_apm_exponent <= -4)
185*59d799daSIngo Weinhold {
186*59d799daSIngo Weinhold M_log_near_1(r, places, tmp0);
187*59d799daSIngo Weinhold M_restore_stack(3);
188*59d799daSIngo Weinhold return;
189*59d799daSIngo Weinhold }
190*59d799daSIngo Weinhold }
191*59d799daSIngo Weinhold
192*59d799daSIngo Weinhold /* make sure our log(10) is accurate enough for this calculation */
193*59d799daSIngo Weinhold /* (and log(2) which is called from M_log_basic_iteration) */
194*59d799daSIngo Weinhold
195*59d799daSIngo Weinhold M_check_log_places(dplaces + 25);
196*59d799daSIngo Weinhold
197*59d799daSIngo Weinhold if (abs(mexp) <= 3)
198*59d799daSIngo Weinhold {
199*59d799daSIngo Weinhold M_log_basic_iteration(r, places, a);
200*59d799daSIngo Weinhold }
201*59d799daSIngo Weinhold else
202*59d799daSIngo Weinhold {
203*59d799daSIngo Weinhold /*
204*59d799daSIngo Weinhold * use log (x * y) = log(x) + log(y)
205*59d799daSIngo Weinhold *
206*59d799daSIngo Weinhold * here we use y = exponent of our base 10 number.
207*59d799daSIngo Weinhold *
208*59d799daSIngo Weinhold * let 'C' = log(10) = 2.3025850929940....
209*59d799daSIngo Weinhold *
210*59d799daSIngo Weinhold * then log(x * y) = log(x) + ( C * base_10_exponent )
211*59d799daSIngo Weinhold */
212*59d799daSIngo Weinhold
213*59d799daSIngo Weinhold m_apm_copy(tmp2, a);
214*59d799daSIngo Weinhold
215*59d799daSIngo Weinhold mexp = tmp2->m_apm_exponent - 2;
216*59d799daSIngo Weinhold tmp2->m_apm_exponent = 2; /* force number between 10 & 100 */
217*59d799daSIngo Weinhold
218*59d799daSIngo Weinhold M_log_basic_iteration(tmp0, dplaces, tmp2);
219*59d799daSIngo Weinhold
220*59d799daSIngo Weinhold m_apm_set_long(tmp1, (long)mexp);
221*59d799daSIngo Weinhold m_apm_multiply(tmp2, tmp1, MM_lc_log10);
222*59d799daSIngo Weinhold m_apm_add(tmp1, tmp2, tmp0);
223*59d799daSIngo Weinhold
224*59d799daSIngo Weinhold m_apm_round(r, places, tmp1);
225*59d799daSIngo Weinhold }
226*59d799daSIngo Weinhold
227*59d799daSIngo Weinhold M_restore_stack(3); /* restore the 3 locals we used here */
228*59d799daSIngo Weinhold }
229*59d799daSIngo Weinhold /****************************************************************************/
230